aboutsummaryrefslogtreecommitdiff
path: root/bot.py
blob: 80ee4ed8169c6e6f814cbe813ccef09500444e36 (plain) (blame)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# -*- coding: utf-8 -*-

import os
import re
import time
import json
import logging
import urllib
from selenium import webdriver

import telegram
from telegram.error import NetworkError
from pymongo import MongoClient

import ingrex

Debug = True
bot = None
BOT_TOKEN = ''
CHANNEL_NAME = ''
Email = ''
Passwd = ''
PhantomJSPath = ''
DBName = ''
DBUser = ''
DBPass = ''
DBHost = ''
BlockList = ''

LOG_FILENAME = 'voh.log'
logging.basicConfig(level=logging.DEBUG,
                    filename=LOG_FILENAME,
                    filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-8s: %(levelname)-4s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)


class CookieException(Exception):
    ''' CookieError '''


def getTime():
    return time.strftime('%x %X %Z')


def readConfig():
    global Email
    global Passwd
    global BOT_TOKEN
    global CHANNEL_NAME
    global PhantomJSPath
    global DBName
    global DBUser
    global DBPass
    global DBHost
    global BlockList

    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"]

    os.environ['TZ'] = 'Asia/Shanghai'
    time.tzset()


def fetchCookie():
    global Debug
    global Email
    global Passwd
    global PhantomJSPath

    logger = logging.getLogger('fetchCookie')
    logger.info(getTime() + ': 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)
    if Debug:
        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)
    if Debug:
        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)
    if Debug:
        driver.get_screenshot_as_file('3.png')
    driver.find_element_by_css_selector('#signIn').click()
    time.sleep(3)
    if Debug:
        driver.get_screenshot_as_file('3.png')
    # get cookies
    temp = driver.get_cookies()

    csrftoken = ''
    SACSID = ''
    for a in temp:
        if (a['name'] == 'csrftoken'):
            csrftoken = a['value']
        if (a['name'] == 'SACSID'):
            SACSID = a['value']

    if csrftoken == '' or SACSID == '':
        logger.error(getTime() + ': Fetch Cookie Failed')
        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)

    driver.quit()
    logger.info(getTime() + ': Fetching Cookie Succeed')
    return True


def sendMessge(bot, msg):
    "sendMsg"

    logger = logging.getLogger('sendMessage')
    while True:
        try:
            url = 'https://api.telegram.org/bot'
            url += BOT_TOKEN
            url += '/sendMessage?chat_id='
            url += CHANNEL_NAME
            url += '&text='
            url += urllib.parse.quote(msg)

            req = urllib.request.Request(url, headers={'Content-Type': 'application/x-www-form-urlencoded'})
            resp = urllib.request.urlopen(req)
            data = resp.read()
            logger.info(data)
            logger.info(getTime() + ": sendMsg " + msg)
            break
        except NetworkError:
            time.sleep(1)


def sendMonitor(bot, msg):
    logger = logging.getLogger('sendMonitor')
    while True:
        try:
            bot.sendMessage(chat_id="@voamonitor", text=msg)
            logger.info(getTime() + ": sendMsg " + msg)
            break
        except NetworkError:
            time.sleep(1)


def formatMessage(raw):
    pattern = re.compile(BlockList)
    match = pattern.search(str(raw))
    if match:
        return "Blocked"

    msg = ''
    plext = raw[2]['plext']
    markup = plext['markup']

    for mark in markup:
        if mark[0] == 'SECURE':
            msg += mark[1]['plain']
        elif mark[0] == 'SENDER':
            player = mark[1]['plain']
            team = mark[1]['team']

            pattern = re.compile(':')
            match = pattern.search(player)
            if match:
                if team == 'RESISTANCE':
                    player = player[:match.span()[0]] + ' 🐳' + player[match.span()[0]:]
                elif team == 'ENLIGHTENED':
                    player = player[:match.span()[0]] + ' 🐸' + player[match.span()[0]:]
            msg += player

        elif mark[0] == 'PLAYER' or mark[0] == 'AT_PLAYER':
            player = mark[1]['plain']
            team = mark[1]['team']

            msg += player
            if team == 'RESISTANCE':
                msg += ' 🐳'
            elif team == 'ENLIGHTENED':
                msg += ' 🐸'

        elif mark[0] == 'TEXT':
            msg += mark[1]['plain']

    pattern = re.compile('\[secure\]')
    match = pattern.search(msg)
    if match:
        if msg.find(':') != -1:
            msg = msg[:9] + '@' + msg[9:]
        else:
            msg = msg[:10] + '@' + msg[10:]
    else:
        msg = '@' + msg

    return msg


def FindRecord(id):
    uri = 'mongodb://' + DBHost
    Conn = MongoClient(uri)
    Conn.api.authenticate(DBUser, DBPass, DBName)
    database = Conn[DBName]
    mycollection = database.entries
    res = mycollection.find({"id": id})
    if res.count() == 0:
        return False
    else:
        return True


def insertDB(time, id, msg):
    uri = 'mongodb://' + DBHost
    Conn = MongoClient(uri)
    Conn.api.authenticate(DBUser, DBPass, DBName)
    database = Conn[DBName]
    mycollection = database.entries
    post = {"id": id, "time": time, "msg": msg}
    mycollection.insert(post)
    Conn.close()


def main():
    logger = logging.getLogger('main')

    field = {
        'minLngE6': 119618783,
        'minLatE6': 29912919,
        'maxLngE6': 121018722,
        'maxLatE6': 30573739,
    }

    mints = -1
    maxts=-1
    reverse=False
    tab='all'

    while True:
        try:
            if fetchCookie():
                break
        except CookieException:
            time.sleep(3)

    count = 0
    while True:
        count += 1

        with open('cookie') as cookies:
            cookies = cookies.read().strip()

        logger.info(getTime() + ": {} Fetching from Intel...".format(str(count)))

        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 CookieException:
                while True:
                    try:
                        if fetchCookie():
                            break
                    except CookieException:
                        time.sleep(3)
            except Exception:
                pass

        for item in result[::-1]:
            message = ingrex.Message(item)

            if message.ptype == 'PLAYER_GENERATED':
                # logger.info(getTime() + str(item))

                msg = formatMessage(item)
                if msg == 'Blocked':
                    logger.info(getTime() + " " + message.text)
                else:
                    msg = message.time + " " + msg
                    # logger.info(getTime() + " " + msg)
                    if FindRecord(message.guid) is False:
                        insertDB(message.time, message.guid, msg)
                        # sendMonitor(bot, msg)
                        sendMessge(bot, msg)

        time.sleep(10)

if __name__ == '__main__':
    readConfig()
    bot = telegram.Bot(BOT_TOKEN)

    while True:
        try:
            main()
        except Exception:
            sendMonitor(bot, 'Main Error')
Powered by cgit v1.2.3 (git 2.41.0)