-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
246 lines (208 loc) · 6.45 KB
/
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
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
#!/usr/bin/env python
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
from time import time, sleep
import os, sys, types
import urllib2
import json
import string
from bs4 import BeautifulSoup
import requests
from pymongo import MongoClient
from telegram.error import (TelegramError, Unauthorized, BadRequest,
TimedOut, ChatMigrated, NetworkError)
host = 'localhost'
port = 27017
databaseName = 'afshanbot'
t = 60
users = []
usernames = {}
links = []
user_links = {}
link = ''
canStop = False
b = ''
def u1():
global users, usernames, user_links
print 'u1()'
for user in users:
user_id = user['user_id']
username = user['username']
user_link = user['user_link']
usernames[user_id] = username
user_links[user_id] = user_link
def u2():
global users, usernames, user_links
print 'u2()'
for key in usernames.keys():
user = {}
user['user_id'] = key
user['username'] = usernames[key]
if key in user_links.keys():
user['user_link'] = user_links[key]
users.append(user)
def getClient():
global host, port
client = MongoClient(host, port)
return client
def update_dict(id, username):
global usernames
if id not in usernames:
usernames[id] = username
def bs_source(url):
page = urllib2.urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
soup.prettify()
return soup
def read_article(url):
soup = bs_source(url)
ps = soup.findAll('p')
articles = []
for p in ps:
imgs = p.findAll('img')
if len(imgs) > 0:
for img in imgs:
articles.append(img.get('src'))
articles.append(p.text)
print articles
return articles
def check(soup):
global links, link
divs = soup.findAll('div', class_='block-item-big')
for div in divs:
imgs = div.findAll('img')
for img in imgs:
if 'afshan' in img.get('src'):
a = div.find('a')
l = a.get('href')
if l not in links:
link = l
links.append(l)
break
def load():
global users, usernames, links, user_links, link, databaseName
client = getClient()
db = client[databaseName]
userCollection = db['users']
linkCollection = db['links']
cursor = userCollection.find()
for doc in cursor:
users.append(doc)
cursor = linkCollection.find()
for doc in cursor:
link_ = doc['link']
if link_ not in links:
links.append(doc['link'])
link = links[len(links)-1]
client.close()
u1()
def save(bot, update):
save()
def save():
global users, links, databaseName, usernames, user_links
u2()
client = getClient()
db = client[databaseName]
userCollection = db['users']
linkCollection = db['links']
print 'Saving'
print usernames, user_links,
print users, links
for user in users:
userCollection.update({'user_id': user['user_id']}, user, upsert=True)
for link_ in links:
linkCollection.update({'link': link_}, {'link': link_}, upsert=True)
client.close()
def start(bot, update):
global b, usernames, user_links, links
i1 = update.message.chat_id
i2 = update.message.from_user.username
if i2 == 'cagdas':
b = bot
update.message.reply_text('Bot set')
print 'Preload {} {} {}'.format(usernames,user_links,links)
load()
print 'Postload {} {} {}'.format(usernames,user_links,links)
update.message.reply_text('Loaded')
elif i2 == 'abdullahwali':
message = 'Shut the fuck up Wali'
update.message.reply_text(message)
update_dict(i1,i2)
add_user(i1)
message = 'I\'m a Telegram Bot to update you whenever something new Afshan wrote is published\nSay /stop for me to stop.'
update.message.reply_text(message)
def stop(bot, update):
if update.message.from_user.username == 'cagdas':
save()
canStop = True
else:
i1 = update.message.chat_id
remove_user(i1)
update.message.reply_text('Sad to see you go.')
def add_user(id):
global user_links
if id not in user_links:
user_links[id] = []
def remove_user(id):
global user_links
if id in user_links:
del user_links[id]
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def send_messages():
global user_links, links, b, t
for link in links:
if link and b:
articles = read_article(link)
for user in user_links:
if link not in user_links[user]:
t = 60
try:
for i in xrange(len(articles)):
a = articles[i]
if a.encode('ascii', 'ignore') and len(a) > 0:
if 'http' not in a:
b.send_message(chat_id=user, text=a)
else:
b.send_photo(chat_id=user, photo=a)
except BadRequest as inst:
print 'Bad request'
print type(inst)
print inst.args
print inst
pass
except Unauthorized:
if user in usernames:
print '{}, {} blocked'.format(user, usernames[user])
else:
print '{} blocked'.format(user)
user_links[user].append(link)
save()
def main():
global usernames,user_links,links,link,canStop,t
with open('config.json') as config:
c = json.load(config)
token = c['token']
updater = Updater(token)
url = 'http://bilnews.bilkent.edu.tr/category/opinions/'
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("save", save))
dp.add_handler(CommandHandler("stop", stop))
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
while not canStop:
print usernames
print user_links
print links
print link
print canStop
soup = bs_source(url)
check(soup)
send_messages()
sleep(t)
t += 1
updater.idle()
if __name__ == '__main__':
main()