forked from sJohnsonStoever/redditPostArchiver
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.py
More file actions
75 lines (73 loc) · 3.18 KB
/
Copy pathutils.py
File metadata and controls
75 lines (73 loc) · 3.18 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
#!/usr/bin/env python3
from urlextract import URLExtract
def extract_urls(body):
urlset = set()
extractor = URLExtract()
excluded = ['.id', '.you', '.lol', '.like', '.now', '.my', '.love', '.phone', '.how', '.post', '.me', '.got',
'.hot', '.im', '.best']
try:
generatedUrls = extractor.gen_urls(body)
for url in generatedUrls:
if len(url) < 5 or '.' not in url:
continue
if url.count('http') == 1:
try:
url = url.split('http')[1]
url = 'http{}'.format(url)
if '(' in url:
try:
rurl = url.split('(')
if extractor.has_urls(rurl[1]):
url = rurl[1]
elif extractor.has_urls(rurl[0]):
url = rurl[0]
else:
continue
if ')' in url:
try:
lurl = url.split(')')
if extractor.has_urls(lurl[0]):
url = lurl[0]
elif extractor.has_urls(lurl[1]):
url = lurl[1]
else:
continue
sem = 0
for suffix in excluded:
if url.endswith(suffix):
sem = 1
if sem == 1:
continue
# """
if '[IMG]' in url:
try:
url = url.split('[IMG]')[1]
except IndexError:
pass
if '[/IMG]' in url:
try:
url = url.split('[/IMG]')[0]
except IndexError:
pass
if url.endswith('?fb'):
url = url.replace('?fb', '')
if url.endswith('?noredirect'):
url = url.replace('?noredirect', '')
elif url.endswith('_d.jpg?maxwidth=640&shape=thumb&fidelity=medium'):
url = url.replace('_d.jpg?maxwidth=640&shape=thumb&fidelity=medium', '')
elif url.endswith('?s=sms'):
url = url.replace('?s=sms', '')
if '//m.imgur.com' in url:
url = url.replace('//m.imgur.com', '//imgur.com')
if url.startswith('https://thumbs.gfycat.com/'):
url = url.replace('https://thumbs.gfycat.com/', 'https://gfycat.com/')
if url.endswith('-size_restricted.gif'):
url = url.replace('-size_restricted.gif', '')
# """
urlset.add(url)
return urlset
except IndexError as e:
raise e
print("While generating urls, an AttributeError (specifically {e}) was raised. Moving on without extracting urls for now. This is likely an error with the python library URLExtract (https://github.com/lipoja/URLExtract). The issue has been fixed (see issue fix here: https://github.com/lipoja/URLExtract/commit/aa51f52e77b104932c49fb14882c632f12b6e940) but is has not included in the most recent release. Please install the version from GitHub to fix this issue (eg. pip3 install git+https://github.com/lipoja/URLExtract.git".format(e=e))
finally:
return urlset # which is empty