views:

234

answers:

6

on my local Windows machine, how do i write a script to download a comic strip every day and email it to myself?

such as
http://comics.com/peanuts/

Update: i know how to download the image as a file. the hard part is how to email it from my local Windows machine.

+3  A: 

A quick look on google reveals two command-line programs that you should be able to lash together in a batch file or using the scripting language of your choice.

http://www.gnu.org/software/wget/ - to do the download

http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm - to send the email

You can use the Windows Task Scheduler in control panel to make it run daily.

If you are using Python there are surely going to be convenient libraries to do the downloading/emailing parts - browse the official Python site.

Daniel Earwicker
+1  A: 

Here is perhaps the shortest distance to your goal.

It's not simple... you will need to work out how to parse out the image, and the peanuts example seems to be an unpredictable URI, so it might be more difficult than it looks to get the image itself. Your best bet will be to read the HTML of the remote webpage, write a regex to parse out the image url. Then the mail function will work fine, send an HTML email by setting the headers in the mail() function to something like:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html;";
$headers .= " charset=iso-8859-1\r\n";

With the image tags in the mail. This will let you receive emails with all your comic strips placed one after another. Your email software will do the HTTP requests to download the images for you, so you can avoid having to attach the images directly.

Antony Carthy
+7  A: 

This depends how precise you want to be. Downloading the entire web page wouldn't be too challenging - using wget, as Earwicker mentions above.

If you want the actual image file of the comic downloaded, you would need a bit more in your arsenal. In Python - because that's what I know best - I would imagine you'd need to use urllib to access the page, and then a regular expression to identify the correct part of the page. Therefore you will need to know the exact layout of the page and the absolute URL of the image.

For XKCD, for example, 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.'

You can then email it however you feel comfortable.

Gary Chambers
i guess the puzzle part is the email part
動靜能量
+2  A: 

Configure feedburner on the RSS feed, subscribe yourself to the email alerts?

thijs
+1  A: 

Emailing it is easy. Pick a library in your favorite language and read the documentation. Send it through your regular email account, or create a new free GMail account for it.

Sometimes attachments can indeed be tricky, though. If nothing else, give it a good whirl with whatever library you like most, and post another specific question about any problems you encounter.

Ian Terrell
+1  A: 

It's pretty simple if you already know how to download the file. Once its downloaded create a cronjob that emails it to yourself.

Using something like phpmailer would be the easiest way to email it

http://phpmailer.codeworxtech.com/index.php?pg=examplebmail

Galen