diff options
author | Jinwei Zhao <[email protected]> | 2017-02-01 21:43:41 +0800 |
---|---|---|
committer | Jinwei Zhao <[email protected]> | 2017-02-01 21:43:41 +0800 |
commit | c71bb7495240a5c5ae306ea4e734ec3b2cc9edd6 (patch) | |
tree | 5b9d41e143144effe0d0ffe3bdd89e9de82fcdeb | |
parent | e72690105ff9293d5a0a6db38647c292afdb618e (diff) | |
download | COMM2TG-c71bb7495240a5c5ae306ea4e734ec3b2cc9edd6.tar.gz |
delete
-rwxr-xr-x | async_bot.py | 269 |
1 files changed, 0 insertions, 269 deletions
diff --git a/async_bot.py b/async_bot.py deleted file mode 100755 index 8e04359..0000000 --- a/async_bot.py +++ /dev/null | |||
@@ -1,269 +0,0 @@ | |||
1 | # -*- coding: utf-8 -*- | ||
2 | |||
3 | import os | ||
4 | import re | ||
5 | import time | ||
6 | import json | ||
7 | import logging | ||
8 | import urllib | ||
9 | import sys | ||
10 | from selenium import webdriver | ||
11 | |||
12 | import telegram | ||
13 | from pymongo import MongoClient | ||
14 | |||
15 | import ingrex | ||
16 | |||
17 | import aiohttp | ||
18 | import asyncio | ||
19 | import async_timeout | ||
20 | |||
21 | bot = None | ||
22 | BOT_TOKEN = '' | ||
23 | CHANNEL_NAME = '' | ||
24 | Email = '' | ||
25 | Passwd = '' | ||
26 | PhantomJSPath = '' | ||
27 | DBName = '' | ||
28 | DBUser = '' | ||
29 | DBPass = '' | ||
30 | DBHost = '' | ||
31 | BlockList = '' | ||
32 | LOG_FILENAME = 'voh.log' | ||
33 | TIME_ZONE='Asia/Shanghai' | ||
34 | minLngE6 = 0 | ||
35 | minLatE6 = 0 | ||
36 | maxLngE6 = 0 | ||
37 | maxLatE6 = 0 | ||
38 | |||
39 | class CookieException(Exception): | ||
40 | pass | ||
41 | |||
42 | |||
43 | def get_time(): | ||
44 | return time.strftime('%x %X %Z') | ||
45 | |||
46 | |||
47 | def read_config(): | ||
48 | global Email | ||
49 | global Passwd | ||
50 | global BOT_TOKEN | ||
51 | global CHANNEL_NAME | ||
52 | global PhantomJSPath | ||
53 | global DBName | ||
54 | global DBUser | ||
55 | global DBPass | ||
56 | global DBHost | ||
57 | global BlockList | ||
58 | global LOG_FILENAME | ||
59 | global minLngE6 | ||
60 | global minLatE6 | ||
61 | global maxLngE6 | ||
62 | global maxLatE6 | ||
63 | |||
64 | configfile = open("./config.json") | ||
65 | config = json.load(configfile) | ||
66 | Email = config["Email"] | ||
67 | Passwd = config["Passwd"] | ||
68 | BOT_TOKEN = config["BOT_TOKEN"] | ||
69 | CHANNEL_NAME = config["CHANNEL_NAME"] | ||
70 | PhantomJSPath = config["PhantomJSPath"] | ||
71 | DBName = config["DBName"] | ||
72 | DBUser = config["DBUser"] | ||
73 | DBPass = config["DBPass"] | ||
74 | DBHost = config["DBHost"] | ||
75 | BlockList = config["BlockList"] | ||
76 | minLngE6 = config["minLngE6"] | ||
77 | minLatE6 = config["minLatE6"] | ||
78 | maxLngE6 = config["maxLngE6"] | ||
79 | maxLatE6 = config["maxLatE6"] | ||
80 | |||
81 | os.environ['TZ'] = TIME_ZONE | ||
82 | time.tzset() | ||
83 | |||
84 | logging.basicConfig(level=logging.DEBUG, | ||
85 | filename=LOG_FILENAME, | ||
86 | filemode='w') | ||
87 | console = logging.StreamHandler() | ||
88 | console.setLevel(logging.INFO) | ||
89 | formatter = logging.Formatter('%(name)-8s: %(levelname)-4s %(message)s') | ||
90 | console.setFormatter(formatter) | ||
91 | logging.getLogger('').addHandler(console) | ||
92 | |||
93 | |||
94 | def fetch_cookie(): | ||
95 | logger = logging.getLogger('fetch_cookie') | ||
96 | logger.info(get_time() + ': Fetching Cookie...') | ||
97 | |||
98 | driver = webdriver.PhantomJS(PhantomJSPath) | ||
99 | driver.get('https://www.ingress.com/intel') | ||
100 | |||
101 | # get login page | ||
102 | link = driver.find_elements_by_tag_name('a')[0].get_attribute('href') | ||
103 | driver.get(link) | ||
104 | driver.get_screenshot_as_file('1.png') | ||
105 | |||
106 | # simulate manual login | ||
107 | driver.set_page_load_timeout(10) | ||
108 | driver.set_script_timeout(20) | ||
109 | driver.find_element_by_id('Email').send_keys(Email) | ||
110 | driver.get_screenshot_as_file('2.png') | ||
111 | driver.find_element_by_css_selector('#next').click() | ||
112 | time.sleep(3) | ||
113 | driver.find_element_by_id('Passwd').send_keys(Passwd) | ||
114 | driver.get_screenshot_as_file('3.png') | ||
115 | driver.find_element_by_css_selector('#signIn').click() | ||
116 | time.sleep(3) | ||
117 | driver.get_screenshot_as_file('4.png') | ||
118 | |||
119 | # get cookies | ||
120 | cookies = driver.get_cookies() | ||
121 | |||
122 | csrftoken = '' | ||
123 | SACSID = '' | ||
124 | for key in cookies: | ||
125 | if key['name'] == 'csrftoken': | ||
126 | csrftoken = key['value'] | ||
127 | if key['name'] == 'SACSID': | ||
128 | SACSID = key['value'] | ||
129 | |||
130 | if csrftoken == '' or SACSID == '': | ||
131 | raise CookieException | ||
132 | |||
133 | with open('cookie', 'w') as file: | ||
134 | cookie = 'SACSID='+SACSID+'; csrftoken='+csrftoken+'; ingress.intelmap.shflt=viz; ingress.intelmap.lat=29.098418372855484; ingress.intelmap.lng=119.81689453125; ingress.intelmap.zoom=17' | ||
135 | file.write(cookie) | ||
136 | |||
137 | logger.info(get_time() + ': Fetching Cookie Succeed') | ||
138 | driver.quit() | ||
139 | return True | ||
140 | |||
141 | |||
142 | def send_message(bot, message, monitor=False): | ||
143 | logger = logging.getLogger('send_message') | ||
144 | while True: | ||
145 | try: | ||
146 | if monitor is True: | ||
147 | bot.sendMessage(chat_id="@voamonitor", text=message) | ||
148 | else: | ||
149 | bot.sendMessage(chat_id=CHANNEL_NAME, text=message) | ||
150 | logger.info(get_time() + ": sendMsg " + message) | ||
151 | break | ||
152 | except telegram.TelegramError: | ||
153 | logger.error(get_time() + ": Send Message to Channel Failed") | ||
154 | time.sleep(1) | ||
155 | except Exception: | ||
156 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0])) | ||
157 | time.sleep(1) | ||
158 | |||
159 | |||
160 | def find_message_record(id): | ||
161 | uri = 'mongodb://' + DBHost | ||
162 | conn = MongoClient(uri) | ||
163 | conn.api.authenticate(DBUser, DBPass, DBName) | ||
164 | database = conn[DBName] | ||
165 | collection = database.entries | ||
166 | count = collection.find({"id": id}).count() | ||
167 | conn.close() | ||
168 | if count == 0: | ||
169 | return False | ||
170 | else: | ||
171 | return True | ||
172 | |||
173 | |||
174 | def insert_message_to_database(time, id, msg): | ||
175 | uri = 'mongodb://' + DBHost | ||
176 | conn = MongoClient(uri) | ||
177 | conn.api.authenticate(DBUser, DBPass, DBName) | ||
178 | database = conn[DBName] | ||
179 | collection = database.entries | ||
180 | post = {"id": id, "time": time, "msg": msg} | ||
181 | collection.insert(post) | ||
182 | conn.close() | ||
183 | |||
184 | |||
185 | def main(): | ||
186 | logger = logging.getLogger('main') | ||
187 | |||
188 | # Lat & Lng of fetch region | ||
189 | field = { | ||
190 | 'minLngE6': minLngE6, | ||
191 | 'minLatE6': minLatE6, | ||
192 | 'maxLngE6': maxLngE6, | ||
193 | 'maxLatE6': maxLatE6, | ||
194 | } | ||
195 | |||
196 | mints = -1 | ||
197 | maxts = -1 | ||
198 | reverse = False | ||
199 | tab = 'all' | ||
200 | |||
201 | # fetch cookie | ||
202 | while True: | ||
203 | try: | ||
204 | if fetch_cookie(): | ||
205 | break | ||
206 | except CookieException: | ||
207 | logger.error(get_time() + ': Fetch Cookie Failed') | ||
208 | time.sleep(3) | ||
209 | except Exception: | ||
210 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0])) | ||
211 | time.sleep(3) | ||
212 | |||
213 | # fetch message | ||
214 | count = 0 | ||
215 | while True: | ||
216 | count += 1 | ||
217 | |||
218 | with open('cookie') as cookies: | ||
219 | cookies = cookies.read().strip() | ||
220 | logger.info(get_time() + ": {} Fetching from Intel...".format(str(count))) | ||
221 | |||
222 | with open('cookie') as cookies: | ||
223 | cookies = cookies.read().strip() | ||
224 | |||
225 | # fetch message per time | ||
226 | while True: | ||
227 | try: | ||
228 | intel = ingrex.Intel(cookies, field) | ||
229 | result = intel.fetch_msg(mints, maxts, reverse, tab) | ||
230 | if result: | ||
231 | mints = result[0][1] + 1 | ||
232 | break | ||
233 | except CookieException: | ||
234 | while True: | ||
235 | try: | ||
236 | if fetch_cookie(): | ||
237 | break | ||
238 | except CookieException: | ||
239 | time.sleep(3) | ||
240 | except Exception: | ||
241 | logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0])) | ||
242 | time.sleep(3) | ||
243 | |||
244 | for item in result[::-1]: | ||
245 | # Check spam message | ||
246 | pattern = re.compile(BlockList) | ||
247 | match = pattern.search(str(item)) | ||
248 | if match: | ||
249 | continue | ||
250 | |||
251 | message = ingrex.Message(item) | ||
252 | if message.ptype == 'PLAYER_GENERATED': | ||
253 | logger.info(get_time() + " " + message.msg) | ||
254 | if find_message_record(message.guid) is False: | ||
255 | insert_message_to_database(message.time, message.guid, message.msg) | ||
256 | send_message(bot, message.msg, False) | ||
257 | |||
258 | time.sleep(10) | ||
259 | |||
260 | if __name__ == '__main__': | ||
261 | read_config() | ||
262 | bot = telegram.Bot(BOT_TOKEN) | ||
263 | |||
264 | while True: | ||
265 | try: | ||
266 | main() | ||
267 | except Exception: | ||
268 | send_message(bot, 'Main Unexpected error' + str(sys.exc_info()[0]), True) | ||
269 | time.sleep(3) \ No newline at end of file | ||