-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
196 lines (164 loc) · 6.95 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
# -*- coding: utf-8 -*-
from datetime import datetime
from peewee import *
import telebot
import requests
import threading
import time
bot = telebot.TeleBot('')
instagram_token = ''
db = SqliteDatabase('bot.db', fields={'id': 'INTEGER AUTOINCREMENT'})
DEFAULT_RADIUS = 1000
PHOTOS_PER_MESSAGE = 10
SUBSCRIBE_CHECK = 216000
class Response:
def __init__(self, message, user, link, timestamp, description, location=None):
self.message = message
self.user = user
self.link = link
self.timestamp = timestamp
self.description = description
self.location = location
class Sub(Model):
id = IntegerField(primary_key=True)
user_id = IntegerField()
lat = CharField()
long = CharField()
radius = CharField()
last_ig = IntegerField(default=int(datetime.today().timestamp()))
last_vk = IntegerField(default=int(datetime.today().timestamp()))
class Meta:
database = db
db.connect()
if not Sub.table_exists():
db.create_tables([Sub])
def geo(latitude, longitude, radius=DEFAULT_RADIUS):
global instagram_token
url_instagram = 'https://api.instagram.com/v1/media/search?lat={}&lng={}&distance={}&access_token={}'\
.format(latitude, longitude, radius, instagram_token)
url_vk = 'https://api.vk.com/method/photos.search?lat={}&long={}&sort=0&radius={}'\
.format(latitude, longitude, radius)
result_instagram = requests.get(url_instagram).json()['data']
result_vk = requests.get(url_vk).json()['response'][1:]
instagram = []
for ig in result_instagram:
username = ig['user']['username']
link = ig['link']
date = datetime.fromtimestamp(int(ig['created_time']))
timestamp = int(ig['created_time'])
description = ig['caption']['text'] if ig['caption'] else 'No description'
location = ig['location']['name'] if ig['location']['name'] else 'Unknown place'
message = "Username: {}\nLink: {}\nLocation: {}\nDate: {}\nDescription: {}\n\n\n" \
.format(username, link, location, date, description)
instagram_response = Response(message, username, link, timestamp, description, location)
instagram.append(instagram_response)
vk = []
for vkcom in result_vk:
username = vkcom['owner_id']
link = 'https://vk.com/photo{}_{}'.format(vkcom['owner_id'], vkcom['pid'])
img = vkcom['src_big']
date = datetime.fromtimestamp(int(vkcom['created']))
timestamp = int(vkcom['created'])
description = vkcom['text'] if vkcom['text'] else 'No description'
message = "ID: id{}\nLink: {}\nImage: {}\nDate: {}\nDescription: {}\n\n\n" \
.format(username, link, img, date, description)
vk_response = Response(message, username, link, timestamp, description)
vk.append(vk_response)
return instagram, vk
@bot.message_handler(commands=['start'])
def start(message):
bot.reply_to(message, 'Please send me your location or use command /location lat long radius (in meters)')
@bot.message_handler(commands=['sub'])
def sub(message):
args = message.text
if ',' in args:
args = args.replace(',', ' ')
args = args.split()[1:]
if len(args) < 2:
bot.reply_to(message, 'You must send longitude and latitude. And maybe radius (in meters)')
else:
latitude = args[0]
longitude = args[1]
if len(args) > 3:
radius = args[2]
else:
radius = DEFAULT_RADIUS
Sub(user_id=message.chat.id, lat=latitude, long=longitude, radius=radius).save()
bot.reply_to(message, 'Got it!')
@bot.message_handler(commands=['sublist'])
def sublist(message):
subs = Sub.select().where(Sub.user_id == message.chat.id)
response = ''
visible_id = 1
for sub in subs:
response += "{} lat: {} long: {} radius: {} last update instagram: {} last update vk: {}\n"\
.format(visible_id, sub.lat, sub.long, sub.radius, datetime.fromtimestamp(int(sub.last_ig)),
datetime.fromtimestamp(int(sub.last_vk)))
visible_id += 1
bot.reply_to(message, response)
@bot.message_handler(commands=['subremove'])
def subremove(message):
args = message.text
args = args.split()[1:]
if len(args) < 1:
bot.reply_to(message, 'You must specify a subscription number')
else:
try:
sub_num = int(args[0])
except ValueError:
bot.reply_to(message, 'Not a number!')
else:
sub_id = Sub.select().where(Sub.user_id == message.chat.id).offset(sub_num - 1).limit(1)
Sub.delete().where(Sub.id == sub_id).execute()
bot.reply_to(message, 'Done!')
@bot.message_handler(commands=['location'])
def location(message):
args = message.text
if ',' in args:
args = args.replace(',', ' ')
args = args.split()[1:]
if len(args) < 2:
bot.send_message(message.chat.id, 'You must send longitude and latitude. And maybe radius (in meters)')
else:
try:
safe_args = [float(arg) for arg in args]
except ValueError:
bot.send_message(message.chat.id, 'Unfortunately, I can not understand these coordinates')
else:
geo_result = geo(*safe_args)
geo_result = geo_result[0] + geo_result[1]
while geo_result:
response_text = []
response = geo_result[:PHOTOS_PER_MESSAGE]
response_text += [resp.message for resp in response]
response_text = ''.join(response_text)
geo_result = geo_result[PHOTOS_PER_MESSAGE:]
bot.send_message(message.chat.id, response_text, disable_web_page_preview=True)
@bot.message_handler(content_types=['location'])
def location(message):
longitude = message.location.longitude
latitude = message.location.latitude
geo_result = geo(latitude, longitude)
while geo_result:
response_text = []
response = geo_result[:PHOTOS_PER_MESSAGE]
response_text += [resp.message for resp in response]
response_text = ''.join(response_text)
geo_result = geo_result[PHOTOS_PER_MESSAGE:]
bot.send_message(message.chat.id, response_text, disable_web_page_preview=True)
def subscribe_daemon():
while True:
subs = Sub.select()
for sub in subs:
geo_result = geo(sub.lat, sub.long, sub.radius)
for instagram in reversed(geo_result[0]):
if instagram.timestamp > sub.last_ig:
Sub.update(last_ig=instagram.timestamp).where(Sub.id == sub.id).execute()
bot.send_message(sub.user_id, instagram.message)
for vk in reversed(geo_result[1]):
if vk.timestamp > sub.last_vk:
Sub.update(last_vk=vk.timestamp).where(Sub.id == sub.id).execute()
bot.send_message(sub.user_id, vk.message)
time.sleep(SUBSCRIBE_CHECK)
threading.Thread(target=bot.polling).start()
threading.Thread(target=subscribe_daemon).start()