Created
November 16, 2012 09:17
-
-
Save rebornix/4085783 to your computer and use it in GitHub Desktop.
Weibo Relation
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
import urllib2 | |
import urllib | |
import json | |
import ConfigParser | |
COMMENTS_SHOW = 'https://api.weibo.com/2/comments/show.json' | |
USER_TIMELINE = 'https://api.weibo.com/2/statuses/user_timeline.json' | |
HDR = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', | |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', | |
'Accept-Encoding': 'none', | |
'Accept-Language': 'en-US,en;q=0.8', | |
'Connection': 'keep-alive'} | |
def check_related(access_token, statuses, main_uid, comment_uid): | |
if len(statuses['statuses']) == 0: | |
print 'no update' | |
return | |
print len(statuses['statuses']) | |
for status in statuses['statuses']: | |
#TODO Check whether there is any @ | |
item_data = {} | |
item_data['id'] = status['id'] | |
item_data['access_token'] = access_token | |
item_values = urllib.urlencode(item_data) | |
full_url = COMMENTS_SHOW + '?' + item_values | |
try: | |
req = urllib2.Request(full_url, headers=HDR) | |
comments = json.loads(urllib2.urlopen(req).read()) | |
except Exception as ex: | |
print 'comment JSON Exception', ex | |
continue | |
for comment in comments['comments']: | |
if comment['user']['id'] == int(comment_uid): | |
# save this status item into database | |
print status['text'], status['created_at'] | |
break | |
def update(): | |
config = ConfigParser.RawConfigParser() | |
config.read('memory.cfg') | |
ACCESS_TOKEN = config.get('setting', 'access_token') | |
REBORN_UID = config.get('reborn', 'reborn_uid') | |
REGINA_UID = config.get('regina', 'regina_uid') | |
data = {} | |
data['access_token'] = ACCESS_TOKEN | |
data['count'] = 32 | |
data['page'] = 7 | |
# Check Reborn | |
print 'check reborn' | |
data['uid'] = REBORN_UID | |
if config.has_option('reborn', 'reborn_since_id'): | |
data['since_id'] = config.get('reborn', 'reborn_since_id') | |
full_url = USER_TIMELINE + '?' + urllib.urlencode(data) | |
# fake as a browser | |
try: | |
req = urllib2.Request(full_url, headers=HDR) | |
statuses = json.loads(urllib2.urlopen(req).read()) | |
check_related(ACCESS_TOKEN, statuses, REBORN_UID, REGINA_UID) | |
if len(statuses['statuses']) != 0: | |
config.set('reborn', 'reborn_since_id', statuses['statuses'][0]['id']) | |
except Exception as ex: | |
print 'reborn statuses exception', ex | |
# Check Regina | |
print 'check regina' | |
data['uid'] = REGINA_UID | |
if 'since_id' in data.keys(): | |
del data['since_id'] | |
if config.has_option('regina', 'regina_since_id'): | |
data['since_id'] = config.get('regina', 'regina_since_id') | |
full_url = USER_TIMELINE + '?' + urllib.urlencode(data) | |
try: | |
req = urllib2.Request(full_url, headers=HDR) | |
statuses = json.loads(urllib2.urlopen(req).read()) | |
check_related(ACCESS_TOKEN, statuses, REGINA_UID, REBORN_UID) | |
if len(statuses['statuses']) != 0: | |
config.set('regina', 'regina_since_id', statuses['statuses'][0]['id']) | |
except Exception: | |
print 'regina statuses exception' | |
print 'write memory' | |
with open('memory.cfg', 'wb') as configfile: | |
config.write(configfile) | |
if __name__ == '__main__': | |
update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment