forked from acidpop/xpebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfreeca.py
249 lines (181 loc) · 8.28 KB
/
tfreeca.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
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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os
import sys
import urllib2
import requests
import BeautifulSoup
import base64
import urlparse
import sqlite3
import linecache
import hashlib
import traceback
from LogManager import log
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
import main
class tfreeca(object):
"""description of class"""
db_path = main.botConfig.GetExecutePath() + "/cbbot.db"
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('tfreeca Query : %s', query)
cursor.execute(query)
db.commit()
cursor.close()
db.close()
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 SearchTfreeca(self, keyword, torType=3, max_count=20):
if torType < 3 or torType > 4:
log.info('Search tfreeca, Unknown Type:%d', torType)
return False, None, None
try:
keyword = keyword.replace(" ", "+")
torList = self.GetTorrentList(keyword.encode('utf-8'), torType)
sortTorList = sorted(torList.items(), reverse=True)
if not sortTorList:
log.info("Search tfreeca not found, keyword:%s", keyword)
return False, None, None
inline_keyboard = self.MakeTorrentInlineKeyboard(sortTorList, torType, max_count)
outTitleList = self.MakeTorrentTitleList(sortTorList, max_count)
if outTitleList == None:
log.info("Get Title List fail")
return False, None, None
except:
self.PrintException()
return False, None, None
return True, inline_keyboard, outTitleList
def GetTitle(self, board, keyword, torType=3):
if torType == 3:
typeMap = {"tdrama": 3, "tent" : 4, "tv" : 5}
else:
typeMap = {"tdrama": 6, "tent" : 7, "tv" : 8}
url = "http://www.tfreeca22.com/board.php?b_id=" + board + "&mode=list&sc=" + keyword + "&x=0&y=0"
boardType = typeMap.get(board, 999)
if boardType == 999:
log.info("tfreeca get title unknown board")
return None
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'), ('Cookie', 'uuoobe=on;')]
data = opener.open(url)
sp = BeautifulSoup.BeautifulSoup(data)
bList = sp.find('table', attrs={'class':'b_list'})
if bList == None:
return None
torTRs = bList.findAll('td', attrs={'class':'subject'})
titleList = dict()
listValue = list()
for item in torTRs:
alink = item.find('a', attrs={'class':['stitle1','stitle2', 'stitle3', 'stitle4', 'stitle5', 'stitle6']})
torUrl = "http://www.tfreeca22.com/" + alink['href']
torTitle = alink.text
par = urlparse.parse_qs(urlparse.urlparse(torUrl).query)
torId = str(par["id"][0])
titleList[torId] = list()
titleList[torId].append(torTitle)
titleList[torId].append(boardType)
return titleList
def GetTorrentList(self, keyword, torType=3):
boardList = ['tdrama', 'tent', 'tv']
torList = dict()
for item in boardList:
tempList = self.GetTitle(item, keyword, torType)
if tempList == None:
continue
torList.update(tempList)
return torList
def GetTorrentFile(self, torType, id):
typeMap = {3 : "tdrama",
4 : "tent",
5 : "tv",
6 : "tdrama",
7 : "tent",
8 : "tv"}
board = typeMap.get(torType, "unknown")
if board == "unknown":
log.info('tfreeca get torrent fail, unknown type:%d', torType)
return False, ''
torId = str(id)
torrentEnc = base64.encodestring(board + '|' + torId + '|')
torrentUrl = "http://file.filetender.com/Execdownload.php?link=" + torrentEnc
torrentName = torId + ".torrent"
try:
r = requests.get(torrentUrl, stream=True, headers={'User-agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
'Connection' : 'keep-alive',
'Content-Type' : 'application/x-bittorrent',
'pragma' : 'no-cache',
'expires' : '0',
'Content-Disposition' : 'attachment; filename=\"' + torId + '.torrent\"',
'content-description' : 'php generated data'})
with open(torrentName, 'wb') as f:
chunks = enumerate(r.iter_content(chunk_size=4096))
for index, chunk in chunks:
if chunk:
f.write(chunk)
f.flush()
except requests.exceptions.RequestException as e:
log.error('tfreeca GetTorrentFile Fail, Request Exception : %s', e)
log.error("tfreeca GetTorrentfile Exception : %s", traceback.format_exc())
return False, ''
except:
log.error("tfreeca Get Torrent File Fail, url:'%s'", bbsUrl)
log.error("tfreeca GetTorrentfile Exception : %s", traceback.format_exc())
return False, ''
return True, torrentName.encode('utf-8')
def MakeTorrentInlineKeyboard(self, torList, cmdType=3, max_count=10):
# sqlite ID, Type, Value
db = sqlite3.connect(self.db_path)
cursor = db.cursor()
inline_keyboard_list = list()
idx = 0
#for k, v in torList.iteritems():
for item in torList:
if idx >= max_count:
break
# 고유키는 Url 링크를 SHA-1 으로 처리 한 값으로 한다.
#torType = v[1]
torType = item[1][1]
unique_id = hashlib.sha1(item[0].encode('utf-8')+str(torType)).hexdigest()
query = "INSERT OR REPLACE INTO TG_CB VALUES('%s', %d, '%s');" % (unique_id, torType, item[0])
log.info('Tfreeca Query : %s', query)
cursor.execute(query)
if cmdType == 3:
itemText = str(idx+1) + '. ' + item[1][0].encode('utf-8')
elif cmdType == 4:
itemText = 'DOWNLOAD:[' + item[1][0].encode('utf-8')
callbackText = "%s" % (unique_id)
tempList = list()
tempList.append(InlineKeyboardButton(text=itemText, callback_data=callbackText))
inline_keyboard_list.append(tempList)
idx = idx + 1
db.commit()
cursor.close()
db.close()
return InlineKeyboardMarkup(inline_keyboard=inline_keyboard_list)
def MakeTorrentTitleList(self, torList, max_count=10):
outList = ''
idx = 0
#for k, v in torList.iteritems():
for item in torList:
if idx >= max_count:
break
itemText = str(idx+1) +'. ' + item[1][0]
outList += itemText
outList += '\n'
idx = idx + 1
return outList