| 1 |
#!/usr/bin/env python2 |
| 2 |
#emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- |
| 3 |
#ex: set sts=4 ts=4 sw=4 noet: |
| 4 |
#------------------------- =+- Python script -+= ------------------------- |
| 5 |
""" |
| 6 |
Yaroslav Halchenko Dartmouth |
| 7 |
web: http://www.onerussian.com College |
| 8 |
e-mail: [email protected] ICQ#: 60653192 |
| 9 |
|
| 10 |
DESCRIPTION (NOTES): |
| 11 |
|
| 12 |
COPYRIGHT: Yaroslav Halchenko 2013 |
| 13 |
|
| 14 |
LICENSE: MIT |
| 15 |
|
| 16 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 17 |
of this software and associated documentation files (the "Software"), to deal |
| 18 |
in the Software without restriction, including without limitation the rights |
| 19 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 20 |
copies of the Software, and to permit persons to whom the Software is |
| 21 |
furnished to do so, subject to the following conditions: |
| 22 |
|
| 23 |
The above copyright notice and this permission notice shall be included in |
| 24 |
all copies or substantial portions of the Software. |
| 25 |
|
| 26 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 32 |
THE SOFTWARE. |
| 33 |
""" |
| 34 |
#-----------------\____________________________________/------------------ |
| 35 |
|
| 36 |
__author__ = 'Yaroslav Halchenko' |
| 37 |
__copyright__ = 'Copyright (c) 2013 Yaroslav Halchenko' |
| 38 |
__license__ = 'MIT' |
| 39 |
|
| 40 |
import argparse |
| 41 |
import os |
| 42 |
import re |
| 43 |
import sys |
| 44 |
|
| 45 |
import urllib2 |
| 46 |
from urlparse import urlsplit |
| 47 |
|
| 48 |
|
| 49 |
def main(cmdline=None): |
| 50 |
args = make_parser().parse_args(cmdline) |
| 51 |
|
| 52 |
for url in args.urls: |
| 53 |
if args.print_filename: |
| 54 |
print(get_filename(url)) |
| 55 |
else: |
| 56 |
response = get_url(url) |
| 57 |
# by default just dump all the information |
| 58 |
print("URL: ", url) |
| 59 |
if url != response.url: |
| 60 |
print("REDIRECTED URL: ", response.url) |
| 61 |
print(response.info()) |
| 62 |
|
| 63 |
def make_parser(): |
| 64 |
parser = argparse.ArgumentParser('%prog: little tool to help gather information about URL(s)') |
| 65 |
parser.add_argument('urls', nargs='*', help='URLs') |
| 66 |
parser.add_argument('-f', '--print-filename', action='store_true', |
| 67 |
help='just print a content-disposition filename if present.' |
| 68 |
' Otherwise the trailing portion of the URL') |
| 69 |
return parser |
| 70 |
|
| 71 |
def get_filename(url): |
| 72 |
response = get_url(url) |
| 73 |
info = response.info() |
| 74 |
if info.has_key('Content-Disposition'): |
| 75 |
res = re.match('.* filename="(.*)"', info['Content-Disposition']) |
| 76 |
return res.groups()[0] |
| 77 |
else: |
| 78 |
return os.path.basename(urllib2.unquote(urlsplit(response.url).path)) |
| 79 |
|
| 80 |
def test_get_filename(): |
| 81 |
assert(get_filename('http://human.brain-map.org/api/v2/well_known_file_download/157722290') == 'T1.nii.gz') |
| 82 |
assert(get_filename('https://raw.githubusercontent.com/datalad/datalad/master/README.md') == 'README.md') |
| 83 |
|
| 84 |
|
| 85 |
def get_url(url): |
| 86 |
request = urllib2.Request(url) |
| 87 |
request.add_header('Accept-encoding', 'gzip,deflate') |
| 88 |
response = urllib2.urlopen(request) |
| 89 |
return response |
| 90 |
|
| 91 |
if __name__ == '__main__': |
| 92 |
# for some reason nose refused to load tests even with --exe |
| 93 |
# test_get_filename() |
| 94 |
sys.exit(main()) |