Created
February 26, 2014 20:03
-
-
Save marsam/9237377 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # See: http://xkcd.com/1335/ | |
| # | |
| # Usage: | |
| # $ python xkcd_now.py | |
| # $ convert -delay 10 -loop 0 *.png animation.gif | |
| from __future__ import print_function | |
| import gevent.monkey | |
| gevent.monkey.patch_socket() | |
| from os.path import basename | |
| from datetime import date, datetime, time, timedelta | |
| import requests | |
| import gevent | |
| def time_iterator(from_time, to_time, delta=timedelta(minutes=0)): | |
| today = date.today() | |
| from_date = datetime.combine(today, from_time) | |
| to_date = datetime.combine(today, to_time) | |
| from_date = from_date or datetime.now() | |
| while to_date is None or from_date <= to_date: | |
| yield from_date | |
| from_date = from_date + delta | |
| return | |
| def download_file(url, filename=None): | |
| filename = filename or basename(url) | |
| req = requests.get(url) | |
| if req.status_code != 200: | |
| print('[Error] %s: status code %s' % (url, req.status_code)) | |
| return | |
| with open(filename, 'wb') as f: | |
| print('Downloading %s...' % filename) | |
| for chunk in req.iter_content(): | |
| f.write(chunk) | |
| def xkcd_nowurl(time_): | |
| return 'http://imgs.xkcd.com/comics/now/{0}.png'.format(time_.strftime('%Hh%Mm')) | |
| def main(): | |
| from_time = time(0, 0) | |
| to_time = time(23, 59) | |
| delta = timedelta(minutes=15) | |
| gevent.joinall([gevent.spawn(download_file, xkcd_nowurl(time_)) for time_ in time_iterator(from_time, to_time, delta)]) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment