Created
August 5, 2013 16:00
-
-
Save hupili/6157080 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Convert RSS to timelineJS's JSON format | |
Usage: | |
rss2json.py <fn_rss> <fn_json> | |
''' | |
import docopt | |
arguments = docopt.docopt(__doc__) | |
fn_rss = arguments['<fn_rss>'] | |
fn_json = arguments['<fn_json>'] | |
import sys | |
sys.path.append('snsapi') | |
from snsapi.snspocket import SNSPocket | |
import json | |
import datetime | |
sp = SNSPocket() | |
conf = sp.new_channel('RSS2RW') | |
conf['channel_name'] = 'rss' | |
conf['url'] = fn_rss | |
sp.add_channel(conf) | |
result = { | |
"timeline": | |
{ | |
"headline":"Social Aggregation from SNSAPI", | |
"type":"default", | |
"startDate":"2000", | |
"text":"Social Aggregation from SNSAPI", | |
"asset": { | |
"media":"http://snsapi.ie.cuhk.edu.hk", | |
"credit":"", | |
"caption":"" | |
}, | |
"date": [ | |
] | |
} | |
} | |
story_template = { | |
"startDate":"2000,1,1", | |
"headline":"story", | |
"text":"text of story", | |
"asset": { | |
"media":"", | |
"credit":"", | |
"caption":"" | |
}, | |
} | |
def timestamp2tljs_format(ts): | |
dt = datetime.datetime.fromtimestamp(ts) | |
#story['startDate'] = '{0},{1},{2}'.format(dt.year, dt.month, dt.day) | |
return '{0},{1},{2},{3},{4},{5}'.format( | |
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second) | |
ml = sp.home_timeline(count=20) | |
for msg in ml: | |
story = story_template.copy() | |
story['startDate'] = timestamp2tljs_format(msg.parsed.time) | |
story['headline'] = msg.parsed.username + ': ' + msg.parsed.text[0:40] | |
story['text'] = msg.parsed.text | |
story['tag'] = msg.parsed.username | |
result['timeline']['date'].append(story) | |
result['timeline']['startDate'] = timestamp2tljs_format(min([m.parsed.time for m in ml])) | |
open(fn_json, 'w').write(json.dumps(result, indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Convert RSS to jquery-vertical-timeline's JSON format | |
Usage: | |
rss2json.py <fn_rss> <fn_json> | |
''' | |
import docopt | |
arguments = docopt.docopt(__doc__) | |
fn_rss = arguments['<fn_rss>'] | |
fn_json = arguments['<fn_json>'] | |
import sys | |
sys.path.append('snsapi') | |
from snsapi.snspocket import SNSPocket | |
import json | |
import datetime | |
sp = SNSPocket() | |
conf = sp.new_channel('RSS2RW') | |
conf['channel_name'] = 'rss' | |
conf['url'] = fn_rss | |
sp.add_channel(conf) | |
result = [] | |
story_template = { | |
"title": "title", | |
"date": "27 Mar 2008", | |
"displaydate": "", | |
"photourl": "", | |
"caption": "", | |
"body": "", | |
"readmoreurl": "" | |
} | |
def timestamp2tljs_format(ts): | |
dt = datetime.datetime.fromtimestamp(ts) | |
#story['startDate'] = '{0},{1},{2}'.format(dt.year, dt.month, dt.day) | |
return '{0},{1},{2},{3},{4},{5}'.format( | |
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second) | |
def timestamp2jqtlver_format(ts): | |
dt = datetime.datetime.fromtimestamp(ts) | |
_month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'] | |
_date = '{0} {1} {2} {3}:{4}:{5}'.format(_month[dt.month - 1], dt.day, dt.year, dt.hour, dt.minute, dt.second) | |
_displaydate = '{0} {1}'.format(_month[dt.month - 1], dt.day) | |
#_displaydate = _date | |
return _date, _displaydate | |
icons = { | |
"SinaWeiboStatus": | |
"http://weibo.com/favicon.ico", | |
"SinaWeiboWapStatus": | |
"http://weibo.com/favicon.ico", | |
"RenrenStatus": | |
"http://xnimg.cn/favicon.ico", | |
"RenrenShare": | |
"http://xnimg.cn/favicon.ico", | |
"RenrenFeed": | |
"http://xnimg.cn/favicon.ico", | |
"RenrenBlog": | |
"http://xnimg.cn/favicon.ico", | |
"TencentWeiboStatus": | |
"http://t.qq.com/favicon.ico", | |
"SQLite": | |
"http://www.sqlite.org/favicon.ico", | |
"TwitterStatus": | |
"https://twitter.com/favicon.ico", | |
"RSS": | |
"http://www.girlmeetsdress.com/images/Favicon_RSS.jpg", | |
"RSS2RW": | |
"http://www.girlmeetsdress.com/images/Favicon_RSS.jpg", | |
"RSSSummary": | |
"http://www.girlmeetsdress.com/images/Favicon_RSS.jpg", | |
"Email": | |
"https://mail.google.com/favicon.ico", | |
"FacebookFeed": | |
"http://facebook.com/favicon.ico", | |
} | |
ml = sp.home_timeline(count=20) | |
for msg in ml: | |
story = story_template.copy() | |
story['date'], story['displaydate'] = timestamp2jqtlver_format(msg.parsed.time) | |
#story['title'] = msg.parsed.username + ': ' + msg.parsed.text[0:40] | |
story['title'] = msg.parsed.username + ': ' + msg.parsed.text[0:10] | |
story['body'] = msg.parsed.text | |
#story['caption'] = msg.parsed.username | |
story['caption'] = story['date'] | |
#story['photourl'] = icons[msg.ID.platform] | |
result.append(story) | |
open(fn_json, 'w').write(json.dumps(result, indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment