Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# -*- coding: utf-8 -*-
import re
import random
from datetime import datetime, timedelta
import fire
import requests
from lxml import etree
from pymongo import MongoClient, ReturnDocument
client = MongoClient('mongodb://localhost:27017/')
db = client['lottery']
def get_topic(_id):
_id = str(_id)
topic = db.topic.find_one({'topicId': _id})
if not topic:
topic_url = '{}'.format(_id)
resp = requests.get(topic_url)
if resp.status_code == 200:
member = resp.json()[0]['member']
if member and member.get('username'):
topic = db.topic.find_one_and_update(
{'topicId': _id},
{'$set': {'creator': member, 'created':}},
raise Exception('topic', _id, str(
return topic
def get_replies(_id):
replies_url = '{}'.format(_id)
resp = requests.get(replies_url)
if resp.status_code == 200:
return resp.json()
raise Exception('replies', _id, str(
def petard():
notifications_url = ''
resp = requests.get(notifications_url)
if resp.status_code == 200:
ele = etree.HTML(resp.content)
for item in ele.xpath('//entry')[::-1]:
ident = item.find('id').text
name = item.find('author').find('name').text
content = item.find('content').text or ''
_id ='/t/(\d+)', ident).group(1)
count = int(([0] + re.findall(r'(\d+)', content))[-1])
published = item.find('published').text
created_timestamp = 0
if published:
created_timestamp = int((datetime.strptime(
published, '%Y-%m-%dT%H:%M:%SZ') + timedelta(
hours=8) - datetime(1970, 1, 1)).total_seconds())
if not count or not _id or not name or not ident or not created_timestamp:
topic = get_topic(_id)
if topic and not topic.get('luck'):
creator_username = topic['creator']['username']
if creator_username in [name, 'lottery']:
replies = get_replies(_id)
pool = (set(
[_i['member']['username'] for _i in replies if _i['created'] < created_timestamp]) - set(
[creator_username, 'choujiang', 'lottery']))
luck_pool = random.sample(pool, min(count, len(pool)))
if luck_pool:
{'topicId': _id},
{'$set': {'luck': luck_pool, 'lotteryCreated':}},
raise Exception('notifications')
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.