# coding: utf-8 import urllib2 import logging import codecs from bs4 import BeautifulSoup from bs4.element import Tag from django.core.management.base import NoArgsCommand log = logging.getLogger(__name__) class Command(NoArgsCommand): help = 'Get and parse to python file authorize.net codes from web' def handle(self, *args, **options): url = "http://www.authorize.net/support/merchant/Transaction_Response/Response_Reason_Codes_and_Response_Reason_Text.htm" data = urllib2.urlopen(url).read() soup = BeautifulSoup(data) tables = soup.find_all("table") codes = [] for table in tables: headings = [th.get_text() if isinstance(th, Tag) else '' for th in table.find_all("tr")[0]] for row in table.find_all("tr")[1:]: data = zip(headings, (td.get_text() for td in row.find_all("td"))) c = str(data[1][1]).replace('\n', '') v = unicode(data[3][1]) if v == ' ': v = '' codes.append(u"\t'{code}': '{detail}'".format(code=c, detail=v)) output = u"\n#generated\n\n" \ u"RESPONSE_REASON_CODE_DETAILS = {\n" \ u"%s" \ u"\n}" % (',\n'.join([c.replace('\n', '') for c in codes])) fname = './response_codes.py' file = codecs.open(fname, "w", "ascii", "ignore" ) file.write(output)