diff options
| author | Jon Bernard <jbernard@tuxion.com> | 2014-06-04 10:45:13 -0400 |
|---|---|---|
| committer | Jon Bernard <jbernard@tuxion.com> | 2014-06-04 10:45:13 -0400 |
| commit | e03338bb3c43e519cc5e01e449668f9779dc5260 (patch) | |
| tree | 5adc63c107375f5791430912404f74721696cf5e | |
| parent | 3860aae21d82cda0de6335de1a00a4385029ef5a (diff) | |
| download | porterbox-e03338bb3c43e519cc5e01e449668f9779dc5260.zip | |
Update porterbox to use beautifulsoup
Not only is the resulting code much cleaner, but the results are
actually accurate now.
| -rwxr-xr-x | porterbox | 62 |
1 files changed, 17 insertions, 45 deletions
@@ -1,53 +1,25 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- -# Copyright © 2010 Jon Bernard. All Rights Reserved. -# -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by the Free -# Software Foundation, either version 3 of the License, or (at your option) -# any later version. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -# more details. -# -# You should have received a copy of the GNU General Public License along -# with this program. If not, see <http://www.gnu.org/licenses/>. +import bs4 +import requests -from urllib2 import urlopen -from HTMLParser import HTMLParser -Architectures = [ 'i386', 'm86k', 'sparc', 'alpha', 'powerpc', 'arm', 'armel', - 'mips', 'mipsel', 'hppa', 'ia64', 's390', 'amd64', 'arm/armv5tel'] +machines_url = 'https://db.debian.org/machines.cgi' -class PorterBoxParser(HTMLParser): +response = requests.get(machines_url) +soup = bs4.BeautifulSoup(response.text) - machine_entry_found = False - machine_name_found = False - machine_name = '' - machine_arch = '' +machine_entries = soup.find_all('tr') - def __init__(self, url): - HTMLParser.__init__(self) - self.feed(urlopen(url).read()) +for machine_entry in machine_entries: + machine_data = machine_entry.find_all('td') + try: + purpose = machine_data[3].text.strip() + if purpose == 'porterbox': + hostname = machine_data[0].text.strip() + architecture = machine_data[1].text.strip() + print('%-18s %s' % (architecture, hostname)) - def handle_starttag(self, tag, attrs): - if tag == 'tr' and not self.machine_entry_found: - self.machine_entry_found = True - elif tag == 'a' and self.machine_entry_found: - self.machine_name_found = True - self.machine_entry_found = False - - def handle_data(self, data): - if self.machine_name_found: - self.machine_name = data - self.machine_name_found = False - elif data in Architectures: - self.machine_arch = data - elif data == 'porterbox': - print "%s (%s)" % (self.machine_name, self.machine_arch) - -if __name__ == '__main__': - PorterBoxParser('http://db.debian.org/machines.cgi') + except IndexError: + # malformed entry, skip it + pass |