# -*- coding: utf-8 -*- from urllib import urlopen import md5 import base64 #__metaclass__ = type class switch(object): def __init__(self, value): self.value = value self.fall = False def __iter__(self): yield self.match raise StopIteration def match(self, *args): if self.fall or not args: return True elif self.value in args: self.fall = True return True else: return False def resultOfSearch(keyword,page): result = '' APP_API_KEY = "loooooooooooooooooooool"#你猜 APP_API_BASEURI = "http://api.xxx.com/"#你再猜 APP_API_VERSION = "v1" result = urlopen(APP_API_BASEURI+APP_API_VERSION+"/wpi.php?apikey="+APP_API_KEY+"&method=get_Search&keyword="+(((keyword).decode("GB2312")).encode("UTF-8")).replace(" ","%20")+"&p="+ repr(page)).read() return result def get_keyword(keyword): keyword = keyword.decode("GB2312") def fmtStr2u(srcStr): result = '' i = 0 while i < len(srcStr): if srcStr[i]=='\\' and srcStr[i+1]=='u': result += unichr(int(srcStr[i+2:i+6],16)) i += 6 elif srcStr[i]=='\\' and srcStr[i+1]=='/': result += "/" i += 2 else: result += srcStr[i] i += 1 return result def get_apkId(apiResult): apkID = apiResult[apiResult.index("apkId")+8:apiResult.index("apkName")-3] return apkID def get_apkName(apiResult): apkName = apiResult[apiResult.index("apkName")+10:apiResult.index("apkTitle")-3] return apkName def get_apkTitle(apiResult): apkTitle = apiResult[apiResult.index("apkTitle")+11:apiResult.index("apkVersionCode")-3] apkTitle = fmtStr2u(apkTitle) return apkTitle def get_apkVersionName(apiResult): apkVersionName = apiResult[apiResult.index("apkVersionName")+17:apiResult.index("apkSize")-3] apkVersionName = fmtStr2u(apkVersionName) return apkVersionName def get_catId(apiResult): catid = int(apiResult[apiResult.index("catid")+8:apiResult.index("downnum")-3]) return catid def get_apkDownUrl(apkId): md5str = repr(md5.md5((apkId+"@W2x5B1C0")).hexdigest()) b64str = base64.encodestring(apkId + ",0,0,0,," + md5str[9:17]) apkDownUrl = "PrivateId = " + b64str return apkDownUrl def catid2chs(catid): arg = catid for case in switch(arg): if case(5): return "系统工具".decode("GB2312"), break if case(59): return "桌面插件".decode("GB2312") break if case(6): return "主题美化".decode("GB2312") break if case(7): return "社交聊天".decode("GB2312") break if case(55): return "咨询阅读".decode("GB2312") break if case(8): return "通讯网络".decode("GB2312") break if case(9): return "影音娱乐".decode("GB2312") break if case(10): return "出行拍照".decode("GB2312") break if case(56): return "实用工具".decode("GB2312") break if case(12): return "文档商务".decode("GB2312") break if case(11): return "学习财经".decode("GB2312") break if case(13): return "休闲游戏".decode("GB2312") break if case(14): return "益智游戏".decode("GB2312") break if case(15): return "棋牌游戏".decode("GB2312") break if case(16): return "体育赛车".decode("GB2312") break if case(18): return "掌上网游".decode("GB2312") break if case(60): return "策略塔防".decode("GB2312") break if case(): return "bug" def printList(result): tmpkey = keyword + ' ' tmpkey = (tmpkey.decode("GB2312")).lower() a={} j = 0 numOfDict = 0 while len(tmpkey)!=0: a[j]=tmpkey[:tmpkey.index(" ")] tmpkey=tmpkey[len(a[j])+1:] j+=1 numOfDict += 1 baknum = numOfDict i = 0 while i < result.count("apkId"): apkId = get_apkId(result) apkVersionName = get_apkVersionName(result) apkName = get_apkName(result) apkTitle = get_apkTitle(result) catId = get_catId(result) flag = True numOfDict=baknum while numOfDict !=0: if a[numOfDict-1] in (apkTitle).lower(): numOfDict -= 1 else: flag = False break if flag==True: print fmtStr2u(catid2chs(catId)) + u" : " + apkTitle + u" " +apkVersionName if catId >=13 and catId <=18 or catId == 60: cat = "game" else: cat = "apk" print "http://www.coolapk.com/" + cat + "-" + apkId + "-" + apkName + "/" print get_apkDownUrl(apkId) result = result[result.index("}")+1:] keyword = raw_input("keyword : ") print "\n" page = 1 result = '' while(result!=u"data://[]"): result = resultOfSearch(keyword,page) printList(result) page += 1