Created
August 12, 2012 08:18
-
-
Save hachibeeDI/3330627 to your computer and use it in GitHub Desktop.
get odakyu-line infomation. from its official twitter acounts
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import json | |
import urllib2 | |
from datetime import * | |
_MONTH = {u'Jan':u'01', u'Feb':u'02', u'Mar':u'03', u'Apr':u'04', | |
u'May':u'05', u'Jun':u'06', u'Jul':u'07', u'Aug':u'08', | |
u'Sep':u'09', u'Oct':u'10', u'Nov':u'11', u'Dec':u'12'} | |
def main(): | |
url = 'http://api.twitter.com/1/statuses/user_timeline/odakyuline_info.json?count=1' | |
contents = urllib2.urlopen(url) | |
tweets = contents.readlines() | |
val = json.loads(tweets[0],'utf-8') | |
if _isNew(val[0]['created_at']): | |
return val[0]['text'] | |
else: | |
return 'no recent info' | |
def _isNew(twitime): | |
#twitterに合わせて世界標準時刻 | |
tmp = twitime.split(' ') | |
tmp_times = tmp[3].split(':') | |
twitime = datetime(int(tmp[-1]), int(_MONTH[tmp[1]]), int(tmp[2]), | |
int(tmp_times[0]), int(tmp_times[1]), int(tmp_times[2])) | |
gap = datetime.utcnow() - twitime | |
return (gap <= timedelta(hours=3)) | |
if __name__ == '__main__': | |
sys.stdout.write(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment