-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter-bot.py
53 lines (48 loc) · 1.82 KB
/
twitter-bot.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
import sqlite3
import feedparser
import tweepy
from auth import (consumer_key, consumer_secret, access_token, access_token_secret)
def getRss(twitterApi):
rssFeed = feedparser.parse("https://www.cadavre.co.uk/index.xml")
if rssFeed:
for item in rssFeed["items"]:
# Links are expected to be 100 characters or less
link = item["link"]
if checkLink(link):
print("Already posted:", link)
else:
blogTitle = item["title"]
twitterLengthTitle = (blogTitle[:160] + '...') if len(blogTitle) > 160 else blogTitle
message = "[NEW BLOG POST] " + twitterLengthTitle + " : " + link
saveLink(link)
print("Posted:", link)
status = twitterApi.update_status(status=message)
else:
print("Nothing found in feed", url)
def checkLink(link):
conn = sqlite3.connect('rssFeed.sqlite')
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS feed (link VARCHAR(100), UNIQUE(link));')
cur.execute("SELECT link FROM feed WHERE link = ?;", (link,) )
result = cur.fetchone()
conn.commit()
conn.close()
if result is not None:
return True
return False
def saveLink(link):
conn = sqlite3.connect('rssFeed.sqlite')
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute("INSERT OR IGNORE INTO feed (link) values (?);", (link,))
conn.commit()
conn.close()
def getTwitter():
authenticationToken = tweepy.OAuthHandler(consumer_key,consumer_secret)
authenticationToken.set_access_token(access_token,access_token_secret)
twitter = tweepy.API(authenticationToken)
return twitter
if __name__ == '__main__':
twitterApi = getTwitter();
getRss(twitterApi);