Skip to content
This repository has been archived by the owner. It is now read-only.
Browse files

Added setup script

  • Loading branch information
simoncadman committed Nov 10, 2011
1 parent e6801a4 commit a82fde539e3451d11ef8b36beb12472d0134a08b
Showing with 174 additions and 0 deletions.
  1. +3 −0
  2. +171 −0
@@ -30,6 +30,9 @@ def __init__( self ):
def get ( self, section, key ):
return self.config.get(section, key)

def set ( self, section, key, value ):
return self.config.set(section, key, value)

def save (self ):
with open(self.configfile, 'wb') as configdetail:
@@ -0,0 +1,171 @@
#! /usr/bin/python

import sys, getpass, mimetools, urllib, urllib2, json, cups
from config import Config

CRLF = '\r\n'
BOUNDARY = mimetools.choose_boundary()

# The following are used for authentication functions.
FOLLOWUP_URI = 'select%2Fgaiaauth'
LOGIN_URI = '/accounts/ServiceLoginAuth'
SERVICE = 'cloudprint'

# The following are used for general backend access.
# CLIENT_NAME should be some string identifier for the client you are writing.
CLIENT_NAME = 'CUPS Cloud Print'

useConfigDetails = True

configuration = Config()
except Exception as error:
useConfigDetails = False

# test username and password

def GetAuthTokens(email, password):
"""Assign login credentials from GAIA accounts service.
email: Email address of the Google account to use.
password: Cleartext password of the email account.
dictionary containing Auth token.
tokens = {}

# We still need to get the Auth token.
params = {'accountType': 'GOOGLE',
'Email': email,
'Passwd': password,
'service': SERVICE,
'source': CLIENT_NAME}
stream = urllib.urlopen(LOGIN_URL, urllib.urlencode(params))

success = False
for line in stream:
if line.strip().startswith('Auth='):
tokens['Auth'] = line.strip().replace('Auth=', '')
success = True

if not success:
return None

return tokens

def getPrinters(proxy=None):
response = GetUrl('%s/search?q=' % (CLOUDPRINT_URL), tokens)
responseobj = json.loads(response)
if 'printers' in responseobj and len(responseobj['printers']) > 0:
return responseobj['printers']
return None

def GetUrl(url, tokens, data=None, cookies=False, anonymous=False):
"""Get URL, with GET or POST depending data, adds Authorization header.
url: Url to access.
tokens: dictionary of authentication tokens for specific user.
data: If a POST request, data to be sent with the request.
cookies: boolean, True = send authentication tokens in cookie headers.
anonymous: boolean, True = do not send login credentials.
String: response to the HTTP request.
request = urllib2.Request(url)
request.add_header('X-CloudPrint-Proxy', 'api-prober')
if not anonymous:
if cookies:
logger.debug('Adding authentication credentials to cookie header')
request.add_header('Cookie', 'SID=%s; HSID=%s; SSID=%s' % (
tokens['SID'], tokens['HSID'], tokens['SSID']))
else: # Don't add Auth headers when using Cookie header with auth tokens.
request.add_header('Authorization', 'GoogleLogin auth=%s' % tokens['Auth'])
if data:
request.add_header('Content-Length', str(len(data)))
request.add_header('Content-Type', 'multipart/form-data;boundary=%s' % BOUNDARY)

# In case the gateway is not responding, we'll retry.
retry_count = 0
while retry_count < 5:
result = urllib2.urlopen(request).read()
return result
except urllib2.HTTPError, e:
# We see this error if the site goes down. We need to pause and retry.
err_msg = 'Error accessing %s\n%s' % (url, e)
logger.error(err_msg)'Pausing %d seconds', 60)
retry_count += 1
if retry_count == 5:
return err_msg

def printerNameToUri( printer ) :
printer = urllib.quote(printer)
return 'cloudprint://' + printer

def AddPrinter( printername, uri ) :
print "Setting up",printername,uri
# fix printer name
printername = printername.replace(' ','_')
result = None
result = connection.addPrinter(name=printername,ppdname='CloudPrint.ppd',info=printername,location='Google Cloud Print',device=uri)
connection.setPrinterShared(printername, False)
except Exception as error:
result = error
if result == None:
print "Added",printername
print "Error adding:",printername,result
return None

tokens = None
success = False
while success == False:
if useConfigDetails:
print "Using authentication details from configuration"
username = configuration.get('Google', 'username')
password = configuration.get('Google', 'password')
print "Please enter your Google Credentials, or CTRL+C to exit: "
username = raw_input("Username: ")
password = getpass.getpass()

tokens = GetAuthTokens(username, password)
if tokens == None:
print "Invalid username/password"
success = False
useConfigDetails = False
print "Successfully connected"
success = True

connection = cups.Connection()
cupsprinters = connection.getPrinters()

printers = getPrinters()
if printers == None:
print "No Printers Found"

for printer in printers:
uri = printerNameToUri(printer['name'])
found = False
for cupsprinter in cupsprinters:
if cupsprinters[cupsprinter]['device-uri'] == uri:
found = True
if found == False:
AddPrinter(printer['name'], uri)

0 comments on commit a82fde5

Please sign in to comment.
You can’t perform that action at this time.