Skip to content

Instantly share code, notes, and snippets.

@ymotongpoo
Created September 29, 2011 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymotongpoo/1250029 to your computer and use it in GitHub Desktop.
Save ymotongpoo/1250029 to your computer and use it in GitHub Desktop.
download file on basic authed site
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
from datetime import datetime, timedelta
# create Basic Authorized opener
username = "nishio"
password = "nishio"
realm = "secret bucho contents"
irclog_url = "http://pyspalog.wozozo.jp/"
basic_auth_hander = urllib2.HTTPBasicAuthHandler()
basic_auth_hander.add_password(realm=realm,
uri=irclog_url,
user=username,
passwd=password)
opener = urllib2.build_opener(basic_auth_hander)
urllib2.install_opener(opener)
# craete filename based on date
start_date = datetime(2011, 5, 11)
filename_format = "%Y.%m.%d.txt"
today = datetime.today()
one_day = timedelta(days=1)
cur_date = today
while cur_date >= start_date:
cur_date_file = cur_date.strftime(filename_format)
url = irclog_url + cur_date_file
print url
req = urllib2.Request(url)
req.add_header('Referer', irclog_url)
p = urllib2.urlopen(req)
with open(cur_date_file, 'wb') as f:
data = p.read()
f.write(data)
cur_date -= one_day
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment