Created
April 16, 2012 19:48
-
-
Save honzajavorek/2401048 to your computer and use it in GitHub Desktop.
Jednoduchý FB group Pyonieri -> @naPyVo bot splácaný za hodinu na koleně
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import imaplib | |
import re | |
import base64 | |
import quopri | |
import urllib | |
import redis | |
import tweepy | |
# redis | |
print 'Redis...' | |
redis_server = redis.Redis('localhost') | |
print 'Twitter...' | |
consumer_key='j8LLDtko8lFxsHmjJi8w' | |
consumer_secret='PyzBJyiO2gmM8SK9aFkD0lm3OMHrYQAGepSAYWCZE' | |
access_token='293644462-tW11axCa3khtEESUH9DgySq5WJqnWZfUgJKEb4KZ' | |
access_token_secret='JFh0FxKr0ZrlDAnuKFqE7atVTRuMJt9Om65xRyVqA' | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
# login into my mailbox using username and password | |
print 'Gmail...' | |
mail = imaplib.IMAP4_SSL('imap.gmail.com') | |
mail.login('[email protected]', base64.b64decode('amUtbGkgbmFzZSBjaXNsbyAzOA==')) | |
# get all emails in selected label '✉ MAILING LISTS/✩ Pyonýři' | |
mail.select('&Jwk- MAILING LISTS/&Jyk- Pyon&AP0BWQ-i') | |
result, data = mail.search(None, 'ALL') | |
ids = data[0].split() # ids is a space separated string | |
# iterate over emails in order from the oldest to the newest | |
def generate_links(ids, redis_server): | |
for email_id in ids: | |
# check if present in redis | |
if not redis_server.sadd('pyvo:tweeted', email_id): | |
continue | |
# get raw email data | |
result, data = mail.fetch(email_id, '(RFC822)') # fetch the email body (RFC822) for the given ID | |
raw_email = data[0][1] # here's the body, which is raw text of the whole email (including headers and alternate payloads) | |
# get only plaintext part of the email | |
plaintext_parts = [part for part in re.split(r'\n\-\-', raw_email) if re.search(r'Content\-Type: text\/plain', part)] | |
if not plaintext_parts: | |
continue | |
plaintext = plaintext_parts[0] | |
# separate headers and content | |
headers, content = re.split(r'\r?\n\r?\n', plaintext, 1) | |
encoding = re.search(r'Content\-Transfer\-Encoding: ([^\n]+)', headers, flags=re.I).group(1).strip() | |
charset = re.search(r'charset=["\']?([\w\-_]+)', headers, flags=re.I).group(1).strip() | |
# decode content | |
if encoding == 'quoted-printable': | |
content = unicode(quopri.decodestring(content), charset).strip() | |
else: | |
continue | |
# find author | |
try: | |
author = re.search(r'^([\w ]+) posted in Pyonieri', content, flags=re.I|re.U).group(1).strip() | |
except AttributeError: | |
continue | |
# find link | |
try: | |
link = re.search(r'http://www\.facebook\.com/l/[^/]+/([^\s<,]+)', content).group(1).strip() | |
if link.startswith('https'): | |
link = urllib.unquote(link) | |
else: | |
link = 'http://' + link | |
except AttributeError: | |
continue | |
yield (email_id, author, link) | |
# process links | |
print 'Processing...' | |
try: | |
email_id, author, link = next(generate_links(ids, redis_server)) | |
try: | |
print 'Tweeting... (%s: %s)' % (author, link) | |
api.update_status(u'%s na http://on.fb.me/pyoneers sdílí něco zajímavého: %s' % (author, link)) | |
except tweepy.error.TweepError: | |
print 'Tweeting failed.' | |
redis_server.srem('pyvo:tweeted', email_id) | |
except StopIteration: | |
print 'No more links available now.' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment