Created
March 5, 2010 03:02
-
-
Save jerem/322417 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
import os | |
import re | |
import datetime | |
from ftplib import FTP | |
from BeautifulSoup import BeautifulSoup | |
_multilines_re = re.compile(r'<([a-z0-9\-]+)\s+(.*?)\s+</\1>', re.S | re.M) | |
_singleline_re = re.compile(r'<(product|version)>\s+(.+)$', re.M) | |
class AsusFTP(object): | |
host = 'ftp.asus.com.tw' | |
def __init__(self): | |
BeautifulSoup.NESTABLE_TAGS['path'] = ['version'] | |
BeautifulSoup.NESTABLE_TAGS['release-date'] = ['version'] | |
BeautifulSoup.NESTABLE_TAGS['description'] = ['version'] | |
self.ftp = FTP(self.host, timeout=45) | |
self.ftp.login() | |
def __del__(self): | |
self.ftp.quit() | |
def _parse(self, idx): | |
idx = idx.replace('<~', '</') | |
idx = _multilines_re.sub(r'<\1>\2</\1>', idx) | |
idx = _singleline_re.sub(r'<\1 value="\2">', idx) | |
return BeautifulSoup(idx) | |
def get_idx_as_soup(self, path): | |
self.ftp.cwd(os.path.dirname(path)) | |
idx = [] | |
self.ftp.retrlines('RETR %s' % os.path.basename(path), idx.append) | |
return self._parse('\n'.join(idx)) | |
if __name__ == '__main__': | |
aftp = AsusFTP() | |
soup = aftp.get_idx_as_soup('/pub/ASUS/Path.idx') | |
platform = soup.findAll('product', value='EeePC')[0] | |
platform_path = platform.path.contents[0].strip() | |
soup = aftp.get_idx_as_soup(platform_path) | |
product = soup.findAll('product', value='S101H')[0] | |
product_path = product.path.contents[0].strip() | |
soup = aftp.get_idx_as_soup(product_path) | |
versions = soup.findAll('version') | |
for version in versions: | |
print '\n' | |
print ' - VERSION: ', version.get('value') | |
print '\tDATE: ', datetime.datetime.strptime(version.find('release-date').contents[0].strip(), '%m/%d/%Y') | |
print '\tPATH: ', version.find('path').contents[0].strip() | |
print '\tDESCIPTION: ', version.find('description').contents[0].strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment