From ef72b6c770a113dba79c148596629ff03afbadb6 Mon Sep 17 00:00:00 2001 From: Progfou Date: Sun, 10 Jan 2010 20:55:15 +0700 Subject: [PATCH] =?utf8?q?Petit=20test=20d'outil=20de=20t=C3=A9l=C3=A9charge?= =?utf8?q?ment=20web.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- web/zing-offline.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 web/zing-offline.py diff --git a/web/zing-offline.py b/web/zing-offline.py new file mode 100755 index 0000000..464007e --- /dev/null +++ b/web/zing-offline.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# Licence: public domain +# Original idea from http://projektantos.wordpress.com/\ +# 2009/12/20/tải-nhạc-hot-trong-thang-của-zing-bản-viết-bằng-python/ + +# destination directory, where folder will be placed +DEST_DIR = "~/Music/Zing" + +# download information +HOMEPAGE = "http://mp3.zing.vn/mp3/nghe-album/album-hot/nhac-viet.html" +DOWNLOAD_PATTERN = r'"(http://dl\.mp3.*\?filename=([^/]*\.mp3))"' + +import sys +import logging +from datetime import date +from os import mkdir, utime +from os.path import expanduser, join, isdir, isfile +from urllib import urlopen, urlretrieve +from re import findall +from calendar import timegm + +logging.basicConfig(level=logging.DEBUG, + format="%(asctime)s %(levelname)s %(message)s") +logging.info('Starting.') + +# absolute path to destination directory +dir = join(expanduser(DEST_DIR), "Thang%02d" % date.today().month) +if not isdir(dir): + try: + mkdir(dir) + except: + logging.error("Can't create directory '%s'.", dir) + sys.exit(1) + +# get the page content +try: + f = urlopen(HOMEPAGE) + page_content = f.read() + f.close() +except: + logging.error("Can't read page '%s'.", HOMEPAGE) + sys.exit(2) + +# find all file URLs and download them +count = 0 +for link,filename in findall(DOWNLOAD_PATTERN, page_content): + filepath = join(dir, filename) + if isfile(filepath): + logging.info("'%s' already exists, skipped.", filename) + continue + logging.info("Downloading '%s'..." % filename) + try: + info = urlretrieve(link, filepath)[1] + last_modified = info.getdate('Last-Modified') + if last_modified: + last_modified = timegm(last_modified) + utime(filepath, (last_modified, last_modified)) + logging.info("Done.") + except: + logging.info("ERROR!") + count += 1 + +print "=" * 78 +if count <= 1: + logging.info("%d file has been downloaded.", count) +else: + logging.info("%d files have been downloaded to '%s'.", count, dir) +print "=" * 78 + +sys.exit(0) -- 1.7.10.4