Created
September 5, 2013 10:01
-
-
Save etataurov/6448212 to your computer and use it in GitHub Desktop.
Check email messages from GetPocket and get original article url
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
import tornado.ioloop | |
from datetime import timedelta | |
import imaplib | |
import quopri | |
import requests | |
import logging | |
from bs4 import BeautifulSoup | |
log = logging.getLogger(__name__) | |
logging.basicConfig(level=logging.DEBUG) | |
LOGIN = '' | |
PASSWORD = '' | |
def set_timer(func): | |
ioloop = tornado.ioloop.IOLoop.current() | |
ioloop.add_timeout(timedelta(seconds=20), func) | |
def get_original_url(link): | |
log.debug('makint HTTP request to get original url') | |
response = requests.get(link) | |
return response.url | |
def parse_message(data): | |
log.debug('parsing message') | |
parsed = BeautifulSoup(quopri.decodestring(data)) | |
link = parsed.find('a', class_="title").get('href') # here we got some pocket link, we should follow it to find original | |
return get_original_url(link) | |
def make_api_request(full_url): | |
pass | |
def process_message(data): | |
url = parse_message(data) | |
# TODO parse url ? | |
if 'http://habrahabr.ru' in url: | |
log.info('Habrahabr link in message') | |
make_api_request(url) | |
def get_mail(): | |
mail = imaplib.IMAP4_SSL('imap.yandex.ru') | |
mail.login(LOGIN, PASSWORD) | |
mail.select() | |
typ, data = mail.search(None, 'ALL') | |
dd = data[0] # TODO rename | |
if dd is not None: | |
for num in dd.split(): | |
typ, data = mail.fetch(num, '(BODY[TEXT])') | |
# TODO check sender! | |
log.info('Message %s\n%s\n' % (num, data[0][1])) | |
process_message(data[0][1]) | |
else: | |
log.info("No messages") | |
mail.close() | |
mail.logout() | |
set_timer(get_mail) | |
def main(): | |
get_mail() | |
tornado.ioloop.IOLoop.current().start() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Была идея отправлять статьи из Pocket на специальную почту, которая затем проверяется, и если это статья с хабра, добавлять ее на хабре в избранное.
Остановило отсутствие нормального API у хабра