-
Notifications
You must be signed in to change notification settings - Fork 5
/
torrent.py
executable file
·169 lines (121 loc) · 6.24 KB
/
torrent.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
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
#-*- coding: utf-8 -*-
import os
import sys
import time
import feedparser
import dsdownload
import main
from LogManager import log
# pip install psycopg2
class TorrentManager(object):
"""description of class"""
rssUrl = """https://torrentkim10.net/bbs/rss.php?k="""
navi = feedparser.FeedParserDict()
dsm_id = main.botConfig.GetDsmId()
def tor_search(self, keyword, bot, chat_id, is_group_chat=False):
bot.sendMessage(chat_id, '토렌트 검색 중...')
self.navi = feedparser.parse(self.rssUrl+keyword.encode('utf-8'))
outList = []
if not self.navi.entries:
bot.sendMessage(chat_id, '검색결과가 없습니다. 다시 입력하세요.')
#print('검색결과가 없습니다. 다시 입력하세요.')
return False
title_list = ''
for (i,entry) in enumerate(self.navi.entries):
if i == 10: break
if is_group_chat:
title = '/' + str(i+1) + ". " + entry.title
else:
title = str(i+1) + ". " + entry.title
templist = []
templist.append(title)
outList.append(templist)
title_list += title
title_list += '\n'
show_keyboard = {'keyboard': outList, 'resize_keyboard': True}
bot.sendMessage(chat_id, '받을 Torrent를 선택 하세요')
bot.sendMessage(chat_id, title_list, reply_markup=show_keyboard)
return True
def torrent_download(self, selected, bot, chat_id):
ds = dsdownload.dsdownload()
log.info('DS Torrent download')
if type(selected) is int == False:
log.info('Torrent Download, Selected number type is not int!')
bot.sendMessage(chat_id, u'입력값 오류')
return False
ret, items = ds.db_query("SELECT * FROM USER_SETTING WHERE username='" + self.dsm_id + "';")
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)
index = int(selected.split('.')[0]) - 1
magnet = self.navi.entries[index].link
log.info("DS Download, user:'%s', DownloadPath:'%s', Magnet Link : '%s'", ds_user, sh_dir.decode('utf-8'), magnet)
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, magnet, 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 = self.navi.entries[index].title + u' 다운로드를 시작합니다'
bot.sendMessage(chat_id, msg, reply_markup=hide_keyboard)
self.navi.clear()
else:
bot.sendMessage(chat_id, u'다운로드 실패', reply_markup=hide_keyboard)
return True
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='" + self.dsm_id + "';")
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:'%s', 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 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='" + self.dsm_id + "';")
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'))
try:
filename = filename.encode('utf-8')
except UnicodeDecodeError:
log.info("ReceiveTorrentFile Exception : it was not a ascii-encoded unicode string")
filename = fileid + '.torrent'
except UnicodeEncodeError:
log.info("ReceiveTorrentFile Exception : It may have been an ascii-encoded unicode string")
filename = fileid + '.torrent'
except:
log.info("ReceiveTorrentFile Exception : it is wrong string")
filename = fileid + '.torrent'
bot.download_file(fileid, watch_dir + file_name)
log.debug('downoad success')
log.info('[%s] download success', filename)
hide_keyboard = {'hide_keyboard': True}
#msg = file_name.decode('utf-8') + ' 파일을 ' + watch_dir.decode('utf-8') + ' 경로에 다운로드 하였습니다';
#msg = "%s 파일을\n'%s'\n경로에 다운로드 하였습니다" % (filename.encode('utf-8'), watch_dir.encode('utf-8'))
msg = 'Torrent 파일을 Watch 경로에 다운로드 하였습니다\n잠시 후 다운로드가 시작됩니다'
bot.sendMessage(chat_id, msg, reply_markup=hide_keyboard)
return True