-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsstotwitter.py
executable file
·292 lines (253 loc) · 8.2 KB
/
rsstotwitter.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2011 Luis Cañas Díaz
# Copyright (C) 2011 GSyC/LibreSoft, Universidad Rey Juan Carlos
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors : Luis Cañas Díaz <[email protected]>
from urllib import urlopen
from xml.dom.minidom import parse
from time import time
from calendar import timegm
import re
import urllib
import twitter
import ConfigParser
import os
import getopt
import sys
import urllib2
#import Exceptions
ERROR_MSG = 'Error parsing the feeds'
class FeedParser:
def __init__(self, feed_url, feed_ns, cache_file, verbose):
self.options = ['title', 'link']
self.node_name = "item"
self.feed_url = feed_url
self.feed_ns = feed_ns
self.cache_file = cache_file
self.verbose = verbose
self.xml = parse(urlopen(self.feed_url))
def get_content(self):
try:
return self.get_data()
except:
print ERROR_MSG
return []
def get_data(self):
data = []
#xml_data = xml.getElementsByTagNameNS(self.feed_ns, self.node_name)
xml_data = self.xml.getElementsByTagName(self.node_name)
for node in xml_data:
entry={}
for option in self.options:
entry[option] = node.getElementsByTagName(option)[0].firstChild.data
data.append(entry)
thenew = self.get_new_data(data)
if ( len(thenew) < 1 ) and self.verbose:
print "No new entries"
return thenew
def get_title(self):
try:
xml_data = self.xml.getElementsByTagName("title")
except:
print ERROR_MSG
return ""
return xml_data[0].firstChild.data
def add_cache(self, data):
fd = open(self.cache_file, 'a')
for item in data:
fd.write( item["link"] +"\n")
if self.verbose:
print item["link"] + " added to cache"
fd.close()
def get_new_data(self, data):
new = []
if self.verbose:
print "Using cache file " + str(self.cache_file)
try:
fd = open(self.cache_file, 'r')
except IOError:
new = data
return new
aux = fd.readlines()
links = []
for a in aux:
links.append(a.replace('\n',''))
fd.close()
for item in data:
if item["link"] not in links:
new.append(item)
else:
break
return new
class Sender:
def __init__(self, ck, cs, atk, ats, list_messages, verbose, post, feed_title=""):
self.ck = ck
self.cs = cs
self.atk = atk
self.ats = ats
self.list_messages = list_messages
self.verbose = verbose
self.post = post
self.feed_title = feed_title
self.api = twitter.Api()
self.api = twitter.Api(username = self.ck,\
password = self.cs,\
access_token_key = self.atk,\
access_token_secret = self.ats)
self.list_messages.reverse()
def send(self):
sentm = []
for i in self.list_messages:
if self.post2twitter(i):
sentm.append(i)
return sentm
def compose_msg(self,link, text, feed_title=""):
# it limits the length of the text field
# spaces, link and feed_title are fixed
l = len(link) + len(text) + len(feed_title) + 3
if l > 140:
fixl = len(link) + len(feed_title) + 3
textl = 140 - fixl
text = text[:textl-2] + ".."
output = text + " " + link
if len(feed_title) > 0:
output = feed_title + ": "+ output
return output
def post2twitter(self, message):
text = ""
tinylink = self.tiny_url(message["link"])
if len(self.feed_title) > 0:
text = self.compose_msg(tinylink, message["title"], self.feed_title)
else:
text = self.compose_msg(tinylink, message["title"])
if self.verbose and self.post:
try:
uni = text.decode('unicode_escape')
print "Sending: " + uni.encode('latin-1')
except UnicodeEncodeError:
print "ERROR printing the message in the terminal due to encoding errors"
try:
if self.post:
status = self.api.PostUpdate(text.encode('utf-8'))
except urllib2.HTTPError:
print "Error, the message could not be posted!"
return 0
return 1
def tiny_url(self, url):
apiurl = "http://tinyurl.com/api-create.php?url="
tinyurl = urllib.urlopen(apiurl + url).read()
return tinyurl
FEED_NS = 'http://purl.org/rss/1.0/'
# Some stuff about the project
version = "0.2.3"
author = "(C) 2010 %s <%s>" % ("Luis Cañas Díaz", "[email protected]")
name = "RSS to Twitter %s - http://github.com/sanacl/rsstotwitter" % (version)
credits = "\n%s \n%s\n" % (name, author)
def usage ():
print credits
print "Usage: rsstotwitter [configuration file] [options]"
print """
Post feed entries in a Twitter account. It uses a cache file to avoid
sending old news.
Options:
-h, --help Print this usage message.
-V, --version Show version
-v, --verbose Use the verbose mode
--cache-only Initialize the cache with the feed entries
"""
def main():
# Short (one letter) options. Those requiring argument followed by :
short_opts = "hVv"
# Long options (all started by --). Those requiring argument followed by =
long_opts = ["help","version","cache-only","verbose"]
#modes
post = 1
verbose = 0
try:
opts, args = getopt.getopt (sys.argv[2:], short_opts, long_opts)
cfg_file = sys.argv[1]
except getopt.GetoptError, e:
print e
sys.exit(1)
except IndexError:
if len(sys.argv) < 2 :
print "\nMissing parameters!"
usage()
sys.exit(1)
else:
print "\nUnknown error"
sys.exit(1)
config = ConfigParser.ConfigParser()
ioerror = 0
try:
config.readfp(open(cfg_file))
except IOError:
try:
opts, args = getopt.getopt (sys.argv[1:], short_opts, long_opts)
except getopt.GetoptError:
print "\nOption not recognized"
usage()
sys.exit(1)
# it isn't a file but could be an option
ioerror = 1
isopt = 0
for opt, value in opts:
if opt in ("-h", "--help", "-help"):
usage()
sys.exit(0)
elif opt in ("-V", "--version"):
print version
sys.exit(0)
elif opt in ("-v", "--verbose"):
verbose = 1
isopt = 1
elif opt in ("--cache-only"):
isopt = 1
post = 0
# if it isn't a file and it isn't an option exit
if ioerror and not isopt:
print "\nIncorrect parameters"
usage()
sys.exit(1)
ck = config.get('Configuration', 'consumer_key', 0)
cs = config.get('Configuration', 'consumer_secret', 0)
atk = config.get('Configuration', 'access_token_key', 0)
ats = config.get('Configuration', 'access_token_secret', 0)
feed_url = config.get('Configuration', 'feed_url', 0)
try:
include_feed_title = config.get('Configuration', 'include_feed_title', 0)
except ConfigParser.NoOptionError:
include_feed_title="False"
try:
include_title = config.get('Configuration', 'include_title', 0)
except ConfigParser.NoOptionError:
include_title = None
pass
fp = FeedParser(feed_url, FEED_NS, ck+".cache", verbose)
data = fp.get_content()
if include_feed_title == 'True':
feed_title = fp.get_title()
s = Sender(ck, cs, atk, ats, data, verbose, post, feed_title)
elif include_title:
s = Sender(ck, cs, atk, ats, data, verbose, post, include_title)
else:
s = Sender(ck, cs, atk, ats, data, verbose, post)
sent_messages = s.send()
fp.add_cache(sent_messages)
if __name__ == "__main__":
main()