Skip to content

Instantly share code, notes, and snippets.

@simonszu
Created June 16, 2015 17:38
Show Gist options
  • Save simonszu/e6d91eaf05571f0950ed to your computer and use it in GitHub Desktop.
Save simonszu/e6d91eaf05571f0950ed to your computer and use it in GitHub Desktop.
Converts a Google Contacts database to a phonebook format used by the Cisco IP Phone series
#! /usr/bin/env ruby
# Require rubygems and the Google data gem
require 'rubygems'
require 'gdata'
require 'fileutils'
# Specifying the settings
GMAIL_USER = "[email protected]" # Your gmail username
GMAIL_PASS = "pa55w0rd" # Your gmail pass
HOSTNAME = "192.168.1.4" # The IP which the IP Phone uses to access this computer
WEBSERV_DIR = "/cisco" # The webserver dir which the phone uses to access the files. Typically the OUTPUT_DIR symlinked in wwwroot
OUTPUT_DIR = "output" # The directory for the file output. It's under this file's directory, so you'll need write permission.
# Set STDOUT to sync ALL the zeugs
STDOUT.sync = true
# Specify export directory
outdir = File.join(File.dirname(__FILE__), OUTPUT_DIR)
# Create a new client for the contacts API
client = GData::Client::Contacts.new
# Create an array for storing and sorting the results
contacts = Array.new()
# Authenticate the client
print "Logging in..."
client.clientlogin(GMAIL_USER, GMAIL_PASS)
print "Done\n"
# Get the contacts feed and convert it to valid XML
print "Getting feed..."
feed = client.get('https://www.google.com/m8/feeds/contacts/default/full?max-results=100000').to_xml
print "Done\n"
# Walk over each entry
print "Parsing feed..."
feed.elements.each('entry') do |entry|
# If the entry has one or more telephone numbers specified...
if (!entry.elements['gd:phoneNumber'].nil?)
name = entry.elements['title'].text.gsub(/ä/, "ae").gsub(/ö/, "oe").gsub(/ü/, "ue").gsub(/é/, "e") # Puts the name of the contact
entry.elements.each('gd:phoneNumber') do |number| # Walk over each telephone number
# Catch if there is a label, which is called skype. Ignore these cases. A Cisco IP phone can't call skype
if ((number.attributes.get_attribute("label").nil?) || (!number.attributes.get_attribute("label").value == "Skype"))
# If the number category (home, mobile, work, etc) is specified...
if (number.attributes.get_attribute("rel").value =~ /http:\/\/schemas.google.com\/g\/2005#(.*)/)
# Pushes these numbers with their capitalized category into the contacts array
contacts.push(name + " " + $1.capitalize + ": " + number.text.sub(/\+/, "00").gsub(/ /, ""))
# Close all open blocks. Have to fix the indentation with TextMate, though
end
end
end
end
end
print "Done\n"
# Sort the contacts array
print "Sorting the result..."
contacts.sort!
print "Done\n"
# Check what letters are used at the beginning of the contact names
print "Checking for namespaces..."
letters_used = Array.new()
contacts.each do |contact|
letters_used.push(contact[0].chr.downcase)
end
letters_used.uniq!
print "Done\n"
# Creates output directory if not existent or re-creates it to empty it
print "Preparing directory..."
FileUtils.rm_rf(outdir) unless !File.directory?(outdir)
FileUtils.mkdir(outdir)
print "Done\n"
# Creating output files
print "Creating output files and filling root menu..."
FileUtils.cd(outdir) do
File.open("directory.xml", "w") do |file|
file.puts "<?php header(\"Content-type: text/xml\"); ?>"
file.puts "<CiscoIPPhoneMenu>"
file.puts "<Title>Telefonbuch</Title>"
file.puts "<Prompt>Dir External</Prompt>"
letters_used.each do |letter|
filename = "contacts_" + letter + ".xml"
FileUtils.touch(filename)
file.puts "<MenuItem>"
file.puts "<Name>" + letter.upcase + "</Name>"
file.puts "<URL>http://" + HOSTNAME + WEBSERV_DIR + "/" + filename + "</URL>"
file.puts "</MenuItem>"
end
file.puts "</CiscoIPPhoneMenu>"
end
end
print "Done\n"
=begin
contacts.each do |contact|
file.puts "<MenuItem>"
contact.match(/(.*): (\d*)/) # Split the contacts entry at ": " between name and number
name = $1
number = $2
file.puts "<Name>" + name + "</Name>"
file.puts "<URL>Dial:" + number + "</URL>"
file.puts "</MenuItem>"
end
=end
print "All done. Here we go.\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment