Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import os
- import time
- import atom.data
- import gdata.sites.client
- import gdata.sites.data
- import gdata.gauth
- """Fetch latest deals from Google search, and update Google Sites with them"""
- import urllib2, re
- from time import sleep
- from pprint import pprint
- from datetime import datetime, date, timedelta
- from apiclient.discovery import build
- def weekly_special(market, wednesday):
- day = wednesday.strftime("%d").lstrip("0")
- month = wednesday.strftime("%m").lstrip("0")
- # because tightarse on ozbargains like it this way
- hit = google_search("intitle:%s/%s" % (day, month) + " intitle:" + market)
- # pprint(hit)
- if not hit:
- print("Google search didn't hit.")
- return
- url = 'https://www.ozbargain.com.au/node/198392'
- url = hit['formattedUrl']
- response = urllib2.urlopen(url)
- html = response.read()
- match = re.search('<div itemprop="description"[^>]*>(.*?)</div>', html, re.S)
- if not match:
- print("Couldn't find description")
- else:
- return match.group(1).replace(
- 'href="/', 'href="http://ozbargain.com.au/'
- ) + '''<p>This information is aggregated from
- <a href="%s">ozbargain</a>.
- </p>''' % url
- def google_search(query):
- # query = query + " tightarse"
- # Build a service object for interacting with the API. Visit
- # the Google APIs Console <http://code.google.com/apis/console>
- # to get an API key for your own application.
- # this developerKey and CX belongs to [email protected]
- service = build("customsearch", "v1",
- developerKey="AIzaSyBB_WSkYTglFeKA0RhnDnvmdF2gh79F2lE")
- i = 1
- print("Searching '%s'" % query)
- while (True):
- res = service.cse().list(
- q=query,
- cx='015457467921696338036:jni2mvdmu78',
- start=i,
- ).execute()
- if res['searchInformation']['totalResults'] == '0':
- return False
- else:
- for item in res['items']:
- print(item['formattedUrl'])
- print("\t" + item['htmlTitle'])
- return res['items'][0]
- # list sites under this account - not for production but for debugging
- def print_list_sites(client):
- feed = client.GetSiteFeed()
- print 'Google Sites associated with your account: '
- counter = 0
- for entry in feed.entry:
- print ' %i %s (%s)' % (counter,entry.title.text, entry.site_name.text)
- counter = counter + 1
- print ' --- The End ---'
- def get_webpages(client):
- kind = 'webpage'
- print 'Fetching only %s entries' % kind
- uri = '%s?kind=%s' % (client.MakeContentFeedUri(), kind)
- feed = client.GetContentFeed(uri=uri)
- print "Fetching content feed of '%s'...\n" % client.site
- feed = client.GetContentFeed()
- uri = '%s?kind=%s' % (client.MakeContentFeedUri(),'webpage')
- feed = client.GetContentFeed(uri=uri)
- return {entry.title.text: entry for entry in feed.entry}
- #return feed.entry[0]
- def copy_search_result_to(page):
- since_wednesday = timedelta(days = ((date.today().weekday() - 2)%7 ))
- formatter = '<html:div xmlns:html="http://www.w3.org/1999/xhtml">%s</html:div>'
- html = weekly_special(page.title.text, date.today() - since_wednesday)
- print(html)
- page.content.html = (formatter % html).encode('utf8')
- print("Page %s content obtained" % page.title.text)
- def test(client):
- pprint(get_webpages(client))
- def authorize_google_sites_client(client_id, client_secret, client):
- # obtain oauth2 token
- token_cache_path=os.environ['HOME']+'/.gdata-storage'
- print "Token Cache: %s" % token_cache_path
- try:
- with open(token_cache_path, 'r') as f:
- saved_blob_string=f.read()
- if saved_blob_string is not None:
- token = gdata.gauth.token_from_blob(saved_blob_string)
- else:
- token = None
- except IOError:
- token = None
- if token == None :
- print "Getting a new token."
- token = gdata.gauth.OAuth2Token( client_id=client_id,
- client_secret=client_secret,
- scope='https://sites.google.com/feeds/',
- user_agent='whatson-sale-updater')
- url = token.generate_authorize_url(redirect_uri='urn:ietf:wg:oauth:2.0:oob')
- print 'Please go to the URL below and authorize this '
- print 'application, then enter the code it gives you.'
- print ' %s' % url
- code = raw_input("Code: ")
- token.get_access_token(code)
- token.authorize(client)
- saved_blob_string = gdata.gauth.token_to_blob(token)
- f=open (token_cache_path, 'w')
- f.write(saved_blob_string)
- else:
- print "Using a cached token from %s" % token_cache_path
- token.authorize(client)
- f.close()
- client_id = 'your_id'
- client_secret = 'your_secret'
- client = gdata.sites.client.SitesClient(source='whatson-sale-updater', site='yoursite')
- authorize_google_sites_client(client_id, client_secret, client)
- pages = get_webpages(client)
- copy_search_result_to(pages['Woolworths'])
- client.Update(pages['Woolworths'])
- copy_search_result_to(pages['Coles'])
- client.Update(pages['Coles'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement