diff options
-rwxr-xr-x | async_bot.py | 263 | ||||
-rwxr-xr-x | bot.py | 294 | ||||
-rw-r--r-- | searchbot.py | 41 |
3 files changed, 113 insertions, 485 deletions
diff --git a/async_bot.py b/async_bot.py deleted file mode 100755 index 5c60503..0000000 --- a/async_bot.py +++ /dev/null | |||
@@ -1,263 +0,0 @@ | |||
1 | # -*- coding: utf-8 -*- | ||
2 | |||
3 | import os | ||
4 | import re | ||
5 | import time | ||
6 | import json | ||
7 | import logging | ||
8 | import inspect | ||
9 | import urllib | ||
10 | import sys | ||
11 | from selenium import webdriver | ||
12 | |||
13 | import telegram | ||
14 | from pymongo import MongoClient | ||
15 | |||
16 | import ingrex | ||
17 | |||
18 | import aiohttp | ||
19 | import asyncio | ||
20 | import async_timeout | ||
21 | |||
22 | bot = None | ||
23 | BOT_TOKEN = '' | ||
24 | CHANNEL_NAME = '' | ||
25 | Email = '' | ||
26 | Passwd = '' | ||
27 | PhantomJSPath = '' | ||
28 | DBName = '' | ||
29 | DBUser = '' | ||
30 | DBPass = '' | ||
31 | DBHost = '' | ||
32 | BlockList = '' | ||
33 | LOG_FILENAME = 'voh.log' | ||
34 | TIME_ZONE='Asia/Shanghai' | ||
35 | minLngE6 = 0 | ||
36 | minLatE6 = 0 | ||
37 | maxLngE6 = 0 | ||
38 | maxLatE6 = 0 | ||
39 | |||
40 | |||
41 | class CookieException(Exception): | ||
42 | """Intel Error""" | ||
43 | pass | ||
44 | |||
45 | |||
46 | def get_time(): | ||
47 | return time.strftime('%x %X %Z') | ||
48 | |||
49 | |||
50 | def read_config(): | ||
51 | global Email | ||
52 | global Passwd | ||
53 | global BOT_TOKEN | ||
54 | global CHANNEL_NAME | ||
55 | global PhantomJSPath | ||
56 | global DBName | ||
57 | global DBUser | ||
58 | global DBPass | ||
59 | global DBHost | ||
60 | global BlockList | ||
61 | global LOG_FILENAME | ||
62 | global minLngE6 | ||
63 | global minLatE6 | ||
64 | global maxLngE6 | ||
65 | global maxLatE6 | ||
66 | |||
67 | configfile = open("./config.json") | ||
68 | config = json.load(configfile) | ||
69 | Email = config["Email"] | ||
70 | Passwd = config["Passwd"] | ||
71 | BOT_TOKEN = config["BOT_TOKEN"] | ||
72 | CHANNEL_NAME = config["CHANNEL_NAME"] | ||
73 | PhantomJSPath = config["PhantomJSPath"] | ||
74 | DBName = config["DBName"] | ||
75 | DBUser = config["DBUser"] | ||
76 | DBPass = config["DBPass"] | ||
77 | DBHost = config["DBHost"] | ||
78 | BlockList = config["BlockList"] | ||
79 | minLngE6 = config["minLngE6"] | ||
80 | minLatE6 = config["minLatE6"] | ||
81 | maxLngE6 = config["maxLngE6"] | ||
82 | maxLatE6 = config["maxLatE6"] | ||
83 | |||
84 | os.environ['TZ'] = TIME_ZONE | ||
85 | time.tzset() | ||
86 | |||
87 | logging.basicConfig(level=logging.DEBUG, | ||
88 | filename=LOG_FILENAME, | ||
89 | filemode='w') | ||
90 | console = logging.StreamHandler() | ||
91 | console.setLevel(logging.INFO) | ||
92 | formatter = logging.Formatter('%(name)-8s: %(levelname)-4s %(message)s') | ||
93 | console.setFormatter(formatter) | ||
94 | logging.getLogger('').addHandler(console) | ||
95 | |||
96 | |||
97 | def fetch_cookie(): | ||
98 | logger = logging.getLogger('fetch_cookie') | ||
99 | logger.info(get_time() + ': Fetching Cookie...') | ||
100 | |||
101 | driver = webdriver.PhantomJS(PhantomJSPath) | ||
102 | driver.get('https://www.ingress.com/intel') | ||
103 | |||
104 | # get login page | ||
105 | link = driver.find_elements_by_tag_name('a')[0].get_attribute('href') | ||
106 | driver.get(link) | ||
107 | driver.get_screenshot_as_file('1.png') | ||
108 | |||
109 | # simulate manual login | ||
110 | driver.set_page_load_timeout(10) | ||
111 | driver.set_script_timeout(20) | ||
112 | driver.find_element_by_id('Email').send_keys(Email) | ||
113 | driver.get_screenshot_as_file('2.png') | ||
114 | driver.find_element_by_css_selector('#next').click() | ||
115 | time.sleep(3) | ||
116 | driver.find_element_by_id('Passwd').send_keys(Passwd) | ||
117 | driver.get_screenshot_as_file('3.png') | ||
118 | driver.find_element_by_css_selector('#signIn').click() | ||
119 | time.sleep(3) | ||
120 | driver.get_screenshot_as_file('4.png') | ||
121 | |||
122 | # get cookies | ||
123 | cookies = driver.get_cookies() | ||
124 | |||
125 | csrftoken = '' | ||
126 | SACSID = '' | ||
127 | for key in cookies: | ||
128 | if key['name'] == 'csrftoken': | ||
129 | csrftoken = key['value'] | ||
130 | if key['name'] == 'SACSID': | ||
131 | SACSID = key['value'] | ||
132 | |||
133 | if csrftoken == '' or SACSID == '': | ||
134 | raise CookieException | ||
135 | |||
136 | with open('cookie', 'w') as file: | ||
137 | cookie = 'SACSID='+SACSID+'; csrftoken='+csrftoken+'; ingress.intelmap.shflt=viz; ingress.intelmap.lat=29.098418372855484; ingress.intelmap.lng=119.81689453125; ingress.intelmap.zoom=17' | ||
138 | file.write(cookie) | ||
139 | |||
140 | logger.info(get_time() + ': Fetching Cookie Succeed') | ||
141 | driver.quit() | ||
142 | return True | ||
143 | |||
144 | |||
145 | def send_message(bot, message, monitor=False): | ||
146 | logger = logging.getLogger('send_message') | ||
147 | while True: | ||
148 | try: | ||
149 | if monitor is True: | ||
150 | bot.sendMessage(chat_id="@voamonitor", text=message) | ||
151 | else: | ||
152 | print(type(message)) | ||
153 | bot.sendMessage(chat_id=CHANNEL_NAME, text=message) | ||
154 | logger.info(get_time() + ": sendMsg " + message) | ||
155 | break | ||
156 | except telegram.TelegramError: | ||
157 | logger.error(get_time() + ": Send Message to Channel Failed") | ||
158 | time.sleep(1) | ||
159 | except Exception: | ||
160 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno)) | ||
161 | time.sleep(1) | ||
162 | |||
163 | |||
164 | def find_message_record(id): | ||
165 | uri = 'mongodb://' + DBHost | ||
166 | conn = MongoClient(uri) | ||
167 | conn.api.authenticate(DBUser, DBPass, DBName) | ||
168 | database = conn[DBName] | ||
169 | collection = database.entries | ||
170 | count = collection.find({"id": id}).count() | ||
171 | conn.close() | ||
172 | if count == 0: | ||
173 | return False | ||
174 | else: | ||
175 | return True | ||
176 | |||
177 | |||
178 | def insert_message_to_database(time, id, msg): | ||
179 | uri = 'mongodb://' + DBHost | ||
180 | conn = MongoClient(uri) | ||
181 | conn.api.authenticate(DBUser, DBPass, DBName) | ||
182 | database = conn[DBName] | ||
183 | collection = database.entries | ||
184 | post = {"id": id, "time": time, "msg": msg} | ||
185 | collection.insert(post) | ||
186 | conn.close() | ||
187 | |||
188 | |||
189 | def main(): | ||
190 | logger = logging.getLogger(__name__) | ||
191 | |||
192 | # Lat & Lng of fetch region | ||
193 | field = { | ||
194 | 'minLngE6': minLngE6, | ||
195 | 'minLatE6': minLatE6, | ||
196 | 'maxLngE6': maxLngE6, | ||
197 | 'maxLatE6': maxLatE6, | ||
198 | } | ||
199 | |||
200 | mints = -1 | ||
201 | maxts = -1 | ||
202 | reverse = False | ||
203 | tab = 'all' | ||
204 | |||
205 | # fetch cookie | ||
206 | while True: | ||
207 | try: | ||
208 | if fetch_cookie(): | ||
209 | break | ||
210 | except CookieException: | ||
211 | logger.error(get_time() + ': Fetch Cookie Failed') | ||
212 | time.sleep(3) | ||
213 | except: | ||
214 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno)) | ||
215 | time.sleep(3) | ||
216 | |||
217 | |||
218 | # fetch message | ||
219 | count = 0 | ||
220 | while True: | ||
221 | count += 1 | ||
222 | logger.info(get_time() + ": {} Fetching from Intel...".format(str(count))) | ||
223 | |||
224 | with open('cookie') as cookies: | ||
225 | cookies = cookies.read().strip() | ||
226 | |||
227 | # fetch message per time | ||
228 | while True: | ||
229 | try: | ||
230 | intel = ingrex.Intel(cookies, field) | ||
231 | result = intel.fetch_msg(mints, maxts, reverse, tab) | ||
232 | if result: | ||
233 | mints = result[0][1] + 1 | ||
234 | break | ||
235 | except: | ||
236 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno)) | ||
237 | time.sleep(3) | ||
238 | |||
239 | for item in result[::-1]: | ||
240 | # Check spam message | ||
241 | pattern = re.compile(BlockList) | ||
242 | match = pattern.search(str(item)) | ||
243 | if match: | ||
244 | continue | ||
245 | |||
246 | message = ingrex.Message(item) | ||
247 | if message.ptype == 'PLAYER_GENERATED': | ||
248 | if find_message_record(message.guid) is False: | ||
249 | insert_message_to_database(message.time, message.guid, message.msg) | ||
250 | send_message(bot, message.msg, False) | ||
251 | |||
252 | time.sleep(10) | ||
253 | |||
254 | if __name__ == '__main__': | ||
255 | read_config() | ||
256 | bot = telegram.Bot(BOT_TOKEN) | ||
257 | |||
258 | while True: | ||
259 | try: | ||
260 | main() | ||
261 | except Exception: | ||
262 | send_message(bot, 'Main Unexpected error' + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno), True) | ||
263 | time.sleep(3) \ No newline at end of file | ||
@@ -2,19 +2,17 @@ | |||
2 | 2 | ||
3 | import os | 3 | import os |
4 | import re | 4 | import re |
5 | import sys | ||
5 | import time | 6 | import time |
6 | import json | 7 | import json |
8 | import ingrex | ||
7 | import logging | 9 | import logging |
8 | import urllib | 10 | import inspect |
9 | from selenium import webdriver | ||
10 | |||
11 | import telegram | 11 | import telegram |
12 | from telegram.error import NetworkError | ||
13 | from pymongo import MongoClient | ||
14 | 12 | ||
15 | import ingrex | 13 | from selenium import webdriver |
14 | from pymongo import MongoClient | ||
16 | 15 | ||
17 | Debug = True | ||
18 | bot = None | 16 | bot = None |
19 | BOT_TOKEN = '' | 17 | BOT_TOKEN = '' |
20 | CHANNEL_NAME = '' | 18 | CHANNEL_NAME = '' |
@@ -26,30 +24,24 @@ DBUser = '' | |||
26 | DBPass = '' | 24 | DBPass = '' |
27 | DBHost = '' | 25 | DBHost = '' |
28 | BlockList = '' | 26 | BlockList = '' |
29 | |||
30 | LOG_FILENAME = 'voh.log' | 27 | LOG_FILENAME = 'voh.log' |
31 | logging.basicConfig(level=logging.DEBUG, | 28 | TIME_ZONE='Asia/Shanghai' |
32 | filename=LOG_FILENAME, | 29 | minLngE6 = 0 |
33 | filemode='w') | 30 | minLatE6 = 0 |
34 | console = logging.StreamHandler() | 31 | maxLngE6 = 0 |
35 | console.setLevel(logging.INFO) | 32 | maxLatE6 = 0 |
36 | # set a format which is simpler for console use | ||
37 | formatter = logging.Formatter('%(name)-8s: %(levelname)-4s %(message)s') | ||
38 | # tell the handler to use this format | ||
39 | console.setFormatter(formatter) | ||
40 | # add the handler to the root logger | ||
41 | logging.getLogger('').addHandler(console) | ||
42 | 33 | ||
43 | 34 | ||
44 | class CookieException(Exception): | 35 | class CookieException(Exception): |
45 | ''' CookieError ''' | 36 | """Intel Error""" |
37 | pass | ||
46 | 38 | ||
47 | 39 | ||
48 | def getTime(): | 40 | def get_time(): |
49 | return time.strftime('%x %X %Z') | 41 | return time.strftime('%x %X %Z') |
50 | 42 | ||
51 | 43 | ||
52 | def readConfig(): | 44 | def read_config(): |
53 | global Email | 45 | global Email |
54 | global Passwd | 46 | global Passwd |
55 | global BOT_TOKEN | 47 | global BOT_TOKEN |
@@ -60,6 +52,11 @@ def readConfig(): | |||
60 | global DBPass | 52 | global DBPass |
61 | global DBHost | 53 | global DBHost |
62 | global BlockList | 54 | global BlockList |
55 | global LOG_FILENAME | ||
56 | global minLngE6 | ||
57 | global minLatE6 | ||
58 | global maxLngE6 | ||
59 | global maxLatE6 | ||
63 | 60 | ||
64 | configfile = open("./config.json") | 61 | configfile = open("./config.json") |
65 | config = json.load(configfile) | 62 | config = json.load(configfile) |
@@ -67,259 +64,194 @@ def readConfig(): | |||
67 | Passwd = config["Passwd"] | 64 | Passwd = config["Passwd"] |
68 | BOT_TOKEN = config["BOT_TOKEN"] | 65 | BOT_TOKEN = config["BOT_TOKEN"] |
69 | CHANNEL_NAME = config["CHANNEL_NAME"] | 66 | CHANNEL_NAME = config["CHANNEL_NAME"] |
70 | PhantomjsPath = config["PhantomjsPath"] | 67 | PhantomJSPath = config["PhantomJSPath"] |
71 | DBName = config["DBName"] | 68 | DBName = config["DBName"] |
72 | DBUser = config["DBUser"] | 69 | DBUser = config["DBUser"] |
73 | DBPass = config["DBPass"] | 70 | DBPass = config["DBPass"] |
74 | DBHost = config["DBHost"] | 71 | DBHost = config["DBHost"] |
75 | BlockList = config["BlockList"] | 72 | BlockList = config["BlockList"] |
73 | minLngE6 = config["minLngE6"] | ||
74 | minLatE6 = config["minLatE6"] | ||
75 | maxLngE6 = config["maxLngE6"] | ||
76 | maxLatE6 = config["maxLatE6"] | ||
76 | 77 | ||
77 | os.environ['TZ'] = 'Asia/Shanghai' | 78 | os.environ['TZ'] = TIME_ZONE |
78 | time.tzset() | 79 | time.tzset() |
79 | 80 | ||
81 | logging.basicConfig(level=logging.DEBUG, | ||
82 | filename=LOG_FILENAME, | ||
83 | filemode='w') | ||
84 | console = logging.StreamHandler() | ||
85 | console.setLevel(logging.INFO) | ||
86 | formatter = logging.Formatter('%(name)-8s: %(levelname)-4s %(message)s') | ||
87 | console.setFormatter(formatter) | ||
88 | logging.getLogger('').addHandler(console) | ||
80 | 89 | ||
81 | def fetchCookie(): | ||
82 | global Debug | ||
83 | global Email | ||
84 | global Passwd | ||
85 | global PhantomJSPath | ||
86 | 90 | ||
87 | logger = logging.getLogger('fetchCookie') | 91 | def fetch_cookie(): |
88 | logger.info(getTime() + ': Fetching Cookie...') | 92 | logger = logging.getLogger('fetch_cookie') |
93 | logger.info(get_time() + ': Fetching Cookie...') | ||
89 | 94 | ||
90 | driver = webdriver.PhantomJS(PhantomjsPath) | 95 | driver = webdriver.PhantomJS(PhantomJSPath) |
91 | driver.get('https://www.ingress.com/intel') | 96 | driver.get('https://www.ingress.com/intel') |
92 | 97 | ||
93 | # get login page | 98 | # get login page |
94 | link = driver.find_elements_by_tag_name('a')[0].get_attribute('href') | 99 | link = driver.find_elements_by_tag_name('a')[0].get_attribute('href') |
95 | driver.get(link) | 100 | driver.get(link) |
96 | if Debug: | 101 | driver.get_screenshot_as_file('1.png') |
97 | driver.get_screenshot_as_file('1.png') | 102 | |
98 | # simulate manual login | 103 | # simulate manual login |
99 | driver.set_page_load_timeout(10) | 104 | driver.set_page_load_timeout(10) |
100 | driver.set_script_timeout(20) | 105 | driver.set_script_timeout(20) |
101 | driver.find_element_by_id('Email').send_keys(Email) | 106 | driver.find_element_by_id('Email').send_keys(Email) |
102 | if Debug: | 107 | driver.get_screenshot_as_file('2.png') |
103 | driver.get_screenshot_as_file('2.png') | ||
104 | driver.find_element_by_css_selector('#next').click() | 108 | driver.find_element_by_css_selector('#next').click() |
105 | time.sleep(3) | 109 | time.sleep(3) |
106 | driver.find_element_by_id('Passwd').send_keys(Passwd) | 110 | driver.find_element_by_id('Passwd').send_keys(Passwd) |
107 | if Debug: | 111 | driver.get_screenshot_as_file('3.png') |
108 | driver.get_screenshot_as_file('3.png') | ||
109 | driver.find_element_by_css_selector('#signIn').click() | 112 | driver.find_element_by_css_selector('#signIn').click() |
110 | time.sleep(3) | 113 | time.sleep(3) |
111 | if Debug: | 114 | driver.get_screenshot_as_file('4.png') |
112 | driver.get_screenshot_as_file('3.png') | 115 | |
113 | # get cookies | 116 | # get cookies |
114 | temp = driver.get_cookies() | 117 | cookies = driver.get_cookies() |
115 | 118 | ||
116 | csrftoken = '' | 119 | csrftoken = '' |
117 | SACSID = '' | 120 | SACSID = '' |
118 | for a in temp: | 121 | for key in cookies: |
119 | if (a['name'] == 'csrftoken'): | 122 | if key['name'] == 'csrftoken': |
120 | csrftoken = a['value'] | 123 | csrftoken = key['value'] |
121 | if (a['name'] == 'SACSID'): | 124 | if key['name'] == 'SACSID': |
122 | SACSID = a['value'] | 125 | SACSID = key['value'] |
123 | 126 | ||
124 | if csrftoken == '' or SACSID == '': | 127 | if csrftoken == '' or SACSID == '': |
125 | logger.error(getTime() + ': Fetch Cookie Failed') | ||
126 | raise CookieException | 128 | raise CookieException |
127 | 129 | ||
128 | with open('cookie', 'w') as file: | 130 | with open('cookie', 'w') as file: |
129 | cookie = 'SACSID='+SACSID+'; csrftoken='+csrftoken+'; ingress.intelmap.shflt=viz; ingress.intelmap.lat=29.098418372855484; ingress.intelmap.lng=119.81689453125; ingress.intelmap.zoom=17' | 131 | cookie = 'SACSID='+SACSID+'; csrftoken='+csrftoken+'; ingress.intelmap.shflt=viz; ingress.intelmap.lat=29.098418372855484; ingress.intelmap.lng=119.81689453125; ingress.intelmap.zoom=17' |
130 | file.write(cookie) | 132 | file.write(cookie) |
131 | 133 | ||
134 | logger.info(get_time() + ': Fetching Cookie Succeed') | ||
132 | driver.quit() | 135 | driver.quit() |
133 | logger.info(getTime() + ': Fetching Cookie Succeed') | ||
134 | return True | 136 | return True |
135 | 137 | ||
136 | 138 | ||
137 | def sendMessge(bot, msg): | 139 | def send_message(bot, message, monitor=False): |
138 | "sendMsg" | 140 | logger = logging.getLogger('send_message') |
139 | |||
140 | logger = logging.getLogger('sendMessage') | ||
141 | while True: | 141 | while True: |
142 | try: | 142 | try: |
143 | url = 'https://api.telegram.org/bot' | 143 | if monitor is True: |
144 | url += BOT_TOKEN | 144 | bot.sendMessage(chat_id="@voamonitor", text=message) |
145 | url += '/sendMessage?chat_id=' | 145 | else: |
146 | url += CHANNEL_NAME | 146 | print(type(message)) |
147 | url += '&text=' | 147 | bot.sendMessage(chat_id=CHANNEL_NAME, text=message) |
148 | url += urllib.parse.quote(msg) | 148 | logger.info(get_time() + ": sendMsg " + message) |
149 | |||
150 | req = urllib.request.Request(url, headers={'Content-Type': 'application/x-www-form-urlencoded'}) | ||
151 | resp = urllib.request.urlopen(req) | ||
152 | data = resp.read() | ||
153 | logger.info(data) | ||
154 | logger.info(getTime() + ": sendMsg " + msg) | ||
155 | break | 149 | break |
156 | except NetworkError: | 150 | except telegram.TelegramError: |
151 | logger.error(get_time() + ": Send Message to Channel Failed") | ||
157 | time.sleep(1) | 152 | time.sleep(1) |
158 | 153 | except Exception: | |
159 | 154 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno)) | |
160 | def sendMonitor(bot, msg): | ||
161 | logger = logging.getLogger('sendMonitor') | ||
162 | while True: | ||
163 | try: | ||
164 | bot.sendMessage(chat_id="@voamonitor", text=msg) | ||
165 | logger.info(getTime() + ": sendMsg " + msg) | ||
166 | break | ||
167 | except NetworkError: | ||
168 | time.sleep(1) | 155 | time.sleep(1) |
169 | 156 | ||
170 | 157 | ||
171 | def formatMessage(raw): | 158 | def find_message_record(id): |
172 | pattern = re.compile(BlockList) | ||
173 | match = pattern.search(str(raw)) | ||
174 | if match: | ||
175 | return "Blocked" | ||
176 | |||
177 | msg = '' | ||
178 | plext = raw[2]['plext'] | ||
179 | markup = plext['markup'] | ||
180 | |||
181 | for mark in markup: | ||
182 | if mark[0] == 'SECURE': | ||
183 | msg += mark[1]['plain'] | ||
184 | elif mark[0] == 'SENDER': | ||
185 | player = mark[1]['plain'] | ||
186 | team = mark[1]['team'] | ||
187 | |||
188 | pattern = re.compile(':') | ||
189 | match = pattern.search(player) | ||
190 | if match: | ||
191 | if team == 'RESISTANCE': | ||
192 | player = player[:match.span()[0]] + ' 🐳' + player[match.span()[0]:] | ||
193 | elif team == 'ENLIGHTENED': | ||
194 | player = player[:match.span()[0]] + ' 🐸' + player[match.span()[0]:] | ||
195 | msg += player | ||
196 | |||
197 | elif mark[0] == 'PLAYER' or mark[0] == 'AT_PLAYER': | ||
198 | player = mark[1]['plain'] | ||
199 | team = mark[1]['team'] | ||
200 | |||
201 | msg += player | ||
202 | if team == 'RESISTANCE': | ||
203 | msg += ' 🐳' | ||
204 | elif team == 'ENLIGHTENED': | ||
205 | msg += ' 🐸' | ||
206 | |||
207 | elif mark[0] == 'TEXT': | ||
208 | msg += mark[1]['plain'] | ||
209 | |||
210 | pattern = re.compile('\[secure\]') | ||
211 | match = pattern.search(msg) | ||
212 | if match: | ||
213 | if msg.find(':') != -1: | ||
214 | msg = msg[:9] + '@' + msg[9:] | ||
215 | else: | ||
216 | msg = msg[:10] + '@' + msg[10:] | ||
217 | else: | ||
218 | msg = '@' + msg | ||
219 | |||
220 | return msg | ||
221 | |||
222 | |||
223 | def FindRecord(id): | ||
224 | uri = 'mongodb://' + DBHost | 159 | uri = 'mongodb://' + DBHost |
225 | Conn = MongoClient(uri) | 160 | conn = MongoClient(uri) |
226 | Conn.api.authenticate(DBUser, DBPass, DBName) | 161 | conn.api.authenticate(DBUser, DBPass, DBName) |
227 | database = Conn[DBName] | 162 | database = conn[DBName] |
228 | mycollection = database.entries | 163 | collection = database.entries |
229 | res = mycollection.find({"id": id}) | 164 | count = collection.find({"id": id}).count() |
230 | if res.count() == 0: | 165 | conn.close() |
166 | if count == 0: | ||
231 | return False | 167 | return False |
232 | else: | 168 | else: |
233 | return True | 169 | return True |
234 | 170 | ||
235 | 171 | ||
236 | def insertDB(time, id, msg): | 172 | def insert_message_to_database(time, id, msg): |
237 | uri = 'mongodb://' + DBHost | 173 | uri = 'mongodb://' + DBHost |
238 | Conn = MongoClient(uri) | 174 | conn = MongoClient(uri) |
239 | Conn.api.authenticate(DBUser, DBPass, DBName) | 175 | conn.api.authenticate(DBUser, DBPass, DBName) |
240 | database = Conn[DBName] | 176 | database = conn[DBName] |
241 | mycollection = database.entries | 177 | collection = database.entries |
242 | post = {"id": id, "time": time, "msg": msg} | 178 | post = {"id": id, "time": time, "msg": msg} |
243 | mycollection.insert(post) | 179 | collection.insert(post) |
244 | Conn.close() | 180 | conn.close() |
245 | 181 | ||
246 | 182 | ||
247 | def main(): | 183 | def main(): |
248 | logger = logging.getLogger('main') | 184 | logger = logging.getLogger(__name__) |
249 | 185 | ||
186 | # Lat & Lng of fetch region | ||
250 | field = { | 187 | field = { |
251 | 'minLngE6': 119618783, | 188 | 'minLngE6': minLngE6, |
252 | 'minLatE6': 29912919, | 189 | 'minLatE6': minLatE6, |
253 | 'maxLngE6': 121018722, | 190 | 'maxLngE6': maxLngE6, |
254 | 'maxLatE6': 30573739, | 191 | 'maxLatE6': maxLatE6, |
255 | } | 192 | } |
256 | 193 | ||
257 | mints = -1 | 194 | mints = -1 |
258 | maxts=-1 | 195 | maxts = -1 |
259 | reverse=False | 196 | reverse = False |
260 | tab='all' | 197 | tab = 'all' |
261 | 198 | ||
199 | # fetch cookie | ||
262 | while True: | 200 | while True: |
263 | try: | 201 | try: |
264 | if fetchCookie(): | 202 | if fetch_cookie(): |
265 | break | 203 | break |
266 | except CookieException: | 204 | except CookieException: |
205 | logger.error(get_time() + ': Fetch Cookie Failed') | ||
206 | time.sleep(3) | ||
207 | except: | ||
208 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno)) | ||
267 | time.sleep(3) | 209 | time.sleep(3) |
268 | 210 | ||
211 | |||
212 | # fetch message | ||
269 | count = 0 | 213 | count = 0 |
270 | while True: | 214 | while True: |
271 | count += 1 | 215 | count += 1 |
216 | logger.info(get_time() + ": {} Fetching from Intel...".format(str(count))) | ||
272 | 217 | ||
273 | with open('cookie') as cookies: | 218 | with open('cookie') as cookies: |
274 | cookies = cookies.read().strip() | 219 | cookies = cookies.read().strip() |
275 | 220 | ||
276 | logger.info(getTime() + ": {} Fetching from Intel...".format(str(count))) | 221 | # fetch message per time |
277 | |||
278 | while True: | 222 | while True: |
279 | try: | 223 | try: |
280 | |||
281 | intel = ingrex.Intel(cookies, field) | 224 | intel = ingrex.Intel(cookies, field) |
282 | result = intel.fetch_msg(mints, maxts, reverse, tab) | 225 | result = intel.fetch_msg(mints, maxts, reverse, tab) |
283 | |||
284 | if result: | 226 | if result: |
285 | mints = result[0][1] + 1 | 227 | mints = result[0][1] + 1 |
286 | break | 228 | break |
287 | 229 | except: | |
288 | except CookieException: | 230 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno)) |
289 | while True: | 231 | time.sleep(3) |
290 | try: | ||
291 | if fetchCookie(): | ||
292 | break | ||
293 | except CookieException: | ||
294 | time.sleep(3) | ||
295 | except Exception: | ||
296 | pass | ||
297 | 232 | ||
298 | for item in result[::-1]: | 233 | for item in result[::-1]: |
299 | message = ingrex.Message(item) | 234 | # Check spam message |
235 | pattern = re.compile(BlockList) | ||
236 | match = pattern.search(str(item)) | ||
237 | if match: | ||
238 | continue | ||
300 | 239 | ||
240 | message = ingrex.Message(item) | ||
301 | if message.ptype == 'PLAYER_GENERATED': | 241 | if message.ptype == 'PLAYER_GENERATED': |
302 | # logger.info(getTime() + str(item)) | 242 | if find_message_record(message.guid) is False: |
303 | 243 | insert_message_to_database(message.time, message.guid, message.msg) | |
304 | msg = formatMessage(item) | 244 | send_message(bot, message.msg, False) |
305 | if msg == 'Blocked': | ||
306 | logger.info(getTime() + " " + message.text) | ||
307 | else: | ||
308 | msg = message.time + " " + msg | ||
309 | # logger.info(getTime() + " " + msg) | ||
310 | if FindRecord(message.guid) is False: | ||
311 | insertDB(message.time, message.guid, msg) | ||
312 | # sendMonitor(bot, msg) | ||
313 | sendMessge(bot, msg) | ||
314 | 245 | ||
315 | time.sleep(10) | 246 | time.sleep(10) |
316 | 247 | ||
317 | if __name__ == '__main__': | 248 | if __name__ == '__main__': |
318 | readConfig() | 249 | read_config() |
319 | bot = telegram.Bot(BOT_TOKEN) | 250 | bot = telegram.Bot(BOT_TOKEN) |
320 | 251 | ||
321 | while True: | 252 | while True: |
322 | try: | 253 | try: |
323 | main() | 254 | main() |
324 | except Exception: | 255 | except Exception as e: |
325 | sendMonitor(bot, 'Main Error') | 256 | send_message(bot, 'Main Unexpected error' + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno), True) |
257 | time.sleep(3) \ No newline at end of file | ||
diff --git a/searchbot.py b/searchbot.py deleted file mode 100644 index ee504d5..0000000 --- a/searchbot.py +++ /dev/null | |||
@@ -1,41 +0,0 @@ | |||
1 | from pymongo import MongoClient | ||
2 | from telegram.ext import Updater, CommandHandler | ||
3 | import logging | ||
4 | # Enable logging | ||
5 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
6 | level=logging.DEBUG) | ||
7 | logger = logging.getLogger(__name__) | ||
8 | |||
9 | |||
10 | def search(bot, update, args): | ||
11 | agentid = args[0] | ||
12 | print(agentid) | ||
13 | conn = MongoClient('Mongo Address') | ||
14 | db = conn['COMM_Hangzhou'] | ||
15 | collection = db.entries | ||
16 | res = collection.find({"$text": {"$search": agentid}}) | ||
17 | for i in res: | ||
18 | print(i) | ||
19 | bot.sendMessage(update.message.chat_id, text=str(i)) | ||
20 | |||
21 | |||
22 | def start(bot, update): | ||
23 | bot.sendMessage(update.message.chat_id, text="Hi! Use /search agentid to start, no need for @") | ||
24 | |||
25 | |||
26 | def error(bot, update, error): | ||
27 | logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
28 | |||
29 | |||
30 | def main(): | ||
31 | updater = Updater('Telegram Bot ID') | ||
32 | dp = updater.dispatcher | ||
33 | dp.add_handler(CommandHandler("start", start)) | ||
34 | dp.add_handler(CommandHandler("help", start)) | ||
35 | dp.add_handler(CommandHandler("search", search, pass_args=True)) | ||
36 | dp.add_error_handler(error) | ||
37 | updater.start_polling() | ||
38 | updater.idle() | ||
39 | |||
40 | if __name__ == "__main__": | ||
41 | main() | ||