forked from EvanDonato/mlbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlbot.py
executable file
·144 lines (124 loc) · 4.24 KB
/
mlbot.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
import praw
import sqlite3
import time
import datetime
import re
import feedparser
SUBREDDIT = "machinelearning"
#SUBREDDIT = "shortscience_test"
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 = 100
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, end='')
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)):
print ("!!REPLY!!", end='')
replySubmission(i, feed)
print (', ', end='')
print ("done checking Subissions")
def checkComments(subreddit, feed):
for i in list(subreddit.comments(limit=COMMENTLIMIT)):
print (i, end='')
if containsLink(i.body) and not isReplied(i.id) and checkSS(getLinkIDs(i.body), feed):
print ('!!Reply!!', end='')
replyComment(i, feed)
print (', ', end='')
print ("done checking Comments")
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 "[[view more]](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
title = ""
author = ""
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"])
title = i["title"]
author = i["author"]
summary = i["summary"]
summary = summary.replace("\n", "\n\n")
summary = "\n\n**" + title +"** \n\n*Summary by " + author + "*\n\n" + summary
return summary
def isReplied(id):
cursor.execute('SELECT * FROM replied WHERE ID=?', [id])
if cursor.fetchone():
print("\nAlready replied!!!!!!!")
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 += getSummary(i, feed) + " " + getSSLink(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 += getSummary(i, feed) + " " + getSSLink(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)