forked from tsaylor/twitter-link-gettr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkfeed.py
52 lines (40 loc) · 2.16 KB
/
mkfeed.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
import twitter, re, datetime, PyRSS2Gen, urllib2, settings, sys
url = unicode(r"(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?")
def getURLs(text):
#url=unicode(r"((http|ftp)://)?(((([\d]+\.)+){3}[\d]+(/[\w./]+)?)|([a-z]\w*((\.\w+)+){2,})([/][\w.~]*)*)")
return [a.group() for a in re.finditer(url,text)]
def hasURLs(text):
return ( re.search(url, text) != None )
def convertLinks(text):
links = [a.group() for a in re.finditer(url,text)]
#swap links found in above for actual links
for link in links:
text = re.sub(link, "<a href='%s' target='_blank'>%s</a>" % (link,link), text)
return text
def mkFeedItem(status):
return PyRSS2Gen.RSSItem(
title = status.user.name,
link = status.user.url,
description = convertLinks(status.text),
# guid = PyRSS2Gen.Guid(status.text),
pubDate = status.created_at #datetime.datetime(2003, 9, 6, 21, 31) # get the right date/time from python-twitter
)
def mkFeed(statuses):
rss = PyRSS2Gen.RSS2(
title = "Twitter Links",
link = "www.twitter.com",
description = "Your friends' stupid links",
items = [mkFeedItem(status) for status in statuses],
lastBuildDate = datetime.datetime.now() )
rss.write_xml(open(settings.FILE, "w"))
try:
api = twitter.Api(username=settings.USERNAME, password=settings.PASSWORD)
allstatuses = api.GetFriendsTimeline(settings.USERNAME)
except urllib2.HTTPError:
sys.exit()
# get one list of URLs, even if there are multiple URLs in some posts
urlstatuses = []
for status in allstatuses:
if hasURLs(status.text):
urlstatuses.append (status)
mkFeed(urlstatuses)