Created
October 15, 2010 17:53
-
-
Save weaver/628633 to your computer and use it in GitHub Desktop.
Download some grab.by images into the current directory.
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/env python | |
## Download some grab.by images into the current directory. | |
## Requires curl command-line utility. | |
## | |
## The bounds should be given in base62 (e.g. in the range 0 - ZZZZ). | |
## | |
## Example: | |
## | |
## mkdir /tmp/grab | |
## cd /tmp/grab | |
## python /path/to/this/script 1000 1100 | |
import os, sys, re, httplib | |
alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
def encode(n): | |
result = '' | |
while n > 0: | |
n, r = divmod(n, len(alphabet)) | |
result = alphabet[r] + result | |
return result | |
def decode(s): | |
result = 0 | |
while len(s) > 0: | |
result = result * len(alphabet) + alphabet.find(s[0]) | |
s = s[1:] | |
return result | |
def grab(start, stop): | |
for n in (encode(n) for n in xrange(decode(start), decode(stop))): | |
res = get('grab.by', '/%s' % n) | |
if res.status == 302: | |
print 'Found %s.png' % n | |
os.system("curl -Lo %s.png '%s' 2>/dev/null" % (n, res.getheader('Location'))) | |
else: | |
print 'Skipped %s.png (status: %d)' % (n, res.status) | |
def get(host, url): | |
conn = httplib.HTTPConnection('grab.by') | |
conn.request('GET', url) | |
return conn.getresponse() | |
def usage(): | |
print 'usage: python %s start stop' % sys.argv[0] | |
sys.exit(1) | |
def main(start=None, stop=None): | |
if not (start and stop): | |
usage() | |
grab(start, stop) | |
if __name__ == '__main__': | |
main(*sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment