#-*- coding: utf-8 -*-#'''#Created on 2015-1-5#@author: lianghongwei#'''

importdatetime,sys

sys.path.append('/home/lianghongwei/test/src/information_platform')from settings importMYSQL_ROOT_PASSWORDfrom base.utils.json_utils importjson_loadsfrom base.utils.mysql_utils importget_cursorclassDataRsync:def __init__(self,the_date):

self.game_list_url= 'http://bugu.163.com/UG/AgentGameList' #获取游戏列表的链接,参数gid,为-1时,返回id为最大的10个游戏

self.game_info_url = 'http://bugu.163.com/UG/AgentGameInfo' #参数gid,type(1代表GAC,2代表SDC) POST的

#获取数据库连接游标

#通过ajax从外部链接访问到数据

def __post_get_data(self,url,para):importurllib,urllib2

params=urllib.urlencode(para)#print url,params

response =urllib2.urlopen(url,params)

data_string=response.read()#print data_string

returnjson_loads(data_string)

#数据的处理,这里是拿到数据后的处理函数,后面会重点讲这个方法def __save_data(self,data_id):

cur=get_cursor('info_app')try:ifdata_id:

cur.execute('select id from industry_library_agent_game where name=%s',data_id['game_name'])#industry_library_agent_game_review

result=cur.fetchone()ifresult:

data= self.__post_get_data(self.game_info_url,{'gid':data_id['gid'],'type':1})#industry_library_agent_game_review

values =[

data['info']['inno'],data['info']['inno_res'],

data['info']['core'],data['info']['core_res'],

data['info']['biaoxia'],data['info']['biaoxian_res'],

data['info']['consume'],data['info']['consume_res'],

data['info']['shangshou'],data['info']['shangshou_res'],

data_id['gid']]

sql='update industry_library_agent_game_review set bg_inno_res=%s,bg_core_res=%s,bg_biaoxian_res=%s,bg_consume_res=%s,bg_shangshou_res=%s where id=%d'cur.execute(sql,values)printvalueselse:print data_id['game_name']#cur.executemany('insert into industry_library_agent_game (bg_info,bg_nno) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',values)

except:pass

def __resave_data(self,fid):

data_id=self.__post_get_data(self.game_list_url,{'gid':fid})#print data_id

for i in range(len(data_id['info'])):#print len(data_id['info'])

printi

self.__save_data(data_id['info'][i])if i<9:returnFalseelse:print 666self.__resave_data(data_id['info'][i]['gid'])

#calData方法,用来访问数据defcalcData(self):

data_id=self.__post_get_data(self.game_list_url,{'gid':-1})for i in range(len(data_id['info'])):#print len(data_id['info'])

self.__save_data(data_id['info'][i])

self.__resave_data(data_id['info'][i]['gid'])

#代码运行入口if __name__ == "__main__":

now= datetime.date(2013,7,9)

t=DataRsync(now)

t.calcData()

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐