Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 978
nonopolarity Avatar asked May 26 '09 08:05

nonopolarity


2 Answers

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.

like image 34
Daniel Earwicker Avatar answered Oct 04 '22 01:10

Daniel Earwicker


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.

like image 80
Gary Chambers Avatar answered Oct 04 '22 01:10

Gary Chambers