-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
194 lines (178 loc) · 6.28 KB
/
app.py
File metadata and controls
194 lines (178 loc) · 6.28 KB
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
import configparser
import os
import re
import time
from tqdm import tqdm
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, ElementNotVisibleException, WebDriverException
def sync():
config = configparser.ConfigParser()
config.read("config.ini")
shows = config["settings"]["shows"].split(",")
all_show_dir = config["settings"]["show_dir"]
print("Shows set to sync:")
print(shows)
print("Directory for shows")
print(all_show_dir, "\n")
if not os.path.isdir(all_show_dir):
os.mkdir(all_show_dir)
# Update shows
dirs = os.listdir(all_show_dir)
print("Current Show Directories:")
print(dirs)
print("Updating Shows:\n")
for show in shows:
print("Updating " + show)
# Create any missing directories
if show not in dirs:
os.mkdir(os.path.join(all_show_dir, show))
show_dir = os.path.join(all_show_dir, show)
episode_files = os.listdir(show_dir)
parsed_files = []
for file in episode_files:
n = re.findall(" [0-9]+[.]*[0-9]* ", file)
m = []
for o in n:
m.append(o.replace(" ", ""))
print("Found episode %s in the directory." % m)
parsed_files += m
show_url = config["settings"]["base_url"] + "/" + show
episodes = get_episodes(show_url)
episodes_to_fetch = []
for e in episodes:
if e not in parsed_files:
episodes_to_fetch.append(e)
print("Going to attempt to find episode %s." %e)
links_to_add = get_magnet_links(show_url, episodes_to_fetch)
for link in tqdm(links_to_add):
add_to_deluge(link, os.path.join(all_show_dir, show))
def add_to_deluge(link, save_path):
def wait():
wait_time = 0.5
time.sleep(wait_time)
config = configparser.ConfigParser()
config.read("config.ini")
url = config["deluge"]["url"]
port = config["deluge"]["port"]
username = config["deluge"]["username"]
password = config["deluge"]["password"]
chromedriver_path = config["settings"]["chromedriver_path"]
driver = webdriver.Chrome(chromedriver_path)
# driver.get(url+":"+port)
driver.get("localhost:8112")
wait()
password_box = driver.find_element_by_name("password")
password_box.click()
wait()
password_box.send_keys(password)
login_button = driver.find_element_by_id("ext-gen155")
login_button.click()
# daemon_select = driver.find_element_by_id("ext-gen225")
# daemon_select.click()
# connect_button = driver.find_element_by_id("ext-gen213")
# connect_button.click()
wait()
add_button = driver.find_element_by_id("ext-gen49")
add_button.click()
wait()
url_button = driver.find_element_by_class_name("icon-add-url")
url_button.click()
url_textbox = driver.find_element_by_id("url")
url_textbox.send_keys(link)
add_magnet = driver.find_elements_by_class_name("x-btn-noicon")
for button in add_magnet:
try:
button.click()
except ElementNotVisibleException:
pass
except WebDriverException:
pass
while True:
try:
download_path = driver.find_element_by_id("ext-comp-1084")
break
except NoSuchElementException:
wait()
download_path.clear()
download_path.send_keys(save_path)
add_torrent = driver.find_element_by_id("ext-comp-1071")
add_torrent.click()
driver.close()
def get_magnet_links(show_url, episodes):
wait_time = 0.15
episodes = [e.replace(".", "-") for e in episodes]
config = configparser.ConfigParser()
config.read("config.ini")
chromedriver_path = config["settings"]["chromedriver_path"]
driver = webdriver.Chrome(
chromedriver_path
) # Optional argument, if not specified will search path.
driver.get(show_url)
while True:
try:
show_more = driver.find_element_by_link_text("Show more ▼")
show_more.click()
time.sleep(wait_time)
except:
break
episode_buttons = driver.find_elements_by_class_name("rls-label")
for button in episode_buttons:
button.click()
time.sleep(wait_time)
magnets = []
episode_container = driver.find_element_by_class_name("episode-container")
for ep in episodes:
try:
link_container = driver.find_element_by_id(str(ep) + "-1080p")
link_element = link_container.find_element_by_link_text("Magnet")
magnets.append(link_element.get_attribute("href"))
except:
print("Couldn't find episode " + str(ep))
print(magnets)
return magnets
driver.close()
def get_episodes(show_url):
wait_time = 0.2
config = configparser.ConfigParser()
config.read("config.ini")
chromedriver_path = config["settings"]["chromedriver_path"]
driver = webdriver.Chrome(
chromedriver_path
) # Optional argument, if not specified will search path.
driver.get(show_url)
# Check for show more, expand if found
while True:
try:
show_more = driver.find_element_by_link_text("Show more ▼")
show_more.click()
time.sleep(wait_time)
except:
break
episode_container = driver.find_element_by_class_name("episode-container")
episodes = re.findall("( [0-9]+[.]*[0-9]* )\w+", episode_container.text)
episodes = [e.replace(" ", "") for e in episodes]
driver.close()
return episodes
def set_config():
config = configparser.ConfigParser()
show_dir = os.path.join("D:\\", "Plex", "Anime", "hd_bot_example_dir")
chromedriver_location = os.path.join(
os.path.expanduser("~"), "PycharmProjects", "hd_bot", "chromedriver"
)
config["settings"] = {
"base_url": "https://horriblesubs.info/shows/",
"shows": "one-punch-man,goblin-slayer,tensei-shitara-slime-datta-ken",
"show_dir": show_dir,
"chromedriver_path": chromedriver_location,
}
config["deluge"] = {
"url": "127.0.0.1",
"port": "8112",
"username": "admin",
"password": "deluge",
}
with open("config.ini", "w") as configfile:
config.write(configfile)
if __name__ == "__main__":
set_config()
sync()