Skip to content

Instantly share code, notes, and snippets.

@ahxxm
Created October 7, 2015 10:09
Show Gist options
  • Save ahxxm/a8dc2e33b6f6dc4fcf58 to your computer and use it in GitHub Desktop.
Save ahxxm/a8dc2e33b6f6dc4fcf58 to your computer and use it in GitHub Desktop.
世说新语
# ssxy
import random
from twitter import *
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.Shishuoxinyu
'''
k = oauth_dance('twitter_username',
'',
''
)
# magic here
check k
'''
# Use oauth dance's result to fill this
auth = OAuth(
consumer_key='',
consumer_secret='',
token='',
token_secret=''
)
t = Twitter(auth=auth)
N = db.items.find({'touched':0}).count() # if N = 0, replay
if N == 0:
db.items.update({'touched':1},{"$set": {'touched':0}}, upsert=False, multi=True)
db.items.update({'touched':2},{"$set": {'touched':0}}, upsert=False, multi=True)
N = db.items.find({'touched':0}).count()
seq = int(random.random()*N)
tmp = list(db.items.find().limit(1).skip(seq))[0]
content = tmp['content']
# stats here
'''
# stat
allitem = list(db.items.find())
for item in allitem:
length = len(item['content'])
if length < 10:
print(len(post_content(item['content'])), length)
'''
# according to stat, the longest one is 373, so split by every 130 to make sure every
# item will be done in 3 status.
def post_content(content):
length = len(content)
if length == 0:
raise SystemExit
if length > 260:
contents = [content[:130], content[130:260], content[260:length]]
elif length > 180:
contents = [content[:130], content[130:length]]
elif length > 139: # half split
contents = [content[:90], content[90:length]]
else:
contents = [content]
return contents # a list
contents = post_content(content)
# get random post, mark for use
# update it
for item in contents:
t.statuses.update(status=item)
db.items.update({'md5':tmp['md5']}, {"$set": {"touched":1}})
# mark done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment