-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·61 lines (47 loc) · 1.35 KB
/
main.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
#!/usr/bin/env python
"""Lazy4Chan
Usage:
l4c.py LINK
l4c.py LINK DIR
"""
from bs4 import BeautifulSoup
import urllib, urllib2
from urlparse import urlparse
import re
import os
from docopt import docopt
def getSoup(link=''):
print 'Accesing ', link
page = urllib.urlopen(link)
html = page.read()
page.close()
return BeautifulSoup(html)
if __name__ == '__main__':
arguments = docopt(__doc__)
link = arguments['LINK']
try:
soup = getSoup(link)
try:
folder = arguments['DIR']
except:
folder = urlparse(link).path.split('/')[-1]
if not os.path.exists(folder):
os.makedirs(folder)
old = 0
new = 0
for span in soup.find_all('div',{'class':'fileText'}):
imgLink = re.sub('//','http://',span.find('a')['href'])
name = urlparse(imgLink).path.split('/')[-1]
try:
with open(os.path.join(folder,name)) as f:
old +=1
except:
img = urllib2.urlopen(imgLink).read()
file = open(os.path.join(folder,name),'w+')
file.write(img)
file.close
new += 1
print name
print 'Added %i files. Old files: %i. Total %i' % (new,old, new+old)
except:
print 'No valid link provided'