Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
v2_lottery
# -*- 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 = 'https://www.v2ex.com/api/topics/show.json?id={}'.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': datetime.now()}},
upsert=True,
return_document=ReturnDocument.AFTER,
)
else:
raise Exception('topic', _id, str(datetime.now()))
return topic
def get_replies(_id):
replies_url = 'https://www.v2ex.com/api/replies/show.json?topic_id={}'.format(_id)
resp = requests.get(replies_url)
if resp.status_code == 200:
return resp.json()
raise Exception('replies', _id, str(datetime.now()))
def petard():
notifications_url = 'https://www.v2ex.com/n/b91130c492dd22ee7a05d5ce2942348074b2f460.xml'
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 = re.search(r'/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:
continue
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:
db.topic.find_one_and_update(
{'topicId': _id},
{'$set': {'luck': luck_pool, 'lotteryCreated': datetime.now()}},
)
else:
raise Exception('notifications')
if __name__ == '__main__':
fire.Fire()
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.