-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdilbert.py
More file actions
34 lines (26 loc) · 1019 Bytes
/
dilbert.py
File metadata and controls
34 lines (26 loc) · 1019 Bytes
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
from bs4 import BeautifulSoup
import datetime
import requests
import sys
import os
from redis import Redis
from rq import Queue
from worker import download_image
base = datetime.datetime(2017, 1, 1, 0, 0)
#end = datetime.datetime(1993, 1, 1, 0, 0)
end = datetime.datetime.today()
date_list = [base + datetime.timedelta(days=x) for x in range(0, (end - base).days)]
redis_conn = Redis()
q = Queue(connection=redis_conn) # no args implies the default queue
for date in date_list:
date_string = str(date.strftime('%Y-%m-%d'))
year = str(date.strftime('%Y'))
month = str(date.strftime('%m'))
path = str(year) + '/' + str(month).zfill(2)
if not os.path.isdir(path):
os.makedirs(str(year) + '/' + str(month).zfill(2))
response = requests.get('http://dilbert.com/strip/' + date_string)
soup = BeautifulSoup(response.text, 'html.parser')
body = soup.find("img", class_="img-comic")['src']
job = q.enqueue(download_image, year + '/' + month + '/' + date_string + '.gif', body)
print 'gotten ' + date_string