Php – On my local Windows machine, how do I write a script to download comics every day and email them to myself?

On my local Windows machine, how do I write a script to download comics every day and email them to myself?… here is a solution to the problem.

On my local Windows machine, how do I write a script to download comics every day and email them to myself?

On my local Windows machine, how do I write a script to download comics every day and email them to myself?

For example
http://comics.com/peanuts/

Update: I know how to download an image as a file. The hard part is how to email it from my local Windows computer.

Solution

It depends on the precision you want. Downloading the entire web page won’t be too challenging – use wget, as mentioned by Earwicker above.

If you want to download the actual image file of the manga, you need more in your arsenal. In Python – because that’s what I know best – I guess you need to use urllib to access the page and then use regular expressions to identify the right part of the page. Therefore, you need to know the exact layout of the page and the absolute URL of the image.

For example, for XKCD, the following works:

#!/usr/bin/env python

import re, urllib

root_url = 'http://xkcd.com/'
img_url  = r'http://imgs.xkcd.com/comics/'

dl_dir   = '/path/to/download/directory/'

# Open the page URL and identify comic image URL
page  = urllib.urlopen(root_url).read()
comic = re.match(r'%s[\w]+?\.( png|jpg)' % img_url, page)

# Generate the filename
fname = re.sub(img_url, '', comic)

# Download the image to the specified download directory
try:
    image = urllib.urlretrieve(comic, '%s%s' % (dl_dir, fname))
except ContentTooShortError:
    print 'Download interrupted.'
else:
    print 'Download successful.'

Then you can email it according to your preference.

Related Problems and Solutions