diff options
author | Jinwei Zhao <[email protected]> | 2017-02-01 21:34:30 +0800 |
---|---|---|
committer | Jinwei Zhao <[email protected]> | 2017-02-01 21:41:54 +0800 |
commit | 4d2a94988cf0e51b51bfc4d6f5e73aa6351402c6 (patch) | |
tree | 5b9d41e143144effe0d0ffe3bdd89e9de82fcdeb /async_bot.py | |
parent | 9e8f476d124ae81b0434e17cd6fdb13d981e06ae (diff) | |
download | COMM2TG-4d2a94988cf0e51b51bfc4d6f5e73aa6351402c6.tar.gz |
refactor
Diffstat (limited to 'async_bot.py')
-rwxr-xr-x | async_bot.py | 263 |
1 files changed, 0 insertions, 263 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 | ||