Last active
September 30, 2017 09:40
-
-
Save akgnah/c76b3089170307df456b04673a525408 to your computer and use it in GitHub Desktop.
fanfou-py demo
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
# -*- coding: utf-8 -*- | |
import os | |
import re | |
import json | |
import time | |
import shelve | |
import datetime | |
import fanfou | |
curdir = os.path.dirname(os.path.abspath(__file__)) | |
db = shelve.open(os.path.join(curdir, 'tree.dbm')) | |
consumer = {'key': 'your consumer key', | |
'secret': 'your consumer secret'} | |
client = fanfou.XAuth(consumer, 'username', 'password') | |
fanfou.bound(client) | |
def format_time(s): | |
then = datetime.datetime.strptime(s, '%a %b %d %H:%M:%S +0000 %Y') | |
return (then + datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M') | |
def fetch(): | |
data = [] | |
for i in range(1, 30): | |
body = {'id': 'wangxing', 'page': i, 'count': 60, 'mode': 'lite'} | |
resp = client.statuses.user_timeline(body) # noqa | |
data.extend(json.loads(resp.read().decode())) | |
with open('tree.json', 'w') as w: | |
w.write(json.dumps(data)) | |
def put(val): | |
sent = db.get('sent') or {} | |
sent[val] = 1 | |
db['sent'] = sent | |
def get(val): | |
sent = db.get('sent') or {} | |
return sent.get(val) | |
def update(): | |
data = [] | |
tree = json.loads(open('tree.json').read()) | |
p = re.compile(r'[^@]*路[,;].*树。') | |
for item in tree: | |
if p.match(item['text']) and not get(item['rawid']): | |
data.append(item) | |
for item in data[::-1]: | |
body = {'status': '%s [%s]' % (item['text'], format_time(item['created_at']))} | |
put(item['rawid']) | |
client.statuses.update(body) | |
print('update %s' % item['rawid']) | |
if __name__ == '__main__': | |
fetch() | |
update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment