list of all eBay categories and eBay category ids

it’s strange that a company like eBay doesn’t make it easy for sellers to get the resources they need. have you tried getting a complete list of all eBay categories? maybe with the eBay category tree? – you will find that there’s not a convenient download location, albeit there should be!

the “trick” so I’ve learned is to get it yourself.

here is a very short python script that will get your categories:

#! /usr/bin/env python
import tools as t
import xmltodict as xml
import requests
url = 'http://open.api.ebay.com/Shopping?callname=GetCategoryInfo&appid={}&siteid={}&CategoryID={}&version=729&IncludeSelector=ChildCategories'
app_id = 'XXXX' #get your own eBay app id
site_id = '3' # 3 is UK
to_get = [-1] # start at the top
done = []
categories = {}
if __name__ == "__main__":
    while len(to_get) > 0:
        cid = to_get.pop()
        re = requests.get(url.format(app_id, site_id, cid))
        root = xml.parse(re.text)
        cats = root['GetCategoryInfoResponse']['CategoryArray']['Category']
        for cat in cats:
            if cat['CategoryID'] not in categories:
                print(cat.get('CategoryName', 0))
                c = {'category_id': cat.get('CategoryID', 0),
                    'category_level': cat.get('CategoryLevel', 0),
                    'category_name': cat.get('CategoryName', 0),
                    'category_parent_id': cat.get('CategoryParentID', 0),
                    'leaf_category': cat.get('LeafCategory', 0),
                    'category_name_path': cat.get('CategoryNamePath', 0)}
                categories[cat['CategoryID']] = c
            done.append(cid)
            if cat['CategoryID'] not in done  and cat['LeafCategory'] == 'false' :
                to_get.append(cat['CategoryID'])
    with open('eBay_categories.tsv', 'w', encoding="utf-8") as of:
        of.write("category_idtcategory_leveltcategory_nametcategory_parent_idtleaf_categorytCategoryNamePathn")
        for k,v in categories.items():
            of.write(str(v['category_id']) + "t" + str(v['category_level']) + "t" + str(v['category_name']) + "t" + str(v['category_parent_id']) + "t" + str(v['leaf_category']) + "t" + str(v['category_name_path']) + "n")
the above will then create a tab-delimited text file named ‘eBay_categories.tsv’.
please note you will need both the “requests” and the “xmltodict” modules. they can be easily obtained using pypi. this code is python 3

Python on MS SQL (via pyodbc)

I have been struggling, albeit not very long, to set up a DB connection to a remote MS SQL server through Python. I thought that the process is not as straight forward as I’d like it to be, but I guess that’s okay because as far as I can see the best solution is to cobble it together a bit.

Basically it seems that the best solution is not to use a python end to end module as a driver, but to use features that are available otherwise, such as ODBC. Then use a ODBC driver for the MS SQL engine and then use ODBC within Python.
As a Linux person, I first had to install unixodbc:

sudo apt-get install unixodbc unixodbc-dev

This takes care of you being able to use ODBC to connect to ODBC database servers. The other part is having a ODBC driver for the connection to the MS SQL server. There are probably others and I shall list them in due time, but for MS SQL, I found it easiest to use FreeTDS (an implementation of the Tabular Data Stream for Linux).

To install:

sudo apt-get install freedts-dev tdsodbc

With this done, you can preceed to modifying the /etc/odbcinst.ini:

sudo nano /etc/odbcinst,ini

[ODBC]
Trace = Yes
TraceFile = /tmp/odbc.log

[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout =
CPReuse =
UsageCount = 1

Please note that the paths for libtdsodbc.so and libtdsS.so are not necessarily as the ones given in this example. For one, I’m using a 64bit version, so on 32 the path is probably different. Another possible location for the files would be: /usr/lib/odbc/.
Either search for the location or seek Google to find where your distribution saves these files.

You will also need to have the python-dev stuff installed:

sudo apt-get install python-dev 

Now that the system is installed, you will want to install the pydobc module. You can download the latest version of the pyodbc module here. Extract the file where it’s convenient for you and ‘cd’ into the folder.
To install simply do:

sudo python setup.py install