forked from kaisugi/hyperdoc2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubPages.py
118 lines (112 loc) · 3.93 KB
/
SubPages.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
# Author: Pierce Brooks
import os
import sys
import json
import inspect
import logging
import traceback
from bs4 import BeautifulSoup
def run(target, tropes):
if not (os.path.exists(target)):
return -1
map = {}
data = {}
if (os.path.exists(target+".json")):
descriptor = open(target+".json", "r")
content = descriptor.read()
descriptor.close()
data = json.loads(content)
if (os.path.exists(target+".map.json")):
descriptor = open(target+".map.json", "r")
content = descriptor.read()
descriptor.close()
data = json.loads(content)
for root, folders, files in os.walk(target):
length = len(files)
for i in range(length):
name = files[i]
try:
if not (".html" in name):
continue
path = os.path.join(root, name)
descriptor = open(path, encoding="raw_unicode_escape")
content = descriptor.read()
descriptor.close()
if (len(content) == 0):
continue
parents = name.replace(".html", "")
if ("_" in parents):
parents = parents.split("_")
else:
parents = [parents]
key = parents[len(parents)-1].replace("-", "/")
for j in range(len(parents)):
if not ("-" in parents[j]):
parents[j] = [parents[j]]
continue
parents[j] = parents[j].split("-")
if not (key in data):
data[key] = []
else:
continue
if not (key in map):
map[key] = []
else:
continue
soup = BeautifulSoup(content)
#body = soup.find("div", {"id": "main-article"})
#links = body.find_all("a", href=True, recursive=True)
links = soup.find_all("a", href=True, recursive=True)
parent = parents[len(parents)-1]
parent = parent[len(parent)-1]
for link in links:
url = link["href"]
#print(url)
if (("?" in url) or ("#" in url)):
continue
if not ("/pmwiki/pmwiki.php/" in url):
continue
url = url.replace("/pmwiki/pmwiki.php/", "")
if (url in tropes):
#print(key+" -> "+url)
map[key].append(url)
continue
if not ("/" in url):
continue
parts = url.split("/")
part = parts[len(parts)-1]
if not ((parts[0] == parent) and (part.startswith("Tropes"))):
continue
data[key].append(url)
print(url)
print(path+" ("+str(i)+" / "+str(length)+")")
except Exception as exception:
logging.error(traceback.format_exc())
except KeyboardInterrupt:
break
break
descriptor = open(target+".json", "w")
descriptor.write(json.dumps(data))
descriptor.close()
descriptor = open(target+".map.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]
if not (os.path.exists(tropes)):
return False
descriptor = open(tropes, "r")
tropes = descriptor.read()
descriptor.close()
tropes = json.loads(tropes)
result = run(target, tropes)
print(str(result))
if not (result == 0):
return False
return True
if (__name__ == "__main__"):
print(str(launch(sys.argv)))