Last active
March 26, 2016 06:09
-
-
Save dumpmycode/0636694b76a1e4d948ec to your computer and use it in GitHub Desktop.
Google Python Class - logpuzzle.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Copyright 2010 Google Inc. | |
# Licensed under the Apache License, Version 2.0 | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# Google's Python Class | |
# http://code.google.com/edu/languages/google-python-class/ | |
import os | |
import re | |
import sys | |
import urllib2 | |
import time | |
"""Logpuzzle exercise | |
Given an apache logfile, find the puzzle urls and download the images. | |
Here's what a puzzle url looks like: | |
10.254.254.28 - - [06/Aug/2007:00:13:48 -0700] "GET /~foo/puzzle-bar-aaab.jpg HTTP/1.0" 302 528 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6" | |
""" | |
def read_urls(filename): | |
""" | |
Returns a list of the puzzle urls from the given log file, | |
extracting the hostname from the filename itself. | |
Screens out duplicate urls and returns the urls sorted into | |
increasing order. | |
""" | |
url = [] | |
base = 'http://code.google.com' | |
with open(filename) as fo: | |
data = fo.readlines() | |
for line in data: | |
if 'puzzle' in line: | |
url.append('{}{}'.format(base, line.split(' ')[6])) | |
url = sorted(set(url), key=lambda x:x[-8:]) | |
return url | |
def download_images(urls, directory): | |
x = 0 | |
imgfn = [] | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
for url in urls: | |
filedir = '{}img{}.jpg'.format(directory, x) | |
imgfn.append('<img src="./img{}.jpg">'.format(x)) | |
with open(filedir, 'w') as fobj: | |
try: | |
print('Retrieving file [{}/{}]:img{}'.format(x, len(urls)-1, x)) | |
response = urllib2.urlopen(url) | |
fobj.write(response.read()) | |
print('Complete.') | |
x += 1 | |
except urllib2.URLError as e: | |
print("[-] Couldn't connect to server") | |
print('[-] Error: {}'.format(e)) | |
except urllib2.HTTPError as e: | |
print('[-] HTTP error.') | |
print('[-] Error: {}'.format(e)) | |
with open('{}index.html'.format(directory), 'w') as fobj: | |
fobj.write('<verbatim>\n<html>\n<body>\n{}\n</body>\n</html>'.format(''.join(imgfn))) | |
def main(): | |
args = sys.argv[1:] | |
if not args: | |
print 'usage: [--todir dir] logfile ' | |
sys.exit(1) | |
todir = '' | |
if args[0] == '--todir': | |
todir = args[1] | |
del args[0:2] | |
img_urls = read_urls(args[0]) | |
if todir: | |
download_images(img_urls, todir) | |
else: | |
print '\n'.join(img_urls) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment