forked from acidpop/xpebot
/
botCBManager.py
executable file
·126 lines (90 loc) · 3.63 KB
/
botCBManager.py
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
#-*- coding: utf-8 -*-
import sys
import os
import shutil
import traceback
import time
import datetime
import feedparser
import telepot
import subprocess
import json
import sqlite3
from telepot.delegate import per_chat_id, create_open
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardHide, ForceReply
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from telepot.namedtuple import InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent
import main
import CommonUtil
import torrent
import BotHelp
import systemutil
import ExTimer
import dsdownload
import weather
import wol
import NaverApi
import rssManager
import airkorea
import namuwiki
import TorrentKim
from LogManager import log
class botCBManager(object):
callback_db_path = main.botConfig.GetExecutePath() + "/cbbot.db"
watch_dir = main.botConfig.GetTorrentWatchDir()
torKim = TorrentKim.TorrentKim()
"""description of class"""
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
def CBParser(self, unique_id, bot, chat_id):
typeFuncMap = {
1:self.TorrentKimUrlDownload,
2:self.TorrentKimGetFile
}
type, value = self.GetTypeValue(unique_id)
if type == None:
bot.sendMessage(chat_id, '검색된 데이터가 없습니다\n다시 검색하여 주세요')
return False
if typeFuncMap.get(type) == None:
msg = '지원하지 않는 타입(%d) 입니다' % (type)
bot.sendMessage(chat_id, msg)
return False
return typeFuncMap.get(type)(value, bot, chat_id)
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 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