-
Notifications
You must be signed in to change notification settings - Fork 0
/
download.py
103 lines (72 loc) · 3.52 KB
/
download.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
import config
import os
import time
import requests
import re
from flask import send_file
from selenium import webdriver
from selenium.webdriver.common.by import By
from ext import chrome_cmd
class Download:
__slots__ = ["b_id", "id", "driver", "logger", "driver_error"]
def __init__(self, b_id, id):
self.b_id = b_id
self.id = id
self.logger = config.logger
try:
self.driver = webdriver.Chrome("{}/chromedriver/chromedriver_{}".format(os.getcwd(), config.os_info),
chrome_options=config.options)
self.driver_error = False
except Exception as _:
self.driver_error = True
def __del__(self):
# for handle in self.driver.window_handles:
# self.driver.switch_to.window(handle)
# self.driver.close()
self.driver.quit()
def start(self):
if self.driver_error:
return "chrome driver를 로드할 수 없습니다."
try:
view_url = "https://www.tfreeca22.com/board.php?mode=view&b_id={}&id={}".format(self.b_id, self.id)
self.logger.debug("VIEW_URL : {}".format(view_url))
self.driver.get(view_url)
chrome_cmd.wait(driver=self.driver,
element_type=By.XPATH,
element_name='/html/body/table/tbody/tr/td[2]/table[1]/tbody/tr[4]/td/a',
timeout=5,
throw_error=True)
self.logger.debug("Download Page Link Click")
down_href = self.driver.find_element_by_xpath("/html/body/table/tbody/tr/td[2]/table[1]/tbody/tr[4]/td/a")
down_href.click()
self.logger.debug("Download Click")
self.driver.switch_to.window(self.driver.window_handles[-1])
time.sleep(1)
chrome_cmd.wait(driver=self.driver,
element_type=By.XPATH,
element_name='//*[@id="Down"]/input[1]',
timeout=5,
throw_error=True)
time.sleep(1)
header_referer = config.headers.copy()
header_referer['Referer'] = self.driver.current_url
html = self.driver.page_source
form_down = self.driver.find_element_by_xpath('//*[@id="Down"]')
param_key = form_down.find_elements_by_name("key")[0].get_attribute("value")
param_ticket = form_down.find_elements_by_name("Ticket")[0].get_attribute("value")
param_randstr = form_down.find_elements_by_name("Randstr")[0].get_attribute("value")
param_userip = form_down.find_elements_by_name("UserIP")[0].get_attribute("value")
last_url = "?key={}&Ticket={}&Randstr={}&UserIP={}".format(param_key, param_ticket, param_randstr, param_userip)
regex2 = r"var newUrl = '(.*?)';"
matches2 = re.finditer(regex2, html, re.DOTALL | re.MULTILINE)
for matchNum2, match2 in enumerate(matches2, start=1):
last_url = match2.group(1) + last_url
r = requests.get(last_url, headers=header_referer, stream=True)
return send_file(r.raw,
mimetype="application/x-bittorrent",
attachment_filename=self.id + ".torrent",
as_attachment=True
)
except Exception as e:
self.logger.error(e)
return "Download Error", 500