-
Notifications
You must be signed in to change notification settings - Fork 0
/
webshots.py
47 lines (39 loc) · 1.29 KB
/
webshots.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
import subprocess
from tempfile import mktemp
from PIL import Image
from BeautifulSoup import BeautifulSoup
import urllib2
import os.path
def create_local_webshot(config, url):
bin = config.get("wkhtmltoimage", "bin")
args = config.get("wkhtmltoimage", "args")
format = config.get("wkhtmltoimage", "format")
tmp = config.get("wkhtmltoimage", "tmp")
output_path = mktemp(dir=tmp, suffix="."+format, prefix="")
command = " ".join([bin, args, "-f", format, "'%s'" % url, output_path])
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
retcode = p.poll()
result = {}
result["url"] = url
result["path"] = output_path
if retcode != 0 or os.path.getsize(output_path) < 5000:
result["stdout"] = stdout
result["stderr"] = stderr
result["retcode"] = retcode
result["status"] = "failed"
else:
im = Image.open(output_path)
if im.size[0] < 100 or im.size[1] < 100:
result["status"] = "failed"
result["message"] = "Image too small"
else:
soup = BeautifulSoup(urllib2.urlopen(url))
result["title"] = soup.title.string
result["status"] = "succeeded"
return result
if __name__ == "__main__":
from main import get_config
import sys
config = get_config()
print create_local_webshot(config, sys.argv[1])