A1.py: Code for converting Category XML file to Data in Wize2 db @ categories

This code fetched data from category.xml and category name. parent_id and next node id in category table of wize2 database.

import urllib
from collections import defaultdict
import sys
import time
from locale import LC_ALL, format_string, setlocale
from pylons import config
from sqlalchemy import text
from veetwo.models import db
import logging
import MySQLdb
import string
import sys



def fetchdatafromXML():
        
        count=0;
        handler = open("category.xml", "r")
        content = handler.read()
        tempnode = content
        while(int(tempnode.find("</node>"))!=-1):
            nodeid = tempnode[int(tempnode.find("<nodeid>"))+8:int(tempnode.find("</nodeid>"))]
            parent_id = tempnode[int(tempnode.find("<parentid>"))+10:int(tempnode.find("</parentid>"))]
            name = tempnode[int(tempnode.find("<name>"))+6:int(tempnode.find("</name>"))]
            tempnode = tempnode[int(tempnode.find("</node>"))+1:]
            nextag_node_id=nodeid
            name=urllib.quote(name)
            id=nodeid
            sql = '''insert into categories(id, name, parent_id, is_live, nextag_node_id) values (%(id)s,'%(name)s', %(parent_id)s, 'YES',%(nextag_node_id)s)''' %dict(id=id, name=name, parent_id=parent_id, nextag_node_id=nextag_node_id) 
            db.wize.execute(sql)
            print count, ": ", sql
            count+=1

if __name__ == u'__main__':
    # for testing
    from contextlib import nested
    from veetwo.models import feed, site, spider, wize
    from veetwo.lib.utils.command import load_configuration
    config_file = sys.argv[1]
    config.push_process_config(load_configuration(config_file))
    for schema in (feed, site, spider, wize):
        dsn = config[u'wize.' + schema.__name__[(len(schema.__package__)) + 1:] + u'_dsn']
        schema.init(dsn)
    run_queries = [fetchdatafromXML]
    with nested(site.Session(), wize.Session()):    
        for run_query in run_queries:
            run_query()
            sys.stdout.flush()    
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: