Subversion Repositories SmartDukaan

Rev

Rev 20592 | Blame | Compare with Previous | Last modification | View Log | RSS feed

import pymongo
import os
import json
from xml.etree.ElementTree import parse, Element
from xml import etree

con=None

filePath="/var/lib/tomcat6/webapps/export/javascripts/autosuggesthotspot.json"
solrPath = "/var/lib/tomcat6/webapps/export/solr/"

def get_mongo_connection(host='localhost', port=27017):
    global con
    if con is None:
        print "Establishing connection %s host and port %d" %(host,port)
        try:
            con = pymongo.MongoClient(host, port)
        except Exception, e:
            print e
            return None
    return con

def generate_auto_suggest():
    PATH = os.path.expanduser(filePath)
    if os.path.isfile(PATH):
        try:
            os.remove(os.path.expanduser(filePath))
        except OSError:
            pass
    output = open(filePath, 'ab+')
    all = get_mongo_connection().Hotspot.HotspotEntity.find()
    autoSuggestMap = {}
    for a in all:
        autoSuggestMap[a['title']] = [a['title']]
    json.dump(autoSuggestMap, output)

def alter_solr_xml():
    all = get_mongo_connection().Hotspot.HotspotEntity.find()
    for a in all:
        fileName = str(a['_id'])+"_irdata_solr.xml"
        doc = parse(solrPath+fileName)
        root = doc.find('doc')
        co = Element("field", name="P_120537")
        co.text="Yes"
        root.append(co)
        doc.write(solrPath+fileName)
    
def main():
    generate_auto_suggest()
    alter_solr_xml()

if __name__ == '__main__':
    main()