#!/usr/bin/python

import pycurl

import os

import MySQLdb

import cStringIO

import simplejson

import redis


def fts_get_appname(host,user,port,db,passwd):

    conn=MySQLdb.connect(host=str(host),user=user,passwd=passwd,port=int(port),db=db)

    cur=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

    cur.execute('select appname,accessKey from keyPair where extsrvStatus like "%AB%";')

    result=cur.fetchall()

    return result


def curl_solr(acceskey):

    url = 'http://****/solr/collection1/select?q=userid:'+acceskey+'&wt=json&indent=true'

    buf=cStringIO.StringIO()

    c=pycurl.Curl()

    c.setopt(c.URL,url)

    c.setopt(c.WRITEFUNCTION,buf.write)

    c.perform()

    return buf.getvalue()

    buf.close()

 

def set_redis_billing(r,acceskey,app_sum_data):

    r.rpush('list_AN',"1\t%s\tfts\t%s\t0"%(acceskey,app_sum_data))


if __name__ == '__main__':

    app_dict={}

    mysql_dict = {'ip':'***','user':'***','port':***,'db':'***','passwd':'***'}

    host = mysql_dict['ip']

    user = mysql_dict['user']

    port = mysql_dict['port']

    db = mysql_dict['db']

    passwd = mysql_dict['passwd']

    fts_appname = fts_get_appname(host,user,port,db,passwd)

    pool = redis.StrictRedis(host='***',port=***)

    for i in fts_appname:

        accessKey = i['accessKey']

        appname = i['appname']

        app_dict[appname] = {}

        #app_dict[appname] = accessKey

        solr_url_result = curl_solr(accessKey)

        dict_solr_url_result = simplejson.loads(solr_url_result)

        app_sum_data = dict_solr_url_result['response']['numFound']

        print app_sum_data

        app_dict[appname][accessKey]=str(app_sum_data)

        if app_sum_data != 0:

            set_redis_billing(pool,accessKey,app_sum_data)

    print app_dict