Skip to content

Commit

Permalink
Comic number added to comic name
Browse files Browse the repository at this point in the history
This is with the aim of placing the comics in good order so as to enable for easier
viewing when one has downloaded a lot of the comics

Signed-off-by: Victor Otieno Omondi <[email protected]>
  • Loading branch information
vickz84259 committed Jul 20, 2015
1 parent 312c026 commit 888bd26
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions XKCD/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_image_url(webpage):
return None


def download_image(url, path):
def download_image(url, path, number):
""" This function downloads and saves the image specified
by the given url.
Expand All @@ -41,7 +41,8 @@ def download_image(url, path):
print 'Downloading image {0}...'.format(os.path.basename(url))
res = get_resource(url)

with open(os.path.join(path, os.path.basename(url)), 'wb') as imageFile:
with open(os.path.join(path, number, os.path.basename(url)), 'wb') \
as imageFile:
for chunk in res.iter_content(100000):
imageFile.write(chunk)

Expand Down
6 changes: 3 additions & 3 deletions scripts/download_xkcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# download_xkcd.py - Downloads comics from xkcd.com.

__author__ = 'Victor Otieno Omondi'
__version__ = '2.1.1-alpha'
__version__ = '2.2.1'

# Standard library modules
import logging
Expand Down Expand Up @@ -98,8 +98,8 @@ def download_comic(path, start=1, end=0):
start = req.json()['num']
end = start + 1

for i in range(start, end):
url_queue.put('http://xkcd.com/{}/info.0.json'.format(i))
for number in range(start, end):
url_queue.put((number, 'http://xkcd.com/{}/info.0.json'.format(i)))

time.sleep(5)

Expand Down
8 changes: 4 additions & 4 deletions scripts/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def __init__(self, web_queue, image_queue):
def run(self):
while True:
# fetch webpage url from queue
url = self.web_queue.get()
comic_number, url = self.web_queue.get()

# fetch image url from webpage
image_url = xkcd.get_image_url(url)

if image_url is not None:
# place url in download queue
self.image_queue.put(image_url)
self.image_queue.put((comic_number, image_url))

self.web_queue.task_done()

Expand All @@ -60,9 +60,9 @@ def __init__(self, path, image_queue):
def run(self):
while True:
# fetch image url from queue
url = self.image_queue.get()
comic_number, url = self.image_queue.get()

# download the image
xkcd.download_image(url, self.path)
xkcd.download_image(url, self.path, comic_number)

self.image_queue.task_done()

0 comments on commit 888bd26

Please sign in to comment.