-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweetyourRSS.py
70 lines (63 loc) · 2.29 KB
/
tweetyourRSS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
'''
tweetyourRSS is a simple python script that enables you to publish any RSS feed
on twitter
'''
import sys
import tweepy
import pickle
from bitly import Bitly
from twitter import Twitter
from feeds import Feeds
from settings import *
__author__ = "Alberto Buratti, Mattia Larentis"
__credits__ = ["Alberto Buratti", "Mattia Larentis", "Federico Scrinzi"]
__license__ = "WTFPL"
__maintainer__ = "Alberto Buratti"
__email__ = "[email protected]"
def main():
'''
app entry point
'''
# gets a twitter object
tw = Twitter(TWITTER['CONSUMER_KEY'], TWITTER['CONSUMER_SECRET'], \
TWITTER['ACCESS_TOKEN'], TWITTER['ACCESS_TOKEN_SECRET'])
# gets a bitly object
bl = Bitly(BITLY['USER'], BITLY['APIKEY'])
# tries to load the history from the file. If an exception is raised,
# istantiates an empty dictionary object
try:
with open(HISTORY_FILE, 'rb') as history_file:
history = pickle.load(history_file)
except:
history = dict()
# cycles through the RSSs defined in settings
for rsskey, rssvalue in RSS.iteritems():
# gets a feed object
fd = Feeds(rssvalue['RSS'])
# tries to load last timestamp. If an exception is raised,
# initializes it with the init value defined in settings
try:
last_timestamp = history[rsskey]
except:
last_timestamp = (rssvalue['HISTORY'])['INIT_VALUE']
history[rsskey] = last_timestamp
# gets the updated feeds
entries = fd.get_updated_feeds(rssvalue['HISTORY'], last_timestamp)
# cycles through the feeds, tweetin them
for feed in entries:
link = bl.shorten_url(getattr(feed, rssvalue['LINK']))
tweet = getattr(feed, rssvalue['TEXT'])
length = TWITTER['TWEET_LENGTH'] - len(rssvalue['HASHTAG']) \
- len(link) - 10
tweet = rssvalue['HASHTAG'] + ' ' + tw.truncate(tweet, length) \
+ ' ' + link
tw.update_status(tweet, DEBUG)
# updates the last timestamp
history[rsskey] = fd.get_last_timestamp()
# saves the history
with open(HISTORY_FILE, 'wb') as history_file:
pickle.dump(history, history_file)
sys.exit(0)
if __name__ == "__main__":
main()