Generate a .html with the Ebay Categories Tree by terminal and save it in a sqlite DB

in #python9 years ago

At the end we’re going to generate a .html file that would look like the image above.First of all we’re going to create the categories.sh script to control de terminal commands.
The script will manage two terminal commands

  • — build
  • — render [categoryID]


#!/bin/sh
if [ $# -eq 0 ]
then
echo “Please insert — rebuild or— render [categoryId]”
else
if [ $1 = “ — rebuild” ];
 then exec python db.py
elif [ $1 = “ — render” ];
 then exec python html.py $2
fi
fi

Then we create two python scripts, one to manage the the database CRUD actions and the rest query, and the other to render the .html file.The first one is called db.py, and is going to search for a db file called “categories_ht.db” if the file exists, the script will delete it, then create a new db file called “categories_ht.db”.

#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = ‘hetdev’
import sqlite3
import os
import requests
import xml.etree.ElementTree as ET
def delete_database():
filelist = [ f for f in os.listdir(“.”) if f == ‘categories_ht.db’ ]
for f in filelist:
os.remove(f)
def create_database():
conn = sqlite3.connect(‘categories_ht.db’)
c = conn.cursor()
c.execute(‘’’CREATE TABLE categories
(id integer primary key, categoryName text, categoryLevel integer,          bestOfferEnabled integer,
categoryParentID integer)’’’)
conn.commit()
conn.close()

Make the REST api request, and save the data into the db.

def get_categories():
request_headers = {
‘X-EBAY-API-CALL-NAME’: ‘GetCategories’,
‘X-EBAY-API-APP-NAME’: ‘EchoBay62–5538–466c-b43b-662768d6841’,
‘X-EBAY-API-CERT-NAME’: ‘00dd08ab-2082–4e3c-9518–5f4298f296db’,
‘X-EBAY-API-DEV-NAME’: ‘16a26b1b-26cf-442d-906d-597b60c41c19’,
‘X-EBAY-API-SITEID’: 0,
‘X-EBAY-API-COMPATIBILITY-LEVEL’: 861
}
data_xml = ‘’’<?xml version=”1.0" encoding=”utf-8"?>
<GetCategoriesRequest xmlns=”urn:ebay:apis:eBLBaseComponents”>
<CategorySiteID>0</CategorySiteID>
<ViewAllNodes>True</ViewAllNodes>
<DetailLevel>ReturnAll</DetailLevel>
<RequesterCredentials>
<eBayAuthToken>AgAAAA**AQAAAA**aAAAAA**y6rfVg**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wFk4GhAJeHqQydj6x9nY+seQ**PbwDAA**AAMAAA**8OlM6fqJvNyuqK22AdBhw6/Ef9NR5TFA4TbCh2Kj5aZa+cRwuCmml6OFQ9bPWMiCn6fRqiKYjZW4SnihoRtnJhcq9UQL7sgWd7S7rV6k2LMAvUdPWbIadFnxroUnbDpAfCxx+MKKVo4Ez6ENlhtYFVhuVXYRWZ644UVGuTz3JMFdS97ZvTQiZI6oKkRPJM7uYs3+t2VvTzuaI63zMrtIZtsHO2H+qxrqChDL4eGr1K6ugJ1tSYztrmhf2vdtd01QID2CbVrEI/NJOypCs8nTXKEcvMtEsaUmU6ZdlmhTWnfHa45UAWHlEC/sG86r7Y/g72DEhl1SOrRLnng9CnTTT0274q9aJDwgoxK1u6TyP1qoKsa8FrCzIFdevT3FZ2d59PCk6jdKgKEyYiP8EmeNOK82QDOLverS7AhUrJB/fhMMurYy9mQpl0wyz5P8SVbtRb5r8ae3VXM2Sy2+7iSXwjwTyYDkS/ZeBMAEjXslMptIVMUi7pKqUMZofp2nClqrYOJ7k9SHCO3tCG6eckKznOdznSlcizf8/gHNg8AX9i4Dxx6s+L7HebrYptYM565446z7W3hD6fwuzXMONL0q0jffaJvF+buKFVTN/pPXhO/idD310Im5z1NGmb8/suCD6h0jOEGbSl8VRcQ/vNil2ddojHRmKTZr425Rc1JZhAfka4Yem8PmJrVE64C+4PbGXpusrEMK0z5fDpxcAfBw1B+zug42MZuFlO/1xvKEkwopwHlBq4S02sHuiZpfte23</eBayAuthToken>
</RequesterCredentials>
</GetCategoriesRequest>’’’
r =requests.post(‘https://api.sandbox.ebay.com/ws/api.dll', headers=request_headers, data=data_xml)
root = ET.fromstring(r.text.encode(‘utf-8’))
categories = root.find(‘{urn:ebay:apis:eBLBaseComponents}CategoryArray’)
conn = sqlite3.connect(‘categories_ht.db’)
c = conn.cursor()
categories_array_sql = []
for child in categories:
id = int(child.find(‘{urn:ebay:apis:eBLBaseComponents}CategoryID’).text)
categoryName = child.find(‘{urn:ebay:apis:eBLBaseComponents}CategoryName’).text
categoryLevel = int(child.find(‘{urn:ebay:apis:eBLBaseComponents}CategoryLevel’).text)
bestOfferEnabled = 0
try:
if child.find(‘{urn:ebay:apis:eBLBaseComponents}BestOfferEnabled’).text == ‘true’:
bestOfferEnabled = 1
except Exception as e:
pass
categoryParentID = int(child.find(‘{urn:ebay:apis:eBLBaseComponents}CategoryParentID’).text)
if categoryParentID == id:
categoryParentID = -1
category = (id, categoryName, categoryLevel, bestOfferEnabled, categoryParentID)
categories_array_sql.append(category)
c.executemany(‘INSERT INTO categories VALUES (?,?,?,?,?)’, categories_array_sql)
conn.commit()
conn.close()

At then we call the previous methods.

try:
delete_database()
create_database()
get_categories()
except Exception as e:
print e
delete_database()
print (‘Error creating the db or getting the data’)

The other python script is called html.py, this script will search for the category information and the data of their sons, then render it in a html file.The methods to get the category data and the method to get the sons category data.

#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = ‘hetdev’
import sqlite3
import sys
def getCategory(categoryId):
conn = sqlite3.connect(‘categories_ht.db’)
c = conn.cursor()
category = None
c.execute(“SELECT * FROM categories WHERE id = “ + categoryId)
category = c.fetchone()
conn.commit()
conn.close()
return category
def getCategorySons(categoryId):
conn = sqlite3.connect(‘categories_ht.db’)
c = conn.cursor()
categories = None
c.execute(“SELECT * FROM categories WHERE categoryParentID = “ + str(categoryId))
categories = c.fetchall()
conn.commit()
conn.close()
return categories

The method to add the list and the list items.

def render_category_list(f, category):
list_header = “<ul>”
f.write(list_header)
name = category[1]
list_item = “<li>” + name.encode(‘utf-8’) + “</li>”
f.write(list_item)
categoryId = category[0]
sons = getCategorySons(categoryId)
if len(sons)>0:
for son in sons:
render_category_list(f,son)
list_footer = “</ul>”
f.write(list_footer)

Method to create the html file.

def start():
categoryId = sys.argv[1]
category = getCategory(categoryId)
if category is None:
print ‘No category with ID:’ + categoryId
else:
html_file_name = categoryId + ‘.html’
f = open( html_file_name,’w’)
header = “””<!DOCTYPE html>
<head>
<meta charset=”utf-8">
<link rel=”stylesheet” type=”text/css” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link href=”http://fonts.googleapis.com/css?family=Open+Sans" rel=”stylesheet” type=”text/css”>
</head>
<body>
<h2>Ebay Categories List</h2>”””
f.write(header)
render_category_list(f, category)
footer = “””</body>
</html>”””
f.write(footer)
f.close()
print html_file_name

At the we call the previous method

try:
start()
except Exception as e:
# print e
print (‘Error creating the html file’)

Finally we can run in the terminal the commands below.

./categories.sh  --rebuild
./categories.sh  --render 1

The product is a html with the category list.Link to download de files: 

https://mega.nz/#F!bIkSBDDI!kFewaVf4HLIrQvX_ZSjhSA

 As always thanks for reading! 

Sort:  

Congratulations @hetdev! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

The Steem community has lost an epic member! Farewell @woflhart!
SteemitBoard - Witness Update
Do not miss the coming Rocky Mountain Steem Meetup and get a new community badge!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.05
TRX 0.32
JST 0.081
BTC 63253.25
ETH 1685.08
USDT 1.00
SBD 0.42