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 CheckRemoveTest(self, task_list): temp_task = {} # self.task_data 를 임시 dict 로 저장, 구조는 key : exists flag (true, flase) for key in self.task_data.keys(): temp_task[key] = False # 인자로 넘어온 task_list 를 loop 돌면서 temp_task 에 True 세팅 for task_id in task_list: temp_task[task_id] = True # temp_task 를 loop 돌면서 Value 가 False 이면서 status 가 finished 가 아닌 Task 는 다운로드 취소 콜백 호출 후 task_data 에서 삭제한다 delete_task_list = [] for key, value in temp_task.items(): if value == False: remove_task_list = self.task_data[key] if remove_task_list != None and self.noti_callback != None and remove_task_list[ TASK_TYPE.STATUS] != 'finished': self.noti_callback(key, remove_task_list[TASK_TYPE.TITLE], remove_task_list[TASK_TYPE.SIZE], remove_task_list[TASK_TYPE.USER], 'delete') delete_task_list.append(key) for task_id in delete_task_list: log.info("delete task : %s", task_id) del self.task_data[task_id] self.SaveTask()
def SearchTorrentKim(self, keyword, type=1, max_count=10, page_count=2): if type < 1 or type > 2: log.info('Search Torrent Kim, Unknown Type:%d', type) return False, None tor_url = 'https://torrentkim3.net/bbs/s.php?k=' urlTest = tor_url + quote(keyword.encode('utf-8')) 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(urlTest) sp = BeautifulSoup.BeautifulSoup(data) pageList = self.GetPageLink(sp, page_count) titleList = self.GetTitle(sp) for page in pageList: data = opener.open(page) sp = BeautifulSoup.BeautifulSoup(data) titleList.update(self.GetTitle(sp, len(titleList))) sortTorList = sorted(titleList.items(), reverse=True) inline_keyboard = self.MakeTorrentInlineKeyboard( sortTorList, type, max_count) return True, inline_keyboard
def check(self, t_request): ''' ''' http_request = copy.deepcopy(t_request) url_obj = http_request.get_url() domain = url_obj.get_domain() if self._already_flag: return if domain not in self._already_check_domain: self._already_check_domain.append(domain) self._already_flag = True log.info(u"正在检测目标是否存在版本文件漏洞...") uri_string = url_obj.get_uri_string() for item in self._ver_file: ver_url = URL(uri_string).urljoin(item) res = requests.head(ver_url) ver_ct = res.headers["content-type"].lower() # ("wc.db|entries|index","application/octet-stream") if ver_ct == self._ver_content_type: v = vuln() v.set_url(ver_url) v.set_method("GET") v.set_param("") v.set_name("Ver Vuln") v.set_rank(severity.H) vm.append(self, http_request.get_url().get_host(), "ver", v) log.info("Ver Vuln") print "Ver Vuln 漏洞URL:%s" % (ver_url)
def SearchTorrentKim(self, keyword, type=1, max_count=10, page_count = 2): if type < 1 or type > 2: log.info('Search Torrent Kim, Unknown Type:%d', type) return False, None tor_url = 'https://torrentkim3.net/bbs/s.php?k=' urlTest = tor_url + quote(keyword.encode('utf-8')) 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(urlTest) sp = BeautifulSoup.BeautifulSoup(data) pageList = self.GetPageLink(sp, page_count) titleList = self.GetTitle(sp) if titleList == None: log.info("%s not found torrent", keyword) return False, None for page in pageList: data = opener.open(page) sp = BeautifulSoup.BeautifulSoup(data) titleList.update(self.GetTitle(sp, len(titleList))) sortTorList = sorted(titleList.items(), reverse=True) inline_keyboard = self.MakeTorrentInlineKeyboard(sortTorList, type, max_count) return True, inline_keyboard
def TextToVoice(self, text, bot, chat_id): headers = {'X-Naver-Client-Id' : self.naver_client_id, 'X-Naver-Client-Secret' : self.naver_client_secret} # mijin:미진(한국어, 여성) # jinho:진호(한국어, 남성) # clara:클라라(영어, 여성) # matt:매튜(영어, 남성) # yuri:유리(일본어, 여성) # shinji:신지(일본어, 남성) # meimei:메이메이(중국어, 여성) # -5 ~ 5 사이 정수로 -5면 0.5배 빠른, 5면 0.5배 느린, 0이면 정상 속도 data = {'speaker': 'mijin', 'speed': 0, 'text': text} r = requests.post('https://openapi.naver.com/v1/voice/tts.bin', data = data, headers = headers) now = time.localtime() fname = '/tmp/%d%02d%02d%02d%02d%02d' % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec) f = open(fname, 'wb') f.write(r.content) f.close() response = bot.sendVoice(chat_id, open(fname, 'rb')) log.info('TextToVoice, len:%d', len(r.content)) #os.remove(fname) return response['voice']['file_id']
def GetTitle(self, bs, start_idx = 0): torTable = bs.find('table', attrs={'class':'board_list'}) if torTable == None: log.info("Not Found Torrent") return None torTRs = torTable.findAll('tr', attrs={'class':'bg1'}) idx = start_idx titleList = dict() listValue = list() sortRatingList = list() for item in torTRs: Rating = item.find('font').text TargetItem = item.find('a', attrs={'target':'s'}) if TargetItem == None: continue Title = TargetItem.text torUrl = 'https://torrentkim3.net' + item.find('a', attrs={'target':'s'})['href'][2:] titleList[torUrl] = list() titleList[torUrl].append(Rating) titleList[torUrl].append(Title) idx = idx + 1 return titleList
def check(self, t_request): ''' ''' log.info(u"正在检测目标是否存在Directory目录列举漏洞...") http_request = copy.deepcopy(t_request) url_obj = http_request.get_url() dir_list = url_obj.get_dirs() for item in dir_list: if item in self._already_dir_urls: return self._already_dir_urls.append(item) req_url = item.get_uri_string() res = wcurl.get(req_url) if self._find_vuln(res): v = vuln() v.set_url(req_url) v.set_method("GET") v.set_param("") v.set_name("Directory List Vuln") v.set_rank(severity.M) vm.append(self, http_request.get_url().get_host(), "directory", v) log.info("Directory List Vuln") print "----------Directory List Vuln" break
def StartInputPW(self): log.info("Start Input PW Flow") self.cur_mode = 'input_pw' bot = self.BotUpdater.bot #bot.sendMessage(self.cfg.GetDsmPwId(), "DSM 로그인 비밀번호를 입력하세요\n비밀번호 메시지를 메시지 수신 후 삭제합니다") bot.sendMessage(self.cfg.GetDsmPwId(), self.lang.GetBotHandlerLang('input_login_pw'))
def RegisterMagnetLink(self, magnetLink, bot, chat_id): ds = dsdownload.dsdownload() log.info('DS Torrent download for magnetLink') ret, items = ds.db_query("SELECT * FROM USER_SETTING WHERE username='******';") if ret == False: log.info('DS Download User Setting not found..') return False ds_user = items[0][0] sh_dir = items[0][2] log.info('DS Download Config, Download Directory : %s', sh_dir) log.info("DS Download, user:'******', DownloadPath:'%s', Magnet Link : '%s'", ds_user, sh_dir.decode('utf-8'), magnetLink) query = u"INSERT INTO download_queue (username, url, status, filename, pid, created_time, destination) VALUES ('%s', '%s', 1, 'Magnet Link', %d, %d, '%s');" % (ds_user, magnetLink, os.getpid(), int(time.time()), sh_dir.decode('utf-8')) log.debug(query) ret = ds.db_exec(query.encode('utf-8')) log.info('torrent download query complete') hide_keyboard = {'hide_keyboard': True} if ret == True: msg = 'Magnet Link 가 등록 되었습니다.\n다운로드를 시작합니다.' bot.sendMessage(chat_id, msg, reply_markup=hide_keyboard) else: bot.sendMessage(chat_id, u'Magnet Link 등록 실패', reply_markup=hide_keyboard) return True
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 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 file_handler(self, file_name, file_id, file_ext, file_type, chat_id): log.info('file_name:%s, id:%s, ext:%s', file_name, file_id, file_ext) if file_type == 'application/x-bittorrent': self.tor.ReceiveTorrentFile(file_id, file_name, file_ext, file_type, self.bot, chat_id) return else: log.info('unknown file type, ext:%s', file_ext)
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 check(self, t_request): ''' ''' log.info(u"正在检测目标是否存在命令执行漏洞...") http_request = copy.deepcopy(t_request) if http_request.get_method() == "GET": param_dict = http_request.get_get_param() if http_request.get_method() == "POST": param_dict = http_request.get_post_param() cmd_payload_list = self._get_payload_list(param_dict) for name, poc_info, pattern in cmd_payload_list: if http_request.get_method() == "GET": res = wcurl.get(http_request.get_url().get_uri_string(), params=poc_info) if self._find_vuln(res, pattern): v = vuln() url = res.get_url() v.set_url(url.get_uri_string() + "?" + str(poc_info)) v.set_method("GET") v.set_param(name) v.set_name("CMD Vuln") v.set_rank(severity.H) vm.append(self, url.get_host(), "cmd", v) log.info("CMD Vuln") print u"CMD Vuln 漏洞URL:%s,漏洞参数:%s" % (url, name)
def StartInputOTP(self): log.info("Start Input OTP Flow") self.cur_mode = 'input_otp' bot = self.BotUpdater.bot #bot.sendMessage(self.cfg.GetDsmPwId(), "DSM OTP에 표시된 숫자를 입력하세요") bot.sendMessage(self.cfg.GetDsmPwId(), self.lang.GetBotHandlerLang('input_login_otp'))
def check(self, t_request): ''' ''' log.info(u"正在检测目标是否存在文件包含漏洞...") http_request = copy.deepcopy(t_request) if http_request.get_method() == "GET": param_dict = http_request.get_get_param() if http_request.get_method() == "POST": param_dict = http_request.get_post_param() lfi_payload_list = self._get_payload_list(param_dict) vuln_name = set() for name, poc_info, pattern in lfi_payload_list: res = wcurl.get(http_request.get_url().get_uri_string(), params=poc_info) if self._find_vuln(res, pattern): if name in vuln_name: continue vuln_name.add(name) v = vuln() url = res.get_url() v.set_url(url.get_uri_string() + "?" + str(poc_info)) v.set_method("GET") v.set_param(name) v.set_name("LFI Vuln") v.set_rank(severity.H) vm.append(self, url.get_host(), "lfi", v) log.info("LFI Vuln") print u"LFI Vuln 漏洞URL:%s,漏洞参数:%s" % (url, name)
def StartInputLoginId(self): log.info('Start Input ID Flow') self.cur_mode = 'input_id' bot = self.BotUpdater.bot #bot.sendMessage(self.cfg.GetDsmPwId(), "DSM 로그인 ID를 입력하세요") bot.sendMessage(self.cfg.GetDsmPwId(), self.lang.GetBotHandlerLang('input_login_id'))
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 GetDongneWether(self, bot, chat_id, dongne_name): query = u"select code, sido, gugun, dong from kma_dongne where dong like '%" + dongne_name + "%';" log.debug(query) hide_keyboard = {'hide_keyboard': True} if dongne_name == u'전국 날씨': today, tomorrow = self.GetWeatherSummary(u'전국날씨') bot.sendMessage(chat_id, today, reply_markup=hide_keyboard) return db = sqlite3.connect(self.db_path) cursor = db.cursor() cursor.execute(query) rowdata = cursor.fetchall() before_sido = '' # 동네 이름이 여러개 있을 경우 모두 출력 for row in rowdata: code = str(row[0]) sido = row[1] gugun = row[2] dong = row[3] dong_url = "http://www.kma.go.kr/wid/queryDFSRSS.jsp?zone=" + code + '00' log.info('dongne weather url : %s', dong_url) data = urllib.urlopen(dong_url) sp = BeautifulSoup.BeautifulSoup(data) dongne = sp.findAll('data', attrs={'seq': '0'}) # 풍속의 소수점이 길게 나오는 케이스로 인해 소수점 2자리까지만 표현 ws = round(float(dongne[0].find('ws').text), 2) weather_info = sido + ' ' + gugun + ' ' + dong + u' 날씨\n'\ u'날씨 : ' + dongne[0].find('wfkor').text + u'\n'\ u'온도 : ' + dongne[0].find('temp').text + u"℃\n"\ u'풍속 : ' + str(ws) + u"m/s\n"\ u'풍향 : ' + dongne[0].find('wdkor').text + u"풍\n"\ u'습도 : ' + dongne[0].find('reh').text + u"%\n"\ u'강수 확률 : ' + dongne[0].find('pop').text + u"%\n" sp.close() data.close() bot.sendMessage(chat_id, weather_info, reply_markup=hide_keyboard) # 시도별 날씨 요약본 가져 오기 if before_sido != sido: today, tomorrow = self.GetWeatherSummary(sido) bot.sendMessage(chat_id, today, reply_markup=hide_keyboard) before_sido = sido cursor.close() db.close()
def main(): isSucc = False retry_cnt = 10 try_cnt = 1 GetConfig() bot = telepot.Bot(bot_token) while isSucc == False and retry_cnt > 0: isSucc, data, errLog = GetUVOInfo(try_cnt) if isSucc: InsertSqliteDB(data) InsertMySql(data) log.info("Get UVO Information Succes, [%d/10]", try_cnt) bot.sendMessage(bot_chat_id, "UVO 정보 가져오기 성공") InsertDBSuccessResult() break else: log.info("Get UVO Information Fail, [%d/10]", try_cnt) if try_cnt == 10: msg = "UVO 정보 가져오기 실패\n[%s]\n, [%d/10]" % (errLog, try_cnt) bot.sendMessage(bot_chat_id, msg) retry_cnt = retry_cnt - 1 try_cnt = try_cnt + 1
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 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 SetUseTask(self, task_id): self.ChkDBConnection() update_query = "UPDATE dsdownload_event SET isread = 1 WHERE task_id = '%s';" % (task_id) log.info(update_query) self.cur.execute(update_query) self.con.commit()
def GetTopProcess(count=3): proc_cpu = dict() proc_mem = dict() process_list = psutil.get_pid_list() for pid in process_list: try: info = psutil.Process(pid) proc_cpu[info.name()] = info.get_cpu_percent() temp_mem = info.memory_percent() for child in info.children(recursive=True): temp_mem += child.memory_percent() proc_mem[info.name()] = temp_mem except: log.info('GetTopProcess except') continue cpu_top = sorted(proc_cpu.iteritems(), key=itemgetter(1), reverse=True)[:count] mem_top = sorted(proc_mem.iteritems(), key=itemgetter(1), reverse=True)[:count] result = '\n*Top ' + str(count) + ' cpu use*\n' for cpu in cpu_top: temp = '%s : %.1f\n' % (cpu[0], cpu[1]) result += temp result += '\n*Top ' + str(count) + ' memory use*\n' for mem in mem_top: temp = '%s : %.1f\n' % (mem[0], mem[1]) result += temp return result
def DeleteTask(self, task_id): self.ChkDBConnection() log.info('Delete Task : %s' % (task_id) ) delete_query = "DELETE FROM dsdownload WHERE task_id = '%s';" % (task_id) self.cur.execute(delete_query) self.con.commit()
def ExecutePSQL(query): log.info('Execute PSQL query:%s', query) cmd = 'psql -U postgres -d download -c "' + query + '"' output = os.popen(cmd).read() log.info('ExecuteCommand Result : %s', output) return output
def WakeOnLan(self, MAC, BroadCastAddr="192.168.0.1", port=9): soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) soc.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) sendBytes = soc.sendto("\xff" * 6 + self.GetMacHex(MAC) * 16, (BroadCastAddr, port)) log.info( "Wake On Lan Execute - MAC[%s], Broadcast[%s], Port[%d], SendByte[%d]", MAC, BroadCastAddr, port, sendBytes ) return sendBytes
def __init__(self, *args, **kwargs): super(BOTManager, self).__init__(*args, **kwargs) self._answerer = telepot.helper.Answerer(self) if self.bot_update_loop == None: log.info('bot getUpdates loop start...') self.bot_update_loop = ExTimer.ExTimer(60, self.getUpdatesLoop) self.bot_update_loop.start() return
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) #print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj) log.info('EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj))
def ExecuteCommand(command): #cmd = shlex.split(command.encode('utf-8')) log.info('ExecuteCommand : %s', command) #process = Popen(command, stdout=PIPE) #(output, err) = process.communicate() #exit_code = process.wait() output = os.popen(command).read() log.info('ExecuteCommand Result : %s', output) return output
def LoadFile(self): # Json 문자열을 가져와 Dictionary 로 변환 #json_data = None try: if not self.task_data: with open(task_list_file) as json_file: self.task_data = json.load(json_file) #self.task_data = json.loads(json_data) except: log.info('taskdata.json file open error')
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) #print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj) log.info('EXCEPTION IN ({}, LINE {} "{}"): {}'.format( filename, lineno, line.strip(), exc_obj))
def scan_dir(self, site, dir_file=None): ''' ''' self._dir_file = dir_file if dir_file else Settings.DIR_WEB_FILE file_list = open(self._dir_file, "rb").readlines() for item in file_list: path = item.strip() if path.startswith("#"): continue if site.endswith("/"): url = site[0:-1] + path else: url = site + path res = None try: res = wcurl.get(url, allow_redirects=False) status = res.get_code() if status is None: break msg = "Check URL:" + url + " code:" + str(status) log.info(msg) if status == 200: # unicode body = res.body if not is_404(body): msg = "Found URL:" + url + " code:" + str( status) + " 404 Check: False" print msg self._found_dir.append(url) if status == 301 or status == 302: next_res = wcurl.get(url, allow_redirects=True) if next_res.get_code() == 200: body = res.body if not is_404(body): msg = "Found URL:" + url + " code:" + str( status) + " 404 Check: False" print msg self._found_dir.append(url) except Exception, e: print "Http Request Error %s" % str(e) time.sleep(0.1)
def update_scan_status(self): ''' ''' if self._progress_status: self.progress.finish() else: self.progress.incplugin() log.info(u"当前扫描进度为:" + str(self.progress.get_progress()) + "%") if cfg["remote_mysql"] == True: mm.t_update_percent(self.progress.get_progress())
def CreateMonitorTrigger(self): query = """CREATE TRIGGER btdownload_event AFTER INSERT OR UPDATE OR DELETE ON download_queue FOR EACH ROW EXECUTE PROCEDURE process_btdownload_event();""" if self.db_exec(query): log.info('CreateMonitorTrigger Success') else: log.info('CreateMonitorTrigger Fail') return
def CreateMonitorTrigger(self): query = """CREATE TRIGGER btdownload_event AFTER INSERT OR UPDATE OR DELETE ON download_queue FOR EACH ROW EXECUTE PROCEDURE process_btdownload_event();""" if self.db_exec(query) : log.info('CreateMonitorTrigger Success') else: log.info('CreateMonitorTrigger Fail') return
def on_inline_query(self, msg): query_id, from_id, query_string = telepot.glance(msg, flavor='inline_query') print('Inline Query:', query_id, from_id, query_string) log.info("Inline query - query Id:'%s', From:%d, query:'%s'", query_id, from_id, query_string); def compute_answer(): # Compose your own answers articles = [{'type': 'article', 'id': 'abc', 'title': query_string, 'message_text': query_string}] return articles self._answerer.answer(msg, compute_answer)
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 ReceiveTorrentFile(self, fileid, file_name, file_ext, file_type, bot, chat_id): watch_dir = main.botConfig.GetTorrentWatchDir() filename = file_name + "." + file_ext ds = dsdownload.dsdownload() log.info('DS Torrent download') ret, items = ds.db_query("SELECT * FROM USER_SETTING WHERE username='******';") if ret == False: log.info('DS Download User Setting not found..') return ds_user = items[0][0] #watch_dir = items[0][5] log.info('ReceiveTorrentFile, ds_user:%s, Watch:%s', ds_user, watch_dir.decode('utf-8')) bot.download_file(fileid, watch_dir + file_name ) log.debug('downoad success') log.info('%s download success', filename.decode('utf-8')) hide_keyboard = {'hide_keyboard': True} #msg = file_name.decode('utf-8') + ' 파일을 ' + watch_dir.decode('utf-8') + ' 경로에 다운로드 하였습니다'; bot.sendMessage(chat_id, 'Torrent File Download...', reply_markup=hide_keyboard) return True
def TranslateKo2En(self, text, bot, chat_id): if self.naver_api_use == False: return 'Not Support' text = text.replace('"', '\\"') data = {'source' : 'ko', 'target' : 'en', 'text' : text} header = {'X-Naver-Client-Id': self.naver_client_id, 'X-Naver-Client-Secret': self.naver_client_secret } req_data = requests.post('https://openapi.naver.com/v1/language/translate', data=data, headers=header) if req_data.status_code != 200: log.info('TranslateKo2En Requests error:%d, text:%s' % req_data.status_code, text) return response_json = req_data.content response = json.loads(response_json.decode('utf-8')) if response['message'] and response['message']['result'] and response['message']['result']['translatedText']: translateText = response['message']['result']['translatedText'] log.info('Translate Message : %s', translateText) else: log.info('Translate Fail') log.info(response_json) translateText = response['errorMessage'] bot.sendMessage(chat_id, translateText) return
def GetSidoAirData(self, sido): serviceKey = main.botConfig.GetDataServiceKey() url = "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty?numOfRows=200&pageNo=1&sidoName=" + sido + "&ver=1.0&ServiceKey=" + serviceKey + "&_returnType=json" try: r = requests.get(url) if r.status_code != 200: return -1, -1, -1, '' response = r.json() items = response['list'] khai_value = 0 khai_count = 0 pm10_value = 0 pm10_count = 0 pm25_value = 0 pm25_count = 0 dataTime = items[0]['dataTime'] for item in items: if item.get('khaiValue') != None and self.isNum(item['khaiValue']): khai = int(item['khaiValue']) khai_value += khai khai_count += 1 if item.get('pm10Value') != None and self.isNum(item['pm10Value']): pm10 = int(item['pm10Value']) pm10_value += pm10 pm10_count += 1 if item.get('pm25Value') != None and self.isNum(item['pm25Value']): pm25 = int(item['pm25Value']) pm25_value += pm25 pm25_count += 1 except requests.exceptions.RequestException as e: log.info('Request Exception : %s', e) return -1, -1, -1, '' except: self.PrintException() return -1, -1, -1, '' return int(khai_value / khai_count), int(pm10_value / pm10_count), int(pm25_value / pm25_count), dataTime
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 log.info("Callback Query - query Id:'%s', cb_type:'%s', From:%d, chat_id:%d, query:'%s'", query_id, query_type, from_id, chat_id, query_data) self.CBMgr.CBParser(query_data, self, chat_id)
def AddDevice(self, MAC, DeviceName, BroadCastAddr="192.168.0.1"): # insert into wol_device (MAC, DEVNAME, BROADCAST_ADDR, REG_DATE) values('28:92:4A:30:6E:A7', 'test', '192.168.0.1' datetime('now','localtime')); db = sqlite3.connect(self.db_path) cursor = db.cursor() query = ( u"INSERT INTO WOL_DEVICE (MAC, DEVNAME, BROADCAST_ADDR, REG_DATE) VALUES('%s', '%s', '%s', datetime('now','localtime'));" % (MAC, DeviceName, BroadCastAddr) ) cursor.execute(query) db.commit() cursor.close() db.close() log.info("WOL Add Device, MAC[%s], Name[%s], Broadcast[%s]", MAC, DeviceName, BroadCastAddr) 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 CreateMonitorProcedure(self): 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('CreateMonitorProcedure Success') else: log.info('CreateMonitorProcedure Fail') return
def WOLDeviceCount(self): db = sqlite3.connect(self.db_path) cursor = db.cursor() query = "SELECT COUNT(MAC) FROM WOL_DEVICE;" cursor.execute(query) row = cursor.fetchone() count = row[0] cursor.close() db.close() log.info("WOL Device Count[%d]", count) return count
def CreateMonitorTable(self): query = """CREATE TABLE btdownload_event( task_id integer NOT NULL, username character varying(128), filename text, status integer, total_size bigint, isread integer, create_time date );""" if self.db_exec(query) : log.info('CreateMonitorTable Success') else: log.info('CreateMonitorTable Fail') return
def __init__(self): # DB fail if not exists - Create DB db = sqlite3.connect(self.db_path) cursor = db.cursor() query = """CREATE TABLE IF NOT EXISTS "TG_CB" ( `ID` TEXT NOT NULL, `TYPE` INTEGER NOT NULL, `VALUE` TEXT NOT NULL, PRIMARY KEY(ID) );""" log.info('TorKim Query : %s', query) cursor.execute(query) db.commit() cursor.close() db.close()
def SearchDocument(self, keyword, bot, chat_id): url = self.namuwiki_url + keyword r = requests.get(url) if r.status_code != 200: msg = "*'" + keyword + "'*" + '에 해당하는 문서를 찾지 못하였습니다.' #sender.sendMessage(msg) sender.sendMessage(msg, parse_mode='Markdown') return #wikiContent = (r.te[:75] + '..') if len(data) > 75 else data bot.sendMessage(chat_id, r.url) log.info('NamuWiki Found Document : %s', keyword) return
def group_command_handler(self, command, chat_id): log.info("Group Command Handler, Cmd:'%s', chat_id:'%s'", command, chat_id) groupCmd = command idx = groupCmd.rfind('@') if idx >= 0: groupCmd = groupCmd[:idx] if groupCmd == '/cancel': self.sendMessage(chat_id, '모드를 취소합니다', reply_markup=self.hide_keyboard) self.cur_mode = '' return # cur_mode 가 존재 한다면 가장 앞의 / 기호 제거 if self.cur_mode: groupCmd = groupCmd[1:] self.command_handler(groupCmd, chat_id, True) return groupCmd
def GetWeatherSummary(self, LocalName): log.info('GetWeatherSummary : ' + LocalName) sumurl = self.GetSummaryUrl(LocalName) log.debug('url : ' + sumurl) data = urllib.urlopen(sumurl) soup = BeautifulSoup.BeautifulSoup(data) week_summary = soup.findAll('table', attrs={'class':'table_announcementtime'}) today_summary = LocalName + ' ' + week_summary[0].find('caption').text + '\n' + \ week_summary[0].find('p').text.replace(u'다.', u'다.\n') + '\n' tomorrow_summary = LocalName + ' ' + week_summary[1].find('caption').text + '\n' + \ week_summary[1].find('p').text.replace(u'다.', u'다.\n') + '\n' soup.close() data.close() return today_summary, tomorrow_summary
def GetTypeValue(self, unique_id): query = "SELECT * FROM TG_CB WHERE ID = '%s';" % (unique_id) log.info("CBParser - callback_db_path:'%s', query:'%s'", self.callback_db_path, query) db = sqlite3.connect(self.callback_db_path) cursor = db.cursor() cursor.execute(query) row = cursor.fetchone() if row == None or len(row) == 0: cursor.close() db.close() log.info("GetTypeValue | Query result is 0") return None, '' type = row[1] value = row[2] log.info("GetTypeValue - type:%d, value:'%s'", type, value) cursor.close() db.close() return type, value