Created
June 20, 2010 07:21
-
-
Save abhiomkar/445642 to your computer and use it in GitHub Desktop.
Snippet to spit out direct links from the given URL for the mentioned types (like '.mp3', '.zip' etc)
This file contains hidden or 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 | |
| # Title: pythemall - Python clone of Downloadthemall | |
| # Description: snippet to download direct URLs from a given page | |
| __author__ = 'Abhinay Omkar' | |
| from BeautifulSoup import BeautifulSoup | |
| from urllib import urlopen | |
| from urlparse import urlparse | |
| from getopt import getopt | |
| import sys | |
| def main(): | |
| if sys.argv[1:]: | |
| opts, args = getopt(sys.argv[1:], 't:u:', ('type', 'url')) | |
| else: | |
| print 'Usage:' | |
| print 'pythemall --url (-u) <url> --type (-t) <type> ' | |
| print 'Example -' | |
| print '$ pythemall -t zip -u "http://www.agneelive.com/audio.php" ' | |
| return | |
| EXT = '.mp3' # default value of type | |
| URL = '' | |
| for opt, arg in opts: | |
| if opt in ('--type', '-t'): | |
| EXT = arg.lstrip('.') | |
| elif opt in ('--url', '-u'): | |
| URL = arg | |
| if not URL: | |
| if args: | |
| URL = args[0] | |
| u = urlparse(URL) | |
| c_urlpath = u.scheme + '://' + u.netloc + '/'.join(u.path.split('/')[:-1]) + '/' | |
| src = BeautifulSoup(urlopen(URL).read()) | |
| for a_tag in src.findAll('a'): | |
| if a_tag['href'].endswith(EXT): | |
| print c_urlpath + a_tag['href'] | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment