forked from kaisugi/hyperdoc2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaUrls.py
174 lines (164 loc) · 6.2 KB
/
MediaUrls.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
# Author: Pierce Brooks
import os
import sys
import json
import operator
import subprocess
from pprint import pprint
from functools import reduce
from bs4 import BeautifulSoup
class Context(object):
def __init__(self, base):
self.base = base
self.urls = []
self.parents = []
self.paths = []
def get_by_path(root, items):
return reduce(operator.getitem, items, root)
def set_by_path(root, items, value):
get_by_path(root, items[:-1])[items[-1]] = value
def del_by_path(root, items):
del get_by_path(root, items[:-1])[items[-1]]
def handle(context, data, parents):
urls = []
parent = ""
for i in range(len(parents)):
parent += " "+parents[i].replace("/", "-")
parent = parent.strip().replace(" ", "_")
if (len(parent) > 0):
parent += "_"
if not (parent in context.parents):
context.parents.append(parent)
for key in data:
urls.append(key)
value = data[key]
if ("dict" in str(type(value)).lower()):
handle(context, value, parents+[key])
for url in urls:
if (url in context.urls):
continue
context.urls.append(url)
command = []
command.append("curl")
command.append("https://tvtropes.org/pmwiki/pmwiki.php/"+url)
command.append("--output")
command.append(os.path.join(context.base, parent+url.replace("/", "-")+".html"))
print(str(command))
if (os.path.exists(command[len(command)-1])):
continue
output = subprocess.check_output(command)
print(output.decode())
def run(base, target, tropes):
descriptor = open(target, "r")
content = descriptor.read()
descriptor.close()
map = {}
data = json.loads(content)
context = Context(base)
loop = True
while (loop):
loop = False
pprint(data)
handle(context, data, [])
data = {}
for root, folders, files in os.walk(context.base):
for name in files:
if not (".html" in name):
continue
path = os.path.join(root, name)
if (path in context.paths):
continue
context.paths.append(path)
name = name.replace(".html", "").replace("-", "/")
base = name
if not (base in map):
map[base] = []
parents = []
if ("_" in name):
parents = name.split("_")
for i in range(len(parents)):
parent = parents[i]
if ("/" in parent):
parent = parent.split("/")
parents[i] = parent[len(parent)-1]
name = parents[len(parents)-1]
parents = parents[:(len(parents)-1)]
else:
if ("/" in name):
name = name.split("/")
name = name[len(name)-1]
print(path)
descriptor = open(path, encoding="raw_unicode_escape")
content = descriptor.read()
descriptor.close()
if (len(content) == 0):
continue
soup = BeautifulSoup(content)
links = soup.find_all("a", recursive=True, href=True)
urls = []
for link in links:
url = link["href"]
if not ("/pmwiki/pmwiki.php/" in url):
continue
url = url.replace("/pmwiki/pmwiki.php/", "")
if not ("/" in url):
continue
if (url in tropes):
continue
if (url in context.urls):
continue
if not (url.startswith(name)):
check = False
for parent in parents:
if ((url.startswith(parent)) or ((parent.endswith("s")) and (parent[:(len(parent)-1)] == url.split("/")[0]))):
check = True
break
if not (check):
temp = url.split("/")
if (temp[0] == "Main"):
temp = temp[len(temp)-1]
if not (("?" in temp) or ("#" in temp)):
temps = parents+[name]
for parent in temps:
if (temp.startswith(parent)):
temps = base.split("_")+[url]
for i in range(len(temps)):
try:
temp = get_by_path(data, temps[:(i+1)])
except:
set_by_path(data, temps[:(i+1)], {})
set_by_path(data, temps, {})
print(url)
loop = True
break
continue
context.urls.append(url)
urls.append(url)
#print(url)
map[base] = map[base]+urls
#break
break
descriptor = open(target+".json", "w")
descriptor.write(json.dumps(map))
descriptor.close()
return 0
def launch(arguments):
if (len(arguments) < 3):
return False
target = arguments[1]
tropes = arguments[2]
descriptor = open(tropes, "r")
tropes = descriptor.read()
descriptor.close()
tropes = json.loads(tropes)
base = os.path.dirname(arguments[0])
base = os.path.join(base, target+".d")
if not (os.path.exists(base)):
os.makedirs(base)
result = run(base, target, tropes)
print(str(result))
if not (result == 0):
return False
return True
if (__name__ == "__main__"):
print(str(launch(sys.argv)))