Created
April 21, 2013 03:40
-
-
Save zwass/5428374 to your computer and use it in GitHub Desktop.
Really old work on aggregating RSS feeds into a Twitter account. Style is lacking, could really use some lovin'.
This file contains hidden or 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 os | |
import time | |
import json | |
from urllib import urlencode | |
import tweepy | |
import feedparser | |
import pymongo | |
import requests | |
from BeautifulSoup import BeautifulSoup | |
#get auth info from environment variables | |
## CONSUMER_KEY = os.environ.get('CONSUMER_KEY') | |
## CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET') | |
## ACCESS_KEY = os.environ.get('ACCESS_KEY') | |
## ACCESS_SECRET = os.environ.get('ACCESS_SECRET') | |
## #do auth process | |
## auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
## auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) | |
## API = tweepy.API(auth) | |
#get bitly creds | |
BITLY_USERNAME = os.environ.get('BITLY_USERNAME') | |
BITLY_API_KEY = os.environ.get('BITLY_API_KEY') | |
#get db | |
mongo_uri = os.environ.get('MONGOLAB_URI') | |
db_name = mongo_uri.split("/")[-1] | |
connection = pymongo.Connection(mongo_uri) | |
db = connection[db_name] | |
FOOTERS = ["#cisatpenn"] | |
DELAY = 12 | |
def send_tweet(message, url): | |
final_message = message.replace("\n", " ") | |
#add in footers | |
footers = [url] | |
footers.extend(FOOTERS) | |
footer = "... " + " ".join(footers) | |
text_len = 140 - len(footer) | |
final_message = message[:text_len] + footer | |
print final_message | |
return | |
try: | |
API.update_status(final_message) | |
except Exception as e: | |
#this will print to heroku logs | |
print e | |
def get_feeds(feed_urls): | |
#we don't care if this takes a while... | |
feeds = [feedparser.parse(url)['items'] for url in feed_urls] | |
#sort each feed, newest first | |
return zip(feed_urls, feeds) #tuples of feed url and the parsed feed | |
def html_to_text(html): | |
converted = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES) | |
stripped = ''.join(converted.findAll(text=True)) | |
return stripped | |
def shorten_url(url): | |
query = {"login": BITLY_USERNAME, | |
"apiKey": BITLY_API_KEY, | |
"longUrl": url, | |
"format": "json"} | |
bitly_url = "https://api-ssl.bitly.com/v3/shorten?%s" % (urlencode(query)) | |
response = json.loads(requests.get(bitly_url).text) | |
if response['status_code'] != 200: | |
raise Exception("Bitly returned error: %s" % (response['status_txt'])) | |
short_url = response['data']['url'] | |
return short_url.split("http://")[1] | |
if __name__ == "__main__": | |
feed_urls = [line.rstrip() for line in open("feeds.txt").readlines()] | |
while True: #daemon | |
for url, feed in get_feeds(feed_urls): | |
if len(feed) > 0: | |
newest = feed[0] | |
title = newest['title'] | |
#get the title of the last post we tweeted for this person | |
already_posted = db.posts.find_one({'url': url}) | |
if not already_posted or title != already_posted['title']: | |
#then we haven't tweeted this yet | |
description = html_to_text(newest['description']) | |
post_url = newest['link'] | |
tweet_text = "%s: %s" % (title, description) | |
send_tweet(tweet_text, shorten_url(post_url)) #Tweet! Tweet! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment