PYTHON   43
rss plus
Guest on 27th August 2023 06:04:33 AM


  1. #!/usr/bin/env python
  2. # vim:et:ts=2:sw=2
  3.  
  4. import cgi, datetime, email.utils, json, re, time, urllib2
  5.  
  6. CONFIG_FILE = 'rssplus.config.json'
  7. URL = 'https://www.googleapis.com/plus/v1/people/%s/activities/public?key=%s'
  8.  
  9. def error(msg):
  10.   print('')
  11.   print(msg + '.')
  12.   exit(0)
  13.  
  14. def convertUtf8(data):
  15.   if isinstance(data, unicode):
  16.     return data.encode('utf-8')
  17.   elif isinstance(data, dict):
  18.     for key in data.keys():
  19.       data[key] = convertUtf8(data[key])
  20.   elif isinstance(data, list):
  21.     for i in range(len(data)):
  22.       data[i] = convertUtf8(data[i])
  23.   return data
  24.  
  25. def cut(s, chars):
  26.   n = -1
  27.   for c in chars:
  28.     index = s.find(c)
  29.     if index == -1:
  30.       continue
  31.     if n == -1 or index < n:
  32.       n = index
  33.   n = n + 1
  34.   if n == 0:
  35.     return s
  36.   return s[:n]
  37.  
  38. try:
  39.   config = json.load(open(CONFIG_FILE))
  40. except IOError:
  41.   error('config file missing')
  42. except ValueError:
  43.   error('config file invalid')
  44.  
  45. try:
  46.   key = config['key']
  47. except KeyError:
  48.   error('config file invalid')
  49.  
  50. form = cgi.FieldStorage()
  51. if "id" not in form:
  52.   error('"id" is required')
  53. id = form.getfirst('id', None)
  54.  
  55. try:
  56.   ids = config['ids']
  57.   if not id in ids:
  58.     error('id %s not whitelisted' % id)
  59. except KeyError:
  60.   # No whitelist, just allow everything.
  61.   pass
  62.  
  63. if not re.match(r'^[0-9]*$', id):
  64.   id = '+' + id
  65. name = id
  66.  
  67. # HTTP header
  68. print('Content-Type: application/xml; charset=UTF-8')
  69. print('Content-Disposition: inline; filename="rss2.xml"')
  70. print('')
  71.  
  72. # RSS header
  73. print('<?xml version="1.0" encoding="utf-8"?>')
  74. print('<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">')
  75. print('<channel>')
  76. print('<docs>http://blogs.law.harvard.edu/tech/rss</docs>')
  77. print('<link>https://plus.google.com/%s</link>' % id)
  78. if "desc" in form:
  79.   print('<description>%s</description>' % form.getfirst("desc", None))
  80.  
  81. plus = convertUtf8(json.load(urllib2.urlopen(URL % (id, key))))
  82.  
  83. if len(plus['items']) > 0:
  84.   name = plus['items'][0]['actor']['displayName']
  85.  
  86. print('<title>%s\'s G+ feed</title>' % name)
  87.  
  88. # RSS items
  89. for item in plus['items']:
  90.   if item['verb'] != 'post':
  91.     continue
  92.   if item['provider']['title'] != 'Google+':
  93.     continue
  94.  
  95.   url = item['url']
  96.  
  97.   date = item['published']
  98.   date = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ')
  99.   date = email.utils.formatdate(time.mktime(date.timetuple()))
  100.  
  101.   obj = item['object']['content']
  102.   lines = filter(None, obj.split('<br />'))
  103.   minl = max(0, int(form.getfirst('min', "2")) - 1)
  104.   if len(lines) <= minl:
  105.     continue
  106.   title = cut(lines[0], '.!?')
  107.  
  108.   if 'attachments' in item['object'].keys():
  109.     for att in item['object']['attachments']:
  110.       t = att['objectType']
  111.       link = None
  112.       image = None
  113.       if t == 'photo':
  114.         img = att['fullImage']
  115.         image = '<a href="%s"><img src="%s" height="%s" width="%s" /></a>' \
  116.             % (att['url'], img['url'], img['height'], img['width'])
  117.       elif t == 'article' or t == 'video':
  118.         link = '<a href="%s">%s</a>' % (att['url'], att['displayName'])
  119.         img = att['image']
  120.         image = '<a href="%s"><img src="%s" height="%s" width="%s" /></a>' \
  121.             % (att['url'], img['url'], img['height'], img['width'])
  122.       elif t == 'album':
  123.         link = '<a href="%s">%s</a>' % (att['url'], att['displayName'])
  124.         img = att['thumbnails'][0]['image']
  125.         image = '<a href="%s"><img src="%s" height="%s" width="%s" /></a>' \
  126.             % (att['url'], img['url'], img['height'], img['width'])
  127.       if link:
  128.         obj = obj + '<br />' + link
  129.       if image:
  130.         obj = obj + '<br />' + image
  131.  
  132.   # RSS item
  133.   print('<item>')
  134.   print('<title>%s</title>' % cgi.escape(title))
  135.   print('<link>%s</link>' % url)
  136.   print('<guid>%s</guid>' % url)
  137.   print('<pubDate>%s</pubDate>' % date)
  138.   print('<description><![CDATA[%s]]></description>' % obj)
  139.   print('</item>')
  140.  
  141. # RSS footer
  142. print('</channel>')
  143. print('</rss>')

Raw Paste

Login or Register to edit or fork this paste. It's free.