Grab RESPONSE_CODES from Authorize.net html page to python file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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)

More like this

  1. Cached lookup model mixin by isagalaev 5 years, 4 months ago
  2. render_to by asolovyov 4 years, 11 months ago
  3. Arbitrary length formset by Rupe 3 years, 8 months ago
  4. A dict template tag by Batiste 5 years, 1 month ago
  5. Create PDF files using rml and django templates by mporrato 5 years, 11 months ago

Comments

(Forgotten your password?)