-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhtypes.py
279 lines (235 loc) · 8.47 KB
/
htypes.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import typing
from typing import Any, Dict, Optional, Tuple
import contextlib
from enum import IntEnum
if typing.TYPE_CHECKING:
from adapter.adapter import Adapter
from psycopg2.extensions import AsIs, register_adapter
class FicType(IntEnum):
broken = 0
manual = 1
ff_net = 2
dummy = 3
ao3 = 4
hpfanficarchive = 5
fictionalley = 6
fanficauthors = 7
portkeyarchive = 8
siye = 9
fictionpress = 10
fictionhunt = 11
spacebattles = 12
sufficientvelocity = 13
questionablequesting = 14
harrypotterfanfiction = 15
parahumans = 16
adultfanfiction = 17
fanficsme = 18
royalroadl = 19
wavesarisen = 20
sugarquill = 21
bulbagarden = 22
thefanfictionforum = 23
fanficparadisesfw = 24
fanficparadisensfw = 25
wanderinginn = 26
def adaptFicType(ftype: FicType) -> AsIs:
return AsIs(int(ftype))
register_adapter(FicType, adaptFicType)
# map FicType => Adapter
adapters: Dict[FicType, Optional["Adapter"]] = {
FicType.broken: None,
}
def getAdapter(ficType: FicType) -> "Adapter":
adapter = adapters[ficType]
if adapter is not None:
return adapter
raise Exception(f"missing adapter for {ficType}")
class FicId:
# TODO: is this cid or localChapterId?
def __init__(
self,
ftype: FicType,
lid: str,
cid: Optional[int] = None,
ambiguous: bool = True,
):
self.sourceId = ftype
self.localId = lid
self.chapterId = cid
self.ambiguous = ambiguous
def __key(self) -> Tuple[FicType, str, Optional[int], bool]:
return (self.sourceId, self.localId, self.chapterId, self.ambiguous)
def __eq__(self, other: Any) -> bool:
return isinstance(other, type(self)) and self.__key() == other.__key()
def __hash__(self) -> int:
return hash(self.__key())
@staticmethod
def parse(ident: str) -> "FicId":
ident = ident.strip()
res = FicId.tryParse(ident)
if res is None:
raise Exception(f"unable to parse ident: {ident}")
return res
@staticmethod
def cleanupIdent(ident: str) -> str:
import urllib.parse
# attempt to map various generic unsupported urls which redirect to
# supported urls or are equivalent to supported urls to their supported
# version (such as google or facebook redirect links)
ident = ident.rstrip(":,")
ident = ident.replace("http:/", "http://").replace("https:/", "https://")
while ident.find("///") >= 0:
ident = ident.replace("///", "//")
# FIXME this needs to be more general :/
if ident.find("//parahumans.wordpress.com/") >= 0:
ident = "https://parahumans.wordpress.com"
if ident.find("fanfiction.ws/") >= 0:
ident = ident.replace("fanfiction.ws/", "fanfiction.net/")
if ident.find("fanfiction.de/") >= 0:
ident = ident.replace("fanfiction.de/", "fanfiction.net/")
if ident.lower().startswith("https://href.li/?"):
ident = ident[len("https://href.li/?") :]
if ident.lower().startswith("http://web.archive.org/"):
ident = "https://" + ident[len("http://") :]
if ident.lower().startswith("https://web.archive.org/web/"):
ident = "/".join(ident.split("/")[5:])
# if this is a google redirect url, extract the target
if (
ident.find("//") >= 0
and ident.find("google") >= 0
and ident.find("/url?") >= 0
and ident.find("url=") >= 0
):
with contextlib.suppress(Exception):
o = urllib.parse.urlparse(ident)
q = urllib.parse.parse_qs(o.query)
ident = q["url"][0]
if (
ident.find("//") >= 0
and ident.find("google") >= 0
and ident.find("/url?") >= 0
and ident.find("q=") >= 0
):
with contextlib.suppress(Exception):
o = urllib.parse.urlparse(ident)
q = urllib.parse.parse_qs(o.query)
ident = q["q"][0]
# if this is a facebook redirect url, extract the target
if (
ident.find("//") >= 0
and ident.find("facebook.com/l.php") >= 0
and ident.find("?") >= 0
and ident.find("u=") >= 0
):
with contextlib.suppress(Exception):
o = urllib.parse.urlparse(ident)
q = urllib.parse.parse_qs(o.query)
ident = q["u"][0]
return ident
@staticmethod
def tryParse(ident: str) -> Optional["FicId"]:
ident = FicId.cleanupIdent(ident)
# try parsing as original case
fid = FicId.__tryParse(ident)
if fid is not None:
return fid
# fallback to trying to parse as lowercase
ident = ident.lower()
return FicId.__tryParse(ident)
@staticmethod
def __tryParse(ident: str) -> Optional["FicId"]:
if len(ident.strip()) < 1:
return None
# strip view-source from potential urls
if ident.startswith("view-source:http"):
ident = ident[len("view-source:") :]
# guess url next
if ident.startswith("http"):
return FicId.tryParseUrl(ident)
# check for link{site}(id) style idents
for ftype in adapters:
a = adapters[ftype]
if a is None:
continue
if a.botLinkSuffix is None:
continue
pre = f"link{a.botLinkSuffix}("
if ident.startswith(pre) and ident.endswith(")"):
mid = ident[len(pre) : -1]
if not mid.isnumeric():
return FicId.tryParse(ident)
return FicId(ftype, mid, ambiguous=False)
# maybe it's an actual story id
from store import Fic
parts = ident.split("/")
if parts[0].isnumeric():
fic = Fic.get((int(parts[0]),))
if fic is not None:
fid = fic.fid()
if len(parts) == 2 and parts[1].isnumeric():
fid.chapterId = int(parts[1])
fid.ambiguous = False
return fid
# or maybe it's a url id...
potential = Fic.select({"urlId": parts[0]})
if len(potential) == 1:
fid = potential[0].fid()
if len(parts) == 2 and parts[1].isnumeric():
fid.chapterId = int(parts[1])
fid.ambiguous = False
return fid
# assume numeric is ffnet
if ident.isnumeric():
return FicId(FicType.ff_net, ident)
# guess story/chapter on ffnet
if len(parts) == 2 and parts[1].isnumeric():
cid = int(parts[1])
return FicId(FicType.ff_net, parts[0], cid, ambiguous=False)
# try prepending https protocol
if ident.find("://") < 0:
ident = "https://" + ident
return FicId.tryParseUrl(ident)
# just guess manual adapter
return FicId.tryParseFallback(ident)
@staticmethod
def tryParseUrl(url: str) -> Optional["FicId"]:
for ftype in adapters:
a = adapters[ftype]
if a is None:
continue
for fragment in a.urlFragments:
if url.find(fragment) != -1:
return a.tryParseUrl(url)
return FicId.tryParseFallback(url)
@staticmethod
def tryParseFallback(ident: str) -> Optional["FicId"]:
manualAdapter = adapters[FicType.manual]
assert manualAdapter is not None
fid = None
with contextlib.suppress(Exception):
fid = manualAdapter.tryParseUrl(ident)
if fid is None:
ident = ident + "/" if not ident.endswith("/") else ident.rstrip("/")
with contextlib.suppress(Exception):
fid = manualAdapter.tryParseUrl(ident)
return fid
@staticmethod
def guessFicType(ident: str) -> Tuple[Optional[FicType], Optional[str]]:
for ftype in adapters:
a = adapters[ftype]
if a is None:
continue
for fragment in a.urlFragments:
if ident.find(fragment) != -1:
return (ftype, fragment)
return (None, None)
@staticmethod
def help() -> str:
return "\n".join(
[
"FicId: #FFNetId",
" #FFNetId/#ChapterId",
" <story url>",
]
)