Repository navigation
Expand file tree
/
Copy pathmlbot.py
More file actions
135 lines (113 loc) · 3.84 KB
/
Copy pathmlbot.py
File metadata and controls
135 lines (113 loc) · 3.84 KB
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
import praw
import sqlite3
import time
import datetime
import re
import feedparser
SUBREDDIT = "machinelearning"
USERAGENT = "shortscience"
SS_XML = "http://www.shortscience.org/rss-all.xml"
SUBMISSIONLIMIT = 100
COMMENTLIMIT = 100
LINK = re.compile('arxiv\.org/abs/([0-9]+\.[0-9]+)')
WAIT = 30
r = praw.Reddit(USERAGENT)
subreddit = r.subreddit(SUBREDDIT)
sqldb = sqlite3.connect("mlbot.db")
cursor = sqldb.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS replied(id TEXT)')
cursor.execute('CREATE INDEX IF NOT EXISTS replied_index ON replied(id)')
def getDate(submission):
time = submission.created
return datetime.datetime.fromtimestamp(time)
def checkSubmissions(subreddit, feed):
for i in list(subreddit.new(limit=SUBMISSIONLIMIT)):
print (i)
if (containsLink(i.selftext) or containsLink(i.url)) \
and not isReplied(i.id) \
and (checkSS(getLinkIDs(i.selftext), feed) or checkSS(getLinkIDs(i.url), feed)):
replySubmission(i, feed)
def checkComments(subreddit, feed):
for i in list(subreddit.comments(limit=COMMENTLIMIT)):
print (i)
if containsLink(i.body) and not isReplied(i.id) and checkSS(getLinkIDs(i.body), feed):
replyComment(i, feed)
def checkSS(arxivids, feed):
for i in arxivids:
for j in feed.entries:
if "shortscience_arxivid" in j:
if j["shortscience_arxivid"] == i:
return True
return False
def getSSLink(arxivid, feed):
for i in feed.entries:
if "shortscience_arxivid" in i:
if i["shortscience_arxivid"] == arxivid:
return "http://www.shortscience.org/paper?bibtexKey=" + i["shortscience_bibtexkey"]
return None
def containsLink(text):
match = LINK.findall(text)
return len(match) > 0
def getLinkIDs(text):
match = LINK.findall(text)
return match
def getSummary(arxivid, feed):
score = -1
summary = ""
for i in feed.entries:
if "shortscience_arxivid" in i:
if i["shortscience_arxivid"] == arxivid and int(i["shortscience_votes"]) > score:
score = int(i["shortscience_votes"])
summary = i["summary"]
summary = summary.replace("\n", "\n\n")
return summary
def isReplied(id):
cursor.execute('SELECT * FROM replied WHERE ID=?', [id])
if cursor.fetchone():
return True
return False
def makeReplied(id):
cursor.execute('INSERT INTO replied VALUES(?)', [id])
sqldb.commit()
def replyComment(post, feed):
linkids = []
reply = "I am a bot! You linked to a paper that has a summary on ShortScience.org!"
for i in getLinkIDs(post.body):
if i not in linkids and checkSS([i], feed):
linkids.append(i)
for i in linkids:
reply += "\n\n" + getSSLink(i, feed)
reply += "\n\n**Summary Preview:**\n\n" + getSummary(i, feed)
try:
post.reply(reply)
except praw.exceptions.APIException as e:
print (e)
time.sleep(WAIT)
pass
else:
makeReplied(post.id)
def replySubmission(post, feed):
linkids = []
reply = "I am a bot! You linked to a paper that has a summary on ShortScience.org!"
for i in getLinkIDs(post.selftext):
if i not in linkids and checkSS([i], feed):
linkids.append(i)
for i in getLinkIDs(post.url):
if i not in linkids and checkSS([i], feed):
linkids.append(i)
for i in linkids:
reply += "\n\n" + getSSLink(i, feed)
reply += "\n\n**Summary Preview:**\n\n" + getSummary(i, feed)
try:
post.reply(reply)
except praw.exceptions.APIException as e:
print (e)
time.sleep(WAIT)
pass
else:
makeReplied(post.id)
while True:
feed = feedparser.parse(SS_XML)
checkSubmissions(subreddit, feed)
checkComments(subreddit, feed)
time.sleep(WAIT)