-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
38 lines (32 loc) · 1.13 KB
/
Copy pathutils.py
File metadata and controls
38 lines (32 loc) · 1.13 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
# -*- coding: utf-8 -*-
import collections
from os.path import isfile
import mechanize
from lxml.html.clean import Cleaner
from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=['./templates'],
module_directory='/tmp/mako_modules')
def render(template_path, **context):
template = lookup.get_template(template_path)
return template.render_unicode(**context).encode('utf-8', 'replace')
# From answer at http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python/2158532#2158532
def flatten(l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
for sub in flatten(el):
yield sub
else:
yield el
cleaner = Cleaner()
def clean_html(html):
return cleaner.clean_html(html)
cj = mechanize.LWPCookieJar()
if isfile('.cookies'):
cj.load('.cookies')
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cj))
mechanize.install_opener(opener)
def fetch_url(url):
response = mechanize.urlopen(url)
content = response.read()
response.close()
return content