forked from wladich/page2feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
43 lines (34 loc) · 901 Bytes
/
utils.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
# coding: utf-8
import base64
import requests
import urlparse
import time
def datauri(s):
return 'data:text/html;base64,' + base64.standard_b64encode(s)
def get_favicon(url):
url = urlparse.urlparse(url)
url = '%s://%s/favicon.ico' % (url.scheme, url.netloc)
try:
response = requests.get(url)
if response.status_code != 200:
return None
return response.content
except:
pass
return None
def now():
return int(time.time())
def open_in_browser(s, encoding=None):
import os
import webbrowser
import tempfile
handle, fn = tempfile.mkstemp(suffix='.html')
f = os.fdopen(handle, 'wb')
try:
f.write(s)
finally:
# we leak the file itself here, but we should at least close it
f.close()
url = 'file://' + fn.replace(os.path.sep, '/')
print(url)
webbrowser.open(url)