def GetTorrentFile(self, bbsUrl): try: torrentUrl, torrentName = self.GetTorrentFileLink(bbsUrl) if torrentUrl == '' or torrentName == '': log.error("GetTorrentFile| GetTorrentFileLink Fail") return False, '' r = requests.get(torrentUrl, stream=True, headers={'referer': bbsUrl}) size = float(r.headers['content-length']) / 1024.0 with open(torrentName.encode('utf-8'), 'wb') as f: chunks = enumerate(r.iter_content(chunk_size=1024)) for index, chunk in chunks: if chunk: f.write(chunk) f.flush() except requests.exceptions.RequestException as e: log.error('GetTorrentFile Fail, Request Exception : %s', e) log.error("GetTorrentfile Exception : %s", traceback.format_exc()) return False, '' except: log.error("Get Torrent File Fail, url:'%s'", bbsUrl) log.error("GetTorrentfile Exception : %s", traceback.format_exc()) return False, '' return True, torrentName
def wat_update_starttime(self, starttime): ''' ''' try: self._client.root.client_update_starttime(self._taskid, starttime) except Exception, e: log.error(str(e))
def GetTorrentFileLink(self, bbsUrl): try: opener = urllib2.build_opener() opener.addheaders = [( 'User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11' )] data = opener.open(bbsUrl) sp = BeautifulSoup.BeautifulSoup(data) ATags = sp.findAll('a', {'rel': 'nofollow'}) fullLink = ATags[1]['href'] start = fullLink.find("'") + 1 end = fullLink.find("'", start) fileLink = 'https://torrentkim3.net' + fullLink[start:end] torrentName = sp.find('div', { 'id': 'writeContents' }).find('legend').text except urllib2.HTTPError, e: log.error("GetTorrentFileLink Fail, HTTPError:'%d', Except :'%s'", e.code, e) return '', ''
def main(): global botManager try: TOKEN = botConfig.GetBotToken() log.info('Telegram BOT Initialize...') # signal Register signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGABRT, signal_handler) signal.signal(signal.SIGSEGV, signal_handler) signal.signal(signal.SIGHUP, signal_handler) log.info('signal Register success') bot = BotManager.BOTManager(TOKEN) botManager = bot log.info('Telegram BOT Init OK') bot.message_loop() while 1: time.sleep(10) log.info('Telegram BOT Exit...') except Exception, e: log.error(e, exc_info=True)
def DeleteTask(self, task_id): delete_url = self.cfg.GetDSDownloadUrl( ) + '/webapi/DownloadStation/task.cgi' params = { 'api': 'SYNO.DownloadStation.Task', 'version': '3', 'method': 'delete', 'id': task_id } try: res = requests.get(delete_url, params=params, cookies=self.auth_cookie, verify=self.cfg.IsUseCert()) except requests.ConnectionError: log.error('DeleteTask|synology rest api request Connection Error') return False except: log.error('DeleteTask|synology requests fail') return False if res.status_code != 200: log.warn("Delete Task Request fail") return False json_data = json.loads(res.content.decode('utf-8')) if self.ChkAPIResponse(json_data, "Download station Delete Task") == False: return False return True
def GetTorrentFile(self, bbsUrl): try: torrentUrl, torrentName = self.GetTorrentFileLink(bbsUrl) if torrentUrl == '' or torrentName == '': log.error("GetTorrentFile| GetTorrentFileLink Fail") return False, '' #torrentName = "/tmp/" + torrentName torrentName = os.path.join(u"/tmp/", torrentName) r = requests.get(torrentUrl, stream=True, headers={'referer': bbsUrl}) size = float(r.headers['content-length']) / 1024.0 with open(torrentName.encode('utf-8'), 'wb') as f: chunks = enumerate(r.iter_content(chunk_size=1024)) for index, chunk in chunks: if chunk: f.write(chunk) f.flush() except requests.exceptions.RequestException as e: log.error('GetTorrentFile Fail, Request Exception : %s', e) log.error("GetTorrentfile Exception : %s", traceback.format_exc()) return False, '' except: log.error("Get Torrent File Fail, url:'%s'", bbsUrl) log.error("GetTorrentfile Exception : %s", traceback.format_exc()) return False, '' return True, torrentName.encode('utf-8')
def GetConfig(): try: global kia_id global kia_pw global mysql_id global mysql_pw global mysql_db global mysql_host global sqlite_db_path global bot_token global bot_chat_id config = configparser.ConfigParser() config.read('/home/pi/source/UVO/config.ini') mysql_use = config.getboolean('SETTING', 'USE_MYSQL') sqlite_use = config.getboolean('SETTING', 'USE_SQLITE') kia_id = config['KIA']['id'] kia_pw = config['KIA']['pw'] mysql_id = config['MYSQL']['id'] mysql_pw = config['MYSQL']['pw'] mysql_db = config['MYSQL']['db'] mysql_host = config['MYSQL']['host'] sqlite_db_path = config['SQLITE']['file_path'] bot_token = config['BOT']['token'] bot_chat_id = int(config['BOT']['chat_id']) except: msg = 'Get Config file except' log.error(msg) sys.excepthook = exception_hook
def on_chat_message(self, msg): try: flavor = telepot.flavor(msg) # inline query test code... # Have to answer inline query to receive chosen result log.info('flavor : %s', flavor) if flavor == 'inline_query': log.info('inline query!!') query_id, from_id, query_string = telepot.glance(msg, flavor=flavor) log.info('Inline Query: id:%s, from:%d, msg:%s', query_id, from_id, query_string) articles = [{ 'type': 'article', 'id': 'abc', 'title': 'ABC', 'message_text': 'Good morning' }] self.bot.answerInlineQuery(query_id, articles) return content_type, chat_type, chat_id = telepot.glance(msg) log.info("ContentType : '%s'", content_type) log.info("chat_type : '%s'", chat_type) log.info("chat_id : %d", chat_id) # Message to Log Write self.PrintMsg(msg) # Valid User Check if not chat_id in self.valid_user: log.info("Invalid user : %d", chat_id) return log.debug("chat_type:'%s'", chat_type) if chat_type == 'group': groupMsg = self.group_command_handler(unicode(msg['text']), chat_id) log.info("Group Message : %s", groupMsg) return if content_type is 'text': self.command_handler(unicode(msg['text']), chat_id) log.info(msg['text']) return if content_type is 'document': file_name = msg['document']['file_name'] file_id = msg['document']['file_id'] file_ext = os.path.splitext(file_name) file_type = msg['document']['mime_type'] self.file_handler(file_name, file_id, file_ext[1], file_type, chat_id) return except Exception, e: log.error(e, exc_info=True)
def t_update_percent(self, progress): ''' ''' try: self._client.root.client_update_progress(self._taskid, progress) except Exception, e: log.error(str(e))
def InsertMySql(data): if mysql_use == False: return # Mysql Insert conn = pymysql.connect(host=mysql_host, user=mysql_id, password=mysql_pw, db=mysql_db, charset='utf8') curs = conn.cursor() #query = "insert into niro_data values(now(), %s, %s, %s, %s, %s, %s);" % (accumulated_distance, distance_driven.text, operating_time.text, average_speed.text, max_speed.text, safe_score.text) query = "insert into niro_data values(now(), %s, %s, %s, %s, %s, %s);" % ( data[0], data[1], data[2], data[3], data[4], data[5]) log.info("Mysql Query : %s", query) try: curs.execute(query) conn.commit() except: log.error("Mysql Insert Fail") sys.excepthook = exception_hook finally: conn.close()
def InsertSqliteDB(data): if sqlite_use == False: return conn = sqlite3.connect(sqlite_db_path) try: cur = conn.cursor() # Create Table query = "CREATE TABLE IF NOT EXISTS niro_data( date text, accumulated_distance integer, distance_driven integer, operating_time integer, average_speed integer, max_speed integer, safe_score real);" cur.execute(query) today = datetime.now().strftime("%Y/%m/%d") # query = "insert into niro_data(date, accumulated_distance, distance_driven, operating_time, average_speed, max_speed, safe_score) values (?, ?, ?, ?, ?, ?, ?);" #query = "insert into niro_data values (%s, %s, %s, %s, %s, %s, %s);" % (today, data['accumulated_distance'], data['distance_driven'], data['operating_time'], data['average_speed'], data['max_speed'], data['safe_score']) query = "insert into niro_data values (%s, %s, %s, %s, %s, %s, %s);" % ( today, data[0], data[1], data[2], data[3], data[4], data[5]) log.info("sqlite query : %s", query) cur.execute(query) conn.commit() except: log.error("Sqlite insert except") sys.excepthook = exception_hook finally: conn.close()
def CreateTaskForFile(self, file_path): create_url = self.cfg.GetDSDownloadUrl() + '/webapi/DownloadStation/task.cgi' params2 = {'api' : 'SYNO.DownloadStation.Task', 'version' : '3', 'method' : 'create' } files = {'file' : open(file_path, 'rb')} try: res = requests.post(create_url, data=params2, files=files, cookies=self.auth_cookie, verify=self.cfg.IsUseCert()) except requests.ConnectionError: log.error('CreateTaskForFile|synology rest api request Connection Error') return False except: log.error('CreateTaskForFile|synology requests fail') return False if res.status_code != 200: # print('request fail') log.warn("Create Task For File Request fail") return False json_data = json.loads(res.content.decode('utf-8')) if self.ChkTaskResponse(json_data, "Download station Create Task for file") == False: return False # Remove Torrent File files['file'].close() os.remove(file_path) log.info('Torrent File removed, file:%s', file_path) return True
def PrintException(self): exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) log.error('EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj))
def t_task_exist(self): ''' ''' result = None try: result = self._client.root.client_task_exist(self._taskid) except Exception, e: log.error(str(e))
def GetTaskList(self): #url = 'https://downloadstation_dsm_url:9999/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task&version=1&method=list' if self.auth_cookie == None: return False params = { 'api': 'SYNO.DownloadStation.Task', 'version': '3', 'method': 'list' } url = self.cfg.GetDSDownloadUrl() + '/webapi/DownloadStation/task.cgi' try: res = requests.get(url, params=params, cookies=self.auth_cookie, verify=self.cfg.IsUseCert()) except requests.ConnectionError: log.error('GetTaskList|synology rest api request Connection Error') return False except: log.error('GetTaskList|synology requests fail') return False if res.status_code != 200: log.warn("Get Task List Request fail") return False #print(res.content) json_data = json.loads(res.content.decode('utf-8')) if self.ChkTaskResponse( json_data, "GetTaskList Download station api fail") == False: self.auth_cookie = None return False exists_task_list = [] for item in json_data['data']['tasks']: # log.info('GetTaskList : %s, %s, %s, %s, %s' % (item['id'], item['title'], CommonUtil.hbytes(item['size']), item['username'], item['status']) ) # size 가 0 보다 큰 값인 경우에만 Torrent 정보가 정상적으로 확인 된다. exists_task_list.append(item['id']) tor_size = int(item['size']) if tor_size > 0: self.theTaskMgr.InsertOrUpdateTask(item['id'], item['title'], item['size'], item['username'], item['status']) self.theTaskMgr.CheckRemoveTest(exists_task_list) if self.cfg.IsTaskAutoDel() == True: self.TaskAutoDelete(json_data) return True
def PrintException(self): exc_type, exc_obj, tb = sys.exc_info() f = tb.tb_frame lineno = tb.tb_lineno filename = f.f_code.co_filename linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) log.error('EXCEPTION IN ({}, LINE {} "{}"): {}'.format( filename, lineno, line.strip(), exc_obj))
def inc(self): ''' add 1 small unit to the current value ''' if self._current_value == self._max_value: log.error('Current value can never be greater than max value!') else: self._current_value +=0.1 self._update_eta()
def GetStatistic(self): # param = {'api' : 'SYNO.DownloadStation.Statistic', 'version' : '1', 'method' : 'getinfo'} # url = 'https://downloadstation_dsm_url:9999/webapi/DownloadStation/statistic.cgi' if self.auth_cookie == None: return False log.info('try get statistic') params = { 'api': 'SYNO.DownloadStation.Statistic', 'version': '1', 'method': 'getinfo' } url = self.cfg.GetDSDownloadUrl( ) + '/webapi/DownloadStation/statistic.cgi' try: res = requests.get(url, params=params, cookies=self.auth_cookie, verify=self.cfg.IsUseCert()) except requests.ConnectionError: log.error( 'GetStatistic|synology rest api request Connection Error') return False except: log.error('GetStatistic|synology requests fail') return False log.info('GetStatistic|complete get statistic') if res.status_code != 200: log.warn("GetStatistic|Get statistic Request fail") return False json_data = json.loads(res.content.decode('utf-8')) if self.ChkAPIResponse(json_data, "Download station api fail") == False: self.auth_cookie = None log.info('GetStatistic|ChkAPIResponse fail') return False # Data sample : {"data":{"speed_download":3496632,"speed_upload":0},"success":true} item = json_data.get('data') if item != None: download_speed = item['speed_download'] upload_speed = item['speed_upload'] self.SendStatistic(download_speed, upload_speed) else: log.info('GetStatistic|not found data, %s', res.content) return True
def t_report_vuln(self, result, high_count, middle_count, low_count, notice_count): ''' ''' if self.t_task_exist(): try: self._client.root.client_update_reports( self._taskid, result, high_count, middle_count, low_count, notice_count) except Exception, e: log.error(str(e))
def on_close(self, exception): if type(exception) == telepot.helper.WaitTooLong: log.debug('Wait Timeout') if self.cur_mode != '': #self.sender.sendMessage('입력 시간이 초과 되었습니다', reply_markup=self.hide_keyboard) self.cur_mode = '' else: log.error('on_close - exception :') log.error(exception) log.exception("on_close - exception :") traceback.print_exc(file=sys.stdout)
def on_chat_message(self, msg): try: flavor = telepot.flavor(msg) # inline query test code... # Have to answer inline query to receive chosen result log.info('flavor : %s', flavor) if flavor == 'inline_query': log.info('inline query!!') query_id, from_id, query_string = telepot.glance(msg, flavor=flavor) log.info('Inline Query: id:%s, from:%d, msg:%s', query_id, from_id, query_string) articles = [{'type': 'article', 'id': 'abc', 'title': 'ABC', 'message_text': 'Good morning'}] self.bot.answerInlineQuery(query_id, articles) return content_type, chat_type, chat_id = telepot.glance(msg) log.info("ContentType : '%s'", content_type) log.info("chat_type : '%s'", chat_type) log.info("chat_id : %d", chat_id) # Message to Log Write self.PrintMsg(msg) # Valid User Check if not chat_id in self.valid_user: log.info("Invalid user : %d", chat_id) return log.debug("chat_type:'%s'", chat_type) if chat_type == 'group': groupMsg = self.group_command_handler(unicode(msg['text']), chat_id) log.info("Group Message : %s", groupMsg) return if content_type is 'text': self.command_handler(unicode(msg['text']), chat_id) log.info(msg['text']) return if content_type is 'document': file_name = msg['document']['file_name'] file_id = msg['document']['file_id'] file_ext = os.path.splitext(file_name) file_type = msg['document']['mime_type'] self.file_handler(file_name, file_id, file_ext[1], file_type, chat_id) return except Exception, e: log.error(e, exc_info=True)
def db_query(self, query): ret = True if self.curs == None: ret = self.db_connect() if ret == True: try: self.curs.execute(query.decode('utf-8')) result = self.curs.fetchall() except psycopg2.IntegrityError as err: if err.pgcode != '23505': log.error('db_query|DB IntegrityError : %s', err) else: log.error('db_query|DB Not Intergrity Error : %s', err) self.curs.close() self.conn.close() self.curs = None except Exception as err: log.error('db_query|DB Exception : %s', err) self.curs.close() self.conn.close() self.curs = None return False, '' except: log.error("db_query|psycopg except : " + e) self.curs.close() self.conn.close() self.curs = None return False, '' return True, result
def db_query(self, query): ret = True if self.curs == None: ret = self.db_connect() if ret == True: try: self.curs.execute(query.decode('utf-8')) result = self.curs.fetchall() log.info('db_query complete') except psycopg2.IntegrityError as err: if err.pgcode != '23505': log.error('db_query|DB IntegrityError : %s', err) else: log.error('db_query|DB Not Intergrity Error : %s', err) self.curs.close() self.conn.close() self.curs = None except Exception as err: log.error('db_query|DB Exception : %s', err) self.curs.close() self.conn.close() self.curs = None return False, '' except: e = sys.exc_info()[0] log.error("db_query|psycopg except : " + e) self.curs.close() self.conn.close() self.curs = None return False, '' return True, result
def db_exec(self, query): ret = True if self.curs == None: ret = self.db_connect() if ret == True: try: self.curs.execute(query) log.info('db_exec complete') return True except psycopg2.IntegrityError as err: if err.pgcode != '23505': log.error('db_exec|DB IntegrityError : %s', err) else: log.error('db_exec|DB Not Intergrity Error : %s', err) self.curs.close() self.conn.close() self.curs = None except Exception as err: log.error('db_exec|DB Exception : %s', err) self.curs.close() self.conn.close() self.curs = None except: e = sys.exc_info()[0] log.error("db_exec|psycopg except : " + e) self.curs.close() self.conn.close() self.curs = None return False
def CheckDownloadMonitorTable(self): log.info('CheckDownloadMonitorTable start...') ret = True if self.curs == None: ret = self.db_connect() if ret == True: table_query = "select count(*) from information_schema.tables where table_name = 'btdownload_event';" proc_query = "select count(*) from pg_proc where proname = 'process_btdownload_event';" trigger_query = "select count(*) from pg_trigger where tgname = 'btdownload_event';" try: # Check Table Exist self.curs.execute(table_query) rowitem = self.curs.fetchone() if rowitem[0] == 0: log.info('monitor table is not exist.. try create table') self.CreateMonitorTable() self.bot.sendMessage(self.chat_id, 'DS Download Monitor Table 등록') # Check Procedure Exist self.curs.execute(proc_query) rowitem = self.curs.fetchone() if rowitem[0] == 0: log.info('monitor procedure is not exist.. try create procedure') self.CreateMonitorProcedure() self.bot.sendMessage(self.chat_id, 'DS Download Monitor Procedure 등록') # Check Trigger Exist self.curs.execute(trigger_query) rowitem = self.curs.fetchone() if rowitem[0] == 0: log.info('monitor trigger is not exist... try create trigger') self.CreateMonitorTrigger() self.bot.sendMessage(self.chat_id, 'DS Download Monitor Trigger 등록') except psycopg2.IntegrityError as err: if err.pgcode != '23505': log.error('CheckDownloadMonitorTable|DB IntegrityError : %s', err) else: log.error('CheckDownloadMonitorTable|DB Not Intergrity Error : %s', err) self.curs.close() self.conn.close() self.curs = None except Exception as err: log.error('CheckDownloadMonitorTable|DB Exception : %s', err) self.curs.close() self.conn.close() self.curs = None except: log.error("CheckDownloadMonitorTable|psycopg except : " + e) self.curs.close() self.conn.close() self.curs = None log.info('CheckDownloadMonitorTable exit...') return
def _init(self): self._taskid = int(cfg.getData('taskid')) self._website = cfg.getData("target").get_host() self._host = cfg.getData('RPC_SERVER_IP') self._port = int(cfg.getData('RPC_SERVER_PORT')) try: self._client = rpyc.connect(self._host, self._port) self._client.root.open() except Exception, e: log.error(str(e))
def db_exec(self, query): ret = True if self.curs == None: ret = self.db_connect() if ret == True: try: self.curs.execute(query) log.info('db_exec complete') return True except psycopg2.IntegrityError as err: if err.pgcode != '23505': log.error('db_exec|DB IntegrityError : %s', err) else: log.error('db_exec|DB Not Intergrity Error : %s', err) self.curs.close() self.conn.close() self.curs = None except Exception as err: log.error('db_exec|DB Exception : %s', err) self.curs.close() self.conn.close() self.curs = None except: log.error("db_exec|psycopg except : " + e) self.curs.close() self.conn.close() self.curs = None return False
def db_connect(self, host='localhost', dbname='download', user='******', password=''): #global curs #global conn try: self.conn = psycopg2.connect(database=dbname, user=user, password=password) self.conn.autocommit = True except Exception as e: log.error("dsdownload db_connect error") log.error(e) return False self.curs = self.conn.cursor() return True
def TfreecaGetFile(self, board, value, bot, chat_id): bot.sendMessage(chat_id, 'Torrent File 다운로드 시도') result, fileName = self.tfreeca.GetTorrentFile(board, value) if result == False: log.error("url:'%s' Download Fail", value) bot.sendMessage(chat_id, 'Torrent File 다운로드 시도 실패') return False log.info("tfreeca File Download Success, File Name:'%s'", fileName) bot.sendDocument(chat_id, open(fileName.decode('utf-8'), 'rb')) return False
def db_connect(self, host='localhost', dbname='download', user='******', password=''): #global curs #global conn conn_string = "host='%s' dbname='%s' user='******' password='******'" % (host, dbname, user, password) try: self.conn = psycopg2.connect(conn_string) self.conn.autocommit = True except Exception as e: log.error("dsdownload db_connect error") log.error(e) return False self.curs = self.conn.cursor() return True
def TorrentKimGetFile(self, value, bot, chat_id): bot.sendMessage(chat_id, 'Torrent File 다운로드 시도') result, fileName = self.torKim.GetTorrentFile(value) if result == False: log.error("url:'%s' Download Fail", value) bot.sendMessage(chat_id, 'Torrent File 다운로드 시도 실패') return False log.info("Torrent Kim File Download Success, File Name:'%s'", fileName) bot.sendDocument(chat_id, open(fileName, 'rb')) return False
def TorrentKimGetFile(self, type, value, bot, chat_id): bot.sendMessage(chat_id, 'Torrent File 다운로드 시도') result, fileName = self.torKim.GetTorrentFile(value) if result == False: log.error("url:'%s' Download Fail", value) bot.sendMessage(chat_id, fileName) return False log.info("Torrent Kim File Download Success, File Name:'%s'", fileName) bot.sendDocument(chat_id, open(fileName.decode('utf-8'), 'rb')) return False
def GetTorrentFileLink(self, bbsUrl): try: opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11')] data = opener.open(bbsUrl) sp = BeautifulSoup.BeautifulSoup(data) ATags = sp.findAll('a', {'rel' : 'nofollow'}) fullLink = ATags[1]['href'] start = fullLink.find("'")+1 end = fullLink.find("'", start) fileLink = 'https://torrentkim3.net' + fullLink[start:end] torrentName = sp.find('div', {'id': 'writeContents'}).find('legend').text except urllib2.HTTPError, e: log.error("GetTorrentFileLink Fail, HTTPError:'%d', Except :'%s'", e.code, e) return '', ''
def TorrentKimUrlDownload(self, value, bot, chat_id): bot.sendMessage(chat_id, 'Torrent File 다운로드 시도') result, fileName = self.torKim.GetTorrentFile(value) if result == False: log.error("url:'%s' Download Fail", value) bot.sendMessage(chat_id, 'Torrent File 다운로드 시도 실패') return False log.info("File Move '%s' to '%s'", fileName, self.watch_dir.decode('utf-8')) shutil.move(fileName, self.watch_dir.decode('utf-8')) msg = u"%s 파일을\n'%s'\n경로에 다운로드 하였습니다" % ( fileName, self.watch_dir.decode('utf-8')) bot.sendMessage(chat_id, msg) return True
def DsmLogin(self, id, pw, otp_code=None): url = self.cfg.GetDSDownloadUrl() + '/webapi/auth.cgi' if not otp_code: # Not Use OTP Code log.info('without otp') params = { 'api': 'SYNO.API.Auth', 'version': '3', 'method': 'login', 'account': id, 'passwd': pw, 'session': 'DownloadStation', 'format': 'cookie' } else: log.info('with otp') params = { 'api': 'SYNO.API.Auth', 'version': '3', 'method': 'login', 'account': id, 'passwd': pw, 'session': 'DownloadStation', 'format': 'cookie', 'otp_code': otp_code } log.info('Request url : %s', url) try: res = requests.get(url, params=params, verify=self.cfg.IsUseCert()) except requests.ConnectionError: log.error('Login|synology rest api request Connection Error') return False, None except: log.error('Login|synology requests fail') return False, None log.info('auth url requests succ') return True, res
def db_connect(self, host='localhost', dbname='download', user='******', password=''): #global curs #global conn conn_string = "host='%s' dbname='%s' user='******' password='******'" % ( host, dbname, user, password) try: self.conn = psycopg2.connect(conn_string) self.conn.autocommit = True except Exception as e: log.error("dsdownload db_connect error") log.error(e) return False self.curs = self.conn.cursor() return True
def TorrentKimUrlDownload(self, value, bot, chat_id): bot.sendMessage(chat_id, 'Torrent File 다운로드 시도') result, fileName = self.torKim.GetTorrentFile(value) if result == False: log.error("url:'%s' Download Fail", value) bot.sendMessage(chat_id, 'Torrent File 다운로드 시도 실패') return False log.info("File Move '%s' to '%s'", fileName, self.watch_dir.encode('utf-8')) try: shutil.move(fileName, self.watch_dir.encode('utf-8')) except Exception as e: log.error("File Move Exception, '%s'", e) return False try: log.info("File '%s' Download Complete", fileName) msg = "%s 파일을\n'%s'\n경로에 다운로드 하였습니다" % (fileName, self.watch_dir.encode('utf-8')) bot.sendMessage(chat_id, msg) except Exception as e: log.error("BOT SendMessage Error, '%s'", e) return True
def TorrentKimUrlDownload(self, type, value, bot, chat_id): bot.sendMessage(chat_id, 'Torrent File 다운로드 시도') result, fileName = self.torKim.GetTorrentFile(value) if result == False: log.error("url:'%s' Download Fail", value) bot.sendMessage(chat_id, fileName) return False log.info("File Move '%s' to '%s'", fileName, self.watch_dir.encode('utf-8')) try: shutil.move(fileName, self.watch_dir.encode('utf-8')) #shutil.copy(fileName, self.watch_dir.encode('utf-8')) except Exception as e: log.error("File Move Exception, '%s'", e) return False try: log.info("File '%s' Download Complete", fileName) msg = "%s 파일을\n'%s'\n경로에 다운로드 하였습니다" % ( fileName, self.watch_dir.encode('utf-8')) bot.sendMessage(chat_id, msg) except Exception as e: log.error("BOT SendMessage Error, '%s'", e) return True
def DoScanTask(msg_info): log.info("TScanner Get the Msg From the Task Queue") msg_json = json.loads(msg_info) website = msg_json.get("website") taskid = int(msg_json.get("taskid")) profile = msg_json.get("profile") taskstarttime = datetime.datetime.now() if platform.system().lower() == "linux": cmd = "timeout %s %s %s -t %d -s '%s' -p '%s' -m 'teye'" % ( Settings.MAX_SCAN_TIME, Settings.PYTHON_ENV, Settings.TEYE_PY_PATH, taskid, website, profile) else: cmd = "%s %s -t %d -s '%s' -p '%s' -m 'teye'" % ( Settings.PYTHON_ENV, Settings.TEYE_PY_PATH, taskid, website, profile) print cmd log.info(cmd) #ret = subprocess.call(cmd) ret = os.system(cmd) #ret=0 success if ret != 0: log.error("Error DoScanTask:%s" % cmd) rdb = RDB() rdb.connect() taskendtime = datetime.datetime.now() rdb.updateProgress(taskid, 100) rdb.updateFinish(taskid, taskendtime) rdb.close() return False rdb = RDB() rdb.connect() taskendtime = datetime.datetime.now() rdb.updateFinish(taskid, taskendtime) rdb.close() log.info("Scan Website:" + website + " Spend Time:" + str(taskendtime - taskstarttime)) return True
def CreateTaskForUrl(self, url): create_url = self.cfg.GetDSDownloadUrl() + '/webapi/DownloadStation/task.cgi' params = {'api' : 'SYNO.DownloadStation.Task', 'version' : '3', 'method' : 'create' , 'uri' : url} try: res = requests.get(create_url, params=params, cookies=self.auth_cookie, verify=self.cfg.IsUseCert()) except requests.ConnectionError: log.error('CreateTaskForUrl|synology rest api request Connection Error') return False except: log.error('CreateTaskForUrl|synology requests fail') return False if res.status_code != 200: log.warn("Create Task For Url Request fail") return False json_data = json.loads(res.content.decode('utf-8')) if self.ChkTaskResponse(json_data, "Download station Create Task for url") == False: return False return True
if content_type is 'text': self.command_handler(unicode(msg['text']), chat_id) log.info(msg['text']) return if content_type is 'document': file_name = msg['document']['file_name'] file_id = msg['document']['file_id'] file_ext = os.path.splitext(file_name) file_type = msg['document']['mime_type'] self.file_handler(file_name, file_id, file_ext[1], file_type, chat_id) return except Exception, e: log.error(e, exc_info=True) except: log.error('XPEBot on_chat_message Exeption') sys.excepthook = main.exception_hook def on_callback_query(self, msg): query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query') self.PrintMsgCB(msg) chat_id = 0 query_type = msg['message']['chat']['type'] if query_type == 'group': chat_id = int(msg['message']['chat']['id']) else: chat_id = from_id
fullLink = ATags[1]['href'] start = fullLink.find("'")+1 end = fullLink.find("'", start) fileLink = 'https://torrentkim3.net' + fullLink[start:end] torrentName = sp.find('div', {'id': 'writeContents'}).find('legend').text except urllib2.HTTPError, e: log.error("GetTorrentFileLink Fail, HTTPError:'%d', Except :'%s'", e.code, e) return '', '' except urllib2.URLError, e: log.error("GetTorrentFileLink Fail, URLError:'%s', Except :'%s'", str(e.reason), e) return '', '' except httplib.HTTPException, e: log.error("GetTorrentFileLink Fail, HTTP Exception :'%s'", str(e.reason), e) return '', '' return fileLink, torrentName def GetTorrentFile(self, bbsUrl): try: torrentUrl, torrentName = self.GetTorrentFileLink(bbsUrl) if torrentUrl == '' or torrentName == '': log.error("GetTorrentFile| GetTorrentFileLink Fail") return False, '' #torrentName = "/tmp/" + torrentName torrentName = os.path.join(u"/tmp/", torrentName)
def download_db_timer(self): ret = True if self.curs == None: ret = self.db_connect() if ret == True: query = 'SELECT * FROM btdownload_event WHERE isread=0;' try: self.curs.execute(query) rowitems = self.curs.fetchall() if len(rowitems) > 0: for row in rowitems: # task_id|username|filename|status|total_size|isread|create_time task_id = row[0] username = row[1] tor_name = row[2] status_no = row[3] status = self.dsdown_status_to_str(status_no) total_size = CommonUtil.hbytes(row[4]) # bot.sendMessage(24501560, "<b>Bold Text</b>\n<pre color='blue'>Test Message</pre>\nHTML Mode", parse_mode='HTML') # Markdown 문법에서 _ 는 * 로 대체 되므로 \_ 로 변경 # tor_name = tor_name.replace('_', '\_') # Markdown 문법에서 *는 MarkDown 문법을 시작하는 문자이므로 \* 로 변경 # tor_name = tor_name.replace('*', '\*') # Telegram Bot MarkDown 문법 중 *, _, [ 문자는 앞에 역슬래시를 붙여준다' tor_name = re.sub(r"([*_'\[])", r"\\\1", tor_name) # DELETE FROM btdownload_event WHERE task_id = OLD.task_id; if status_no == 999: query = "DELETE FROM btdownload_event WHERE task_id = %d" % (task_id) msg = '*상태* : %s\n*이름* : %s\n*사용자* : %s' % (status, tor_name, username) else: query = "UPDATE btdownload_event SET isread = 1 WHERE task_id = %d" % (task_id) msg = '*상태* : %s\n*이름* : %s\n*크기* : %s\n*사용자* : %s' % (status, tor_name, total_size, username) log.info('Bot send : %s', msg) self.bot.sendMessage(self.chat_id, msg.decode('utf-8'), parse_mode='Markdown') log.info("DB Query : %s", query) self.curs.execute(query) except psycopg2.IntegrityError as err: if err.pgcode != '23505': log.error('download_db_timer|DB IntegrityError : %s', err) else: log.error('download_db_timer|DB Not Intergrity Error : %s', err) self.curs.close() self.conn.close() self.curs = None except Exception as err: log.error('download_db_timer|DB Exception : %s', err) log.error("download_db_timer Exception : %s", traceback.format_exc()) strErr = str(err.message) log.error('error ---- %s, %d', strErr, strErr.find('relation "btdownload_event" does not exist')) if strErr.find('relation "btdownload_event" does not exist') != -1: self.CheckDownloadMonitorTable() self.curs.close() self.conn.close() self.curs = None except: log.error("download_db_timer|psycopg except : " + e) self.curs.close() self.conn.close() self.curs = None
def SendAirKorea(self, bot, chat_id): execute_path = main.botConfig.GetExecutePath() font_path = execute_path + '/NanumGothicExtraBold.ttf' fnt = ImageFont.truetype(font_path, 14) font2_path = execute_path + '/NanumBarunGothic.ttf' fnt2 = ImageFont.truetype(font2_path, 12) img_map_path = execute_path + '/airmap.png' base = Image.open(img_map_path).convert('RGBA') khai_img = Image.new('RGBA', base.size, (255,255,255,0)) drow_khai = ImageDraw.Draw(khai_img) pm10_img = Image.new('RGBA', base.size, (255,255,255,0)) drow_pm10 = ImageDraw.Draw(pm10_img) pm25_img = Image.new('RGBA', base.size, (255,255,255,0)) drow_pm25 = ImageDraw.Draw(pm25_img) date = '' for sido in self.sido_list: khai, pm10, pm25, dataTime = self.GetSidoAirData(sido) if khai == -1 and pm10 == -1 and pm25 == -1: log.error('Get Sido Air Data Fail') return xy = self.sido_xy.get(sido) if xy == None: continue khai_val = str(khai) w, h = drow_khai.textsize(khai_val) x = xy[0] - (w / 2) - 1 y = xy[1] drow_khai.text((x, y), khai_val, font=fnt, fill=self.GetKHAIGradeColor(int(khai_val))) pm10_val = str(pm10) w, h = drow_pm10.textsize(pm10_val) x = xy[0] - (w / 2) - 1 y = xy[1] drow_pm10.text((x, y), pm10_val, font=fnt, fill=self.GetPM10GradeColor(int(pm10_val))) pm25_val = str(pm25) w, h = drow_pm25.textsize(pm25_val) x = xy[0] - (w / 2) - 1 y = xy[1] drow_pm25.text((x, y), pm25_val, font=fnt, fill=self.GetPM25GradeColor(int(pm25_val))) date = self.ChangeTimeString(dataTime).decode('utf-8') drow_khai.text((15, 5), u'통합대기', font=fnt, fill=(0, 0, 0, 255)) drow_khai.text((90, 5), date, font=fnt, fill=(0, 0, 0, 255)) drow_pm10.text((15, 5), u'미세먼지', font=fnt, fill=(0, 0, 0, 255)) drow_pm10.text((90, 5), date, font=fnt, fill=(0, 0, 0, 255)) drow_pm25.text((15, 5), u'초미세먼지', font=fnt, fill=(0, 0, 0, 255)) drow_pm25.text((90, 5), date, font=fnt, fill=(0, 0, 0, 255)) # 통합대기 등급 0~50, 51~100, 101~250, 251~ drow_khai.ellipse(((210,300),(223,312)), fill=(78, 137, 246, 255), outline="white") drow_khai.ellipse(((210,318),(223,330)), fill=(91, 212, 100, 255), outline="white") drow_khai.ellipse(((210,336),(223,348)), fill=(254, 127, 65, 255), outline="white") drow_khai.ellipse(((210,354),(223,366)), fill=(249, 74, 75, 255), outline="white") drow_khai.rectangle(((205, 296), (312, 368)), fill=None, outline=(190, 190, 190, 255)) drow_khai.text((227, 300), u'좋음 0~50', font=fnt2, fill=(0, 0, 0, 255)) drow_khai.text((227, 318), u'보통 51~100', font=fnt2, fill=(0, 0, 0, 255)) drow_khai.text((227, 336), u'나쁨 101~250', font=fnt2, fill=(0, 0, 0, 255)) drow_khai.text((227, 354), u'매우 나쁨 251~', font=fnt2, fill=(0, 0, 0, 255)) drow_khai.text((5, 354), u'※ 출처 : 한국환경공단', font=fnt2, fill=(0, 0, 0, 255)) # 미세먼지 등급 0~30, 31~80, 81~150, 151~ # 미세먼지 등급 WHO 기준 50 이상은 나쁨 drow_pm10.ellipse(((210,300),(223,312)), fill=(78, 137, 246, 255), outline="white") drow_pm10.ellipse(((210,318),(223,330)), fill=(91, 212, 100, 255), outline="white") drow_pm10.ellipse(((210,336),(223,348)), fill=(254, 127, 65, 255), outline="white") drow_pm10.ellipse(((210,354),(223,366)), fill=(249, 74, 75, 255), outline="white") drow_pm10.rectangle(((205, 296), (312, 368)), fill=None, outline=(190, 190, 190, 255)) drow_pm10.text((227, 300), u'좋음 0~25', font=fnt2, fill=(0, 0, 0, 255)) drow_pm10.text((227, 318), u'보통 26~50', font=fnt2, fill=(0, 0, 0, 255)) drow_pm10.text((227, 336), u'나쁨 51~75', font=fnt2, fill=(0, 0, 0, 255)) drow_pm10.text((227, 354), u'매우 나쁨 75~', font=fnt2, fill=(0, 0, 0, 255)) drow_pm10.text((5, 354), u'※ 출처 : 한국환경공단', font=fnt2, fill=(0, 0, 0, 255)) # 초미세먼지 등급 0~15, 16~50, 51~100, 101~ # 초미세먼지 등급 WHO 기준 25 이상은 나쁨 drow_pm25.ellipse(((210,300),(223,312)), fill=(78, 137, 246, 255), outline="white") drow_pm25.ellipse(((210,318),(223,330)), fill=(91, 212, 100, 255), outline="white") drow_pm25.ellipse(((210,336),(223,348)), fill=(254, 127, 65, 255), outline="white") drow_pm25.ellipse(((210,354),(223,366)), fill=(249, 74, 75, 255), outline="white") drow_pm25.rectangle(((205, 296), (312, 368)), fill=None, outline=(190, 190, 190, 255)) drow_pm25.text((227, 300), u'좋음 0~15', font=fnt2, fill=(0, 0, 0, 255)) drow_pm25.text((227, 318), u'보통 16~25', font=fnt2, fill=(0, 0, 0, 255)) drow_pm25.text((227, 336), u'나쁨 26~50', font=fnt2, fill=(0, 0, 0, 255)) drow_pm25.text((227, 354), u'매우 나쁨 51~', font=fnt2, fill=(0, 0, 0, 255)) drow_pm25.text((5, 354), u'※ 출처 : 한국환경공단', font=fnt2, fill=(0, 0, 0, 255)) out_khai = Image.alpha_composite(base, khai_img) out_pm10 = Image.alpha_composite(base, pm10_img) out_pm25 = Image.alpha_composite(base, pm25_img) out_khai.save('/tmp/air_khai.png') out_pm10.save('/tmp/air_pm10.png') out_pm25.save('/tmp/air_pm25.png') #sender.sendMessage(date) #sender.sendPhoto(open('/tmp/air_khai.png')) #sender.sendPhoto(open('/tmp/air_pm10.png')) #sender.sendPhoto(open('/tmp/air_pm25.png')) #bot = BotManager.BOTManager().GetBot() bot.sendMessage(chat_id, date) bot.sendPhoto(chat_id, open('/tmp/air_khai.png')) bot.sendPhoto(chat_id, open('/tmp/air_pm10.png')) bot.sendPhoto(chat_id, open('/tmp/air_pm25.png'))
def UpdateMonitorProcedure(self, bot, chat_id): ret = True if self.curs == None: ret = self.db_connect() if ret == True: try: query = """CREATE OR REPLACE FUNCTION process_btdownload_event() RETURNS TRIGGER AS $btdownload_event$ DECLARE rec_count integer; BEGIN IF (TG_OP = 'INSERT') THEN RETURN NEW; ELSIF (TG_OP = 'UPDATE') THEN IF (NEW.status = 2 AND NEW.total_size > 0 ) THEN SELECT COUNT(*) into rec_count FROM btdownload_event WHERE task_id = NEW.task_id AND status = 2; IF ( rec_count = 0 ) THEN INSERT INTO btdownload_event VALUES(NEW.task_id, NEW.username, NEW.filename, NEW.status, NEW.total_size, 0, now()); END IF; ELSIF (NEW.status = 5 ) THEN SELECT COUNT(*) into rec_count FROM btdownload_event WHERE task_id = NEW.task_id AND status = 5; IF ( rec_count = 0 ) THEN INSERT INTO btdownload_event VALUES(NEW.task_id, NEW.username, NEW.filename, NEW.status, NEW.total_size, 0, now()); END IF; ELSIF (NEW.status = 118) THEN UPDATE download_queue SET status = 5, extra_info = '' WHERE task_id = NEW.task_id; DELETE FROM task_plugin WHERE task_id = NEW.task_id; DELETE FROM thumbnail WHERE task_id = NEW.task_id; ELSIF (NEW.status = 123) THEN SELECT COUNT(*) into rec_count FROM btdownload_event WHERE task_id = NEW.task_id AND status = 123; IF ( rec_count = 0 ) THEN INSERT INTO btdownload_event VALUES(NEW.task_id, NEW.username, NEW.filename, NEW.status, NEW.total_size, 0, now()); END IF; END IF; RETURN NEW; ELSIF (TG_OP = 'DELETE') THEN IF (OLD.status = 2) THEN INSERT INTO btdownload_event VALUES(OLD.task_id, OLD.username, OLD.filename, 999, OLD.total_size, 0, now()); ELSE DELETE FROM btdownload_event WHERE task_id = OLD.task_id; END IF; RETURN OLD; END IF; RETURN NULL; END; $btdownload_event$ LANGUAGE plpgsql;""" if self.db_exec(query) : log.info('UpdateMonitorProcedure Success') bot.sendMessage(chat_id, 'xpebot db update success!!') else: log.info('UpdateMonitorProcedure Fail') bot.sendMessage(chat_id, 'xpebot db update fail!!') except psycopg2.IntegrityError as err: if err.pgcode != '23505': log.error('UpdateMonitorProcedure|DB IntegrityError : %s', err) else: log.error('UpdateMonitorProcedure|DB Not Intergrity Error : %s', err) self.curs.close() self.conn.close() self.curs = None bot.sendMessage(chat_id, 'xpebot db update fail!!') except Exception as err: log.error('UpdateMonitorProcedure|DB Exception : %s', err) log.error("UpdateMonitorProcedure Exception : %s", traceback.format_exc()) strErr = str(err.message) log.error('error ---- %s, %d', strErr, strErr.find('relation "btdownload_event" does not exist')) if strErr.find('relation "btdownload_event" does not exist') != -1: self.CheckDownloadMonitorTable() self.curs.close() self.conn.close() self.curs = None bot.sendMessage(chat_id, 'xpebot db update fail!!') except: log.error("UpdateMonitorProcedure|psycopg except : " + e) self.curs.close() self.conn.close() self.curs = None bot.sendMessage(chat_id, 'xpebot db update fail!!') return
def exception_hook(exc_type, exc_value, exc_traceback): log.error( "Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback) )
signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGABRT, signal_handler) signal.signal(signal.SIGSEGV, signal_handler) signal.signal(signal.SIGHUP, signal_handler) log.info('signal Register success') bot = BotManager.BOTManager(TOKEN) botManager = bot log.info('Telegram BOT Init OK') bot.message_loop() while 1: time.sleep(10) log.info('Telegram BOT Exit...') except Exception, e: log.error(e, exc_info=True) except: log.error('XPEBot Exeption') sys.excepthook = exception_hook return if __name__ == "__main__": main()