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
247
248
249
250
251
252
253
254
255
256
257
|
# -*- coding: utf-8 -*-
import os
import re
import sys
import time
import json
import ingrex
import logging
import inspect
import telegram
from selenium import webdriver
from pymongo import MongoClient
bot = None
BOT_TOKEN = ''
CHANNEL_NAME = ''
Email = ''
Passwd = ''
PhantomJSPath = ''
DBName = ''
DBUser = ''
DBPass = ''
DBHost = ''
BlockList = ''
LOG_FILENAME = 'voh.log'
TIME_ZONE='Asia/Shanghai'
minLngE6 = 0
minLatE6 = 0
maxLngE6 = 0
maxLatE6 = 0
class CookieException(Exception):
"""Intel Error"""
pass
def get_time():
return time.strftime('%x %X %Z')
def read_config():
global Email
global Passwd
global BOT_TOKEN
global CHANNEL_NAME
global PhantomJSPath
global DBName
global DBUser
global DBPass
global DBHost
global BlockList
global LOG_FILENAME
global minLngE6
global minLatE6
global maxLngE6
global maxLatE6
configfile = open("./config.json")
config = json.load(configfile)
Email = config["Email"]
Passwd = config["Passwd"]
BOT_TOKEN = config["BOT_TOKEN"]
CHANNEL_NAME = config["CHANNEL_NAME"]
PhantomJSPath = config["PhantomJSPath"]
DBName = config["DBName"]
DBUser = config["DBUser"]
DBPass = config["DBPass"]
DBHost = config["DBHost"]
BlockList = config["BlockList"]
minLngE6 = config["minLngE6"]
minLatE6 = config["minLatE6"]
maxLngE6 = config["maxLngE6"]
maxLatE6 = config["maxLatE6"]
os.environ['TZ'] = TIME_ZONE
time.tzset()
logging.basicConfig(level=logging.DEBUG,
filename=LOG_FILENAME,
filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-8s: %(levelname)-4s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
def fetch_cookie():
logger = logging.getLogger('fetch_cookie')
logger.info(get_time() + ': Fetching Cookie...')
driver = webdriver.PhantomJS(PhantomJSPath)
driver.get('https://www.ingress.com/intel')
# get login page
link = driver.find_elements_by_tag_name('a')[0].get_attribute('href')
driver.get(link)
driver.get_screenshot_as_file('1.png')
# simulate manual login
driver.set_page_load_timeout(10)
driver.set_script_timeout(20)
driver.find_element_by_id('Email').send_keys(Email)
driver.get_screenshot_as_file('2.png')
driver.find_element_by_css_selector('#next').click()
time.sleep(3)
driver.find_element_by_id('Passwd').send_keys(Passwd)
driver.get_screenshot_as_file('3.png')
driver.find_element_by_css_selector('#signIn').click()
time.sleep(3)
driver.get_screenshot_as_file('4.png')
# get cookies
cookies = driver.get_cookies()
csrftoken = ''
SACSID = ''
for key in cookies:
if key['name'] == 'csrftoken':
csrftoken = key['value']
if key['name'] == 'SACSID':
SACSID = key['value']
if csrftoken == '' or SACSID == '':
raise CookieException
with open('cookie', 'w') as file:
cookie = 'SACSID='+SACSID+'; csrftoken='+csrftoken+'; ingress.intelmap.shflt=viz; ingress.intelmap.lat=29.098418372855484; ingress.intelmap.lng=119.81689453125; ingress.intelmap.zoom=17'
file.write(cookie)
logger.info(get_time() + ': Fetching Cookie Succeed')
driver.quit()
return True
def send_message(bot, message, monitor=False):
logger = logging.getLogger('send_message')
while True:
try:
if monitor is True:
bot.sendMessage(chat_id="@voamonitor", text=message)
else:
print(type(message))
bot.sendMessage(chat_id=CHANNEL_NAME, text=message)
logger.info(get_time() + ": sendMsg " + message)
break
except telegram.TelegramError:
logger.error(get_time() + ": Send Message to Channel Failed")
time.sleep(1)
except Exception:
logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno))
time.sleep(1)
def find_message_record(id):
uri = 'mongodb://' + DBHost
conn = MongoClient(uri)
conn.api.authenticate(DBUser, DBPass, DBName)
database = conn[DBName]
collection = database.entries
count = collection.find({"id": id}).count()
conn.close()
if count == 0:
return False
else:
return True
def insert_message_to_database(time, id, msg):
uri = 'mongodb://' + DBHost
conn = MongoClient(uri)
conn.api.authenticate(DBUser, DBPass, DBName)
database = conn[DBName]
collection = database.entries
post = {"id": id, "time": time, "msg": msg}
collection.insert(post)
conn.close()
def main():
logger = logging.getLogger(__name__)
# Lat & Lng of fetch region
field = {
'minLngE6': minLngE6,
'minLatE6': minLatE6,
'maxLngE6': maxLngE6,
'maxLatE6': maxLatE6,
}
mints = -1
maxts = -1
reverse = False
tab = 'all'
# fetch cookie
while True:
try:
if fetch_cookie():
break
except CookieException:
logger.error(get_time() + ': Fetch Cookie Failed')
time.sleep(3)
except:
logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno))
time.sleep(3)
# fetch message
count = 0
while True:
count += 1
logger.info(get_time() + ": {} Fetching from Intel...".format(str(count)))
with open('cookie') as cookies:
cookies = cookies.read().strip()
# fetch message per time
while True:
try:
intel = ingrex.Intel(cookies, field)
result = intel.fetch_msg(mints, maxts, reverse, tab)
if result:
mints = result[0][1] + 1
break
except:
logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno))
time.sleep(3)
for item in result[::-1]:
# Check spam message
pattern = re.compile(BlockList)
match = pattern.search(str(item))
if match:
continue
message = ingrex.Message(item)
if message.ptype == 'PLAYER_GENERATED':
if find_message_record(message.guid) is False:
insert_message_to_database(message.time, message.guid, message.msg)
send_message(bot, message.msg, False)
time.sleep(10)
if __name__ == '__main__':
read_config()
bot = telegram.Bot(BOT_TOKEN)
while True:
try:
main()
except Exception as e:
send_message(bot, 'Main Unexpected error' + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno), True)
time.sleep(3)
|