Skip to content

Instantly share code, notes, and snippets.

@nqnwebs
Created June 7, 2012 04:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nqnwebs/2886591 to your computer and use it in GitHub Desktop.
Google +1 feed generator
# -*- coding: utf-8 -*-
"""
Google Plus has no feeds for my +1 and I want to share them in my blog.
This is an incredible simple scrapper to hack that stupidly wrong policy.
My contents are mine!
author: Martín Gaitán <gaitan@gmail.com>
How to use?
-----------
set your GOOGLE_ID and run it. you'll get a plusone.xml file
Dependencies: pyquery and PyRSS2Gen (both are pip-installables)
"""
import datetime
from pyquery import PyQuery
import PyRSS2Gen
######
GOOGLE_ID = '102449284377784435533'
######
def _(text):
"""latin1 to utf8 transcode helper"""
try:
return text.encode('latin1').decode('utf8')
except UnicodeError:
return text
class PlusOne2Rss(object):
def __init__(self, google_id):
self.url = 'https://plus.google.com/%s/plusones' % google_id
self.pq = PyQuery(url=self.url)
self.user_name = self.pq('a.a-n[href="%s"]' % google_id).text()
def _title(self, table):
return _(self.pq('div.zCa a.QC', table).text())
def _link(self, table):
return _(self.pq('div.zCa a.QC', table).attr('href'))
def _description(self, table):
return _(self.pq('div.zCa p.GCa', table).text())
def _image_url(pq, table):
return _(self.pq('div.BT a', table).attr('title'))
def write_feed(self, filename='plusone.xml'):
rss_items = []
tables = self.pq('table.KX')
for table in tables:
item = PyRSS2Gen.RSSItem(
title = self._title(table),
link = self._link(table),
description = self._description(table),
guid = PyRSS2Gen.Guid(self._link(table)),
pubDate = datetime.datetime.now()
)
rss_items.append(item)
rss = PyRSS2Gen.RSS2(
title = _("%s's +1s" % self.user_name),
link = self.url,
generator= u'plusone2rss',
description = _("The latest %s's +1s" % self.user_name.split(' ')[0]),
lastBuildDate = datetime.datetime.utcnow(),
items = rss_items
)
rss.write_xml(open(filename, "w"), encoding='utf-8')
if __name__ == '__main__':
PlusOne2Rss(GOOGLE_ID).write_feed()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment