5 | 5 |
|
6 | 6 |
from bs4 import BeautifulSoup
|
7 | 7 |
import requests
|
|
8 |
|
|
9 |
|
|
10 |
LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
|
8 | 11 |
|
9 | 12 |
|
10 | 13 |
def comply_with_terms_of_use():
|
|
39 | 42 |
for block in response.iter_content(1024):
|
40 | 43 |
f.write(block)
|
41 | 44 |
|
|
45 |
comply_with_terms_of_use()
|
42 | 46 |
return True
|
|
47 |
|
|
48 |
|
|
49 |
def load_index(filename):
|
|
50 |
index = []
|
|
51 |
with open(filename) as f:
|
|
52 |
for line in f:
|
|
53 |
index.append(line.strip())
|
|
54 |
return index
|
|
55 |
|
|
56 |
|
|
57 |
def get_random_image(index, dest_dir):
|
|
58 |
media_url = random.choice(index)
|
|
59 |
print media_url
|
|
60 |
local_filename = os.path.join(dest_dir, media_url.split(':')[-1])
|
|
61 |
get_image_from_page(media_url, local_filename)
|
|
62 |
return local_filename
|
43 | 63 |
|
44 | 64 |
|
45 | 65 |
def main(argv):
|
|
84 | 104 |
media_url = line.strip()
|
85 | 105 |
local_filename = media_url.split(':')[-1]
|
86 | 106 |
get_image_from_page(media_url, local_filename)
|
87 | |
comply_with_terms_of_use()
|
88 | 107 |
|
89 | 108 |
elif argv[1] == 'convertmany':
|
90 | 109 |
dest_dir = argv[2]
|
|
103 | 122 |
elif argv[1] == 'random':
|
104 | 123 |
count = int(argv[2])
|
105 | 124 |
dest_dir = argv[3]
|
106 | |
index_filename = argv[4]
|
107 | |
index = []
|
108 | |
with open(index_filename) as f:
|
109 | |
for line in f:
|
110 | |
index.append(line.strip())
|
|
125 |
index = load_index(argv[4])
|
111 | 126 |
for n in xrange(0, count):
|
112 | |
media_url = random.choice(index)
|
113 | |
print media_url
|
114 | |
local_filename = os.path.join(dest_dir, media_url.split(':')[-1])
|
115 | |
get_image_from_page(media_url, local_filename)
|
116 | |
comply_with_terms_of_use()
|
|
127 |
get_random_image(index, dest_dir)
|
|
128 |
|
|
129 |
elif argv[1] == 'render':
|
|
130 |
count = int(argv[2])
|
|
131 |
dest_dir = argv[3]
|
|
132 |
index = load_index(argv[4])
|
|
133 |
template = """\
|
|
134 |
<!DOCTYPE html>
|
|
135 |
<html>
|
|
136 |
<head>
|
|
137 |
<meta charset="utf-8">
|
|
138 |
<title>Lorum Ipsem Shkoo</title>
|
|
139 |
<style>
|
|
140 |
hr { page-break-before: always; }
|
|
141 |
</style>
|
|
142 |
<body>
|
|
143 |
$
|
|
144 |
</body>
|
|
145 |
</html>"""
|
|
146 |
|
|
147 |
body = ''
|
|
148 |
for x in xrange(0, count):
|
|
149 |
filename = get_random_image(index, dest_dir)
|
|
150 |
if x != 0:
|
|
151 |
body += '<hr>'
|
|
152 |
|
|
153 |
paras = ['<p>' + LOREM_IPSUM + '</p>'] * 4
|
|
154 |
paras.append('<img src="%s">' % filename)
|
|
155 |
random.shuffle(paras)
|
|
156 |
body += ''.join(paras)
|
|
157 |
|
|
158 |
template = template.replace('$', body)
|
|
159 |
with open('tmp.html', 'w') as f:
|
|
160 |
f.write(template)
|
|
161 |
import webbrowser
|
|
162 |
webbrowser.open('tmp.html')
|
117 | 163 |
|
118 | 164 |
else:
|
119 | 165 |
raise KeyError('please read the source code')
|