Created
October 14, 2015 20:29
-
-
Save putnik/d12b1e710d2a9d92ab2e to your computer and use it in GitHub Desktop.
@BeWikiTrends
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 -*- | |
from BeautifulSoup import BeautifulSoup as Soup | |
from soupselect import select | |
from dateutil.parser import parse | |
import tweepy | |
import urllib2 | |
import HTMLParser | |
import time | |
import re | |
import datetime | |
# Авторизация в Твиттере | |
consumer_key = '' | |
consumer_secret = '' | |
access_token = '' | |
access_token_secret = '' | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
# Загрузка данных | |
data_url = 'http://tools.wmflabs.org/wikitrends/belarusian-uptrends-today.html' | |
response = urllib2.urlopen(data_url) | |
soup = Soup(response) | |
# Проверка даты обновления. Если больше суток - этот список уже постили | |
last_updated = select(soup, '#topics p small i')[0].contents[2] | |
last_updated = re.sub(r'^.*Last updated ', '', last_updated) | |
last_updated = parse(last_updated) | |
delta = datetime.datetime.now(last_updated.tzinfo) - last_updated | |
if delta.total_seconds() > 24 * 60 * 60: | |
exit() | |
# Получение нужного элемента списка | |
pos = int(time.strftime('%H')) - 10 | |
item = select(soup, '#topics h3')[pos] | |
h = HTMLParser.HTMLParser() | |
# Получение свойств элемента | |
name = h.unescape(item.a.contents[0]) | |
trend = h.unescape(item.span.contents[0]) | |
if re.match(r'^\(\-', trend): | |
exit() # игнорирование минусовых трендов | |
url = item.a['href'] | |
# Формирование твита и отправка | |
status = name + ' ' + trend + ' ' + url | |
api.update_status(status=status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment