def get_musicinfo_byno(mno): if not mno: return None result = [] nos = [] key = get_musicinfo_key(mno) ret = redis_cli.get(key) if ret: ret = json.loads(ret) if isinstance(ret, dict): return ret #TODO: seek music nos in redis cache at first url = cloud_API + '/MusicService.aspx?op=getmusicinfobynos&depot=0&nos=%s' % mno print url try: res = request_json(url, method='GET') session = {} if res['code'] == 1: if not res['result']: #已经下线的歌曲,偶有还可以下载的情况,但是拿不到信息 logger.error( "Failed to get musicinfo (no:%s), possible song has deleted on cloud side" % mno) return None m = res['result']['matches'][0] key = get_musicinfo_key(m['Music_No']) redis_cli.set(key, json.dumps(m)) redis_cli.expire(key, 3600 * 24 * 7) return m except Exception as ex: logger.error(traceback.format_exc()) if not res['code'] == 1: print res['msg'] return None
def get_cloudmusic_url(mno, dtype=0): #https://kcloud.v2.service.ktvdaren.com/MusicService.aspx?appid=ebf0694982384de46e363e74f2c623ed&appver=4.0.0.76&dogname=&filetype=0&musicno=7650021&op=getmusicdownurl&storeid=87832&type=2&userid=17779&username=cx90hcs&utime=1497493237&validkey=ebc9e4378f4dc098368f4492143c06ce&sign=681b1bf04e409d9287c72b68a408a89a&downtyp=0&boxver=5.0.0.72 ses = get_cloud_session() int_t = int(time.time()) params = { 'appid': app_id, 'appver': app_ver, 'dogname': ses['dog'], 'filetype': 0, #0或1:歌曲 2:资料 3:电影 4:任务 'musicno': mno, 'op': 'getmusicdownurl', 'storeid': ses['storeid'], 'userid': ses['uid'], 'username': ses['uname'], 'validkey': ses['validkey'], 'type': 2, 'utime': int_t } qerystr = gen_sign_for_kcloud(params, '', sec_key) url = cloud_API_S + '/MusicService.aspx?' + qerystr #'downtype': dtype, url += "&downtype=%d&boxver=%s&clientid=1" % (dtype, box_ver) print(url) res = request_json(url, method='GET') if res['code'] == 1: minfo = res['result']['matches'][0] redis_cli.set(get_cloudfile_key(mno), json.dumps(minfo)) redis_cli.expire(get_cloudfile_key(mno), 3600 * 3) return minfo elif isinstance(res, dict) and res['code'] == -3: logger.error( u'cannot find this music info from cloud, maybe it has been deleted. musicno: %s' % mno) delete_music_info(mno) return None else: print "get download url: res: %s" % res return None
def get_musicinfo_bylist(mnos): result = [] nos = [] for mno in mnos: key = get_musicinfo_key(mno) ret = redis_cli.get(key) if ret: ret = json.loads(ret) if isinstance(ret, dict): result.append(ret) else: nos.append(mno) if not nos: return result #TODO: seek music nos in redis cache at first url = cloud_API + '/MusicService.aspx?op=getmusicinfobynos&depot=0&nos=%s' % ','.join( nos) print url try: res = request_json(url, method='GET') session = {} if res['code'] == 1: for m in res['result']['matches']: result.append(m) key = get_musicinfo_key(m['Music_No']) redis_cli.set(key, json.dumps(m)) redis_cli.expire(key, 3600 * 24 * 7) return result except Exception as ex: logger.error(traceback.format_exc()) if not res['code'] == 1: print res['msg'] return None
def set_cloud_session(sec): key = get_cloud_session_key() sec_data = json.dumps(sec) redis_cli.set(key, sec_data) redis_cli.expire(key, 60 * 5) return True
def check_aria2_task(self): ret2 = self.server.aria2.tellStopped(0, 1000) if ret2: for r in ret2: print(r['status'], r['files'][0]['uris'][0]['uri'],\ r['files'][0]['path']) if not r['status'] == 'complete': continue mno = aria2_gid2mno(r['gid']) logger.debug("find completed mno: gid: %s -> mno:%s" % (r['gid'], mno)) if mno: minfo = get_musicinfo_byno(mno) if not isinstance(minfo, dict): logger.error('cannot fine minfo: %s' % mno) continue else: logger.debug( "Failed find completed mno: gid: %s -> mno:%s" % (r['gid'], mno)) continue _, fname = os.path.split(r['files'][0]['path']) old_path = self.old_video_path(mno) des_path = self.new_video_path(mno, int(r['totalLength']), fname) #des_path = self.find_video_path(mno, int(r['totalLength']), fname) logger.debug("filepath: %s, filename: %s" % (des_path, fname)) if des_path == '': logger.error("cannot find path for new music") continue logger.debug("addmusic: %s, %s" % (r['files'][0]['path'], des_path)) _ret = self.add_music_info(minfo, r['files'][0]['path'], des_path) if _ret: if old_path and old_path != des_path and os.path.exists( old_path): os.remove(old_path) dinfo = {} dinfo['down_path'] = r['files'][0]['path'] dinfo['down_url'] = r['files'][0]['uris'][0]['uri'] dinfo['down_etime'] = datetime.datetime.now() dinfo['down_status'] = 1 dinfo['music_addtime'] = datetime.datetime.now() if old_path: dinfo['music_replace'] = 1 else: dinfo['music_replace'] = 0 #dinfo['file_md5'] = gid #dinfo['file_type'] = gid logger.error(r) dinfo['file_size'] = r['totalLength'] update_downlog_by_mnogid(mno, r['gid'], dinfo) #Store the completed task for 2 hours. key = get_complete_task_key(aria2_gid2mno(r['gid'])) dlinfo = {} dlinfo['status'] = 'added' dlinfo['percent'] = '100' dlinfo['url'] = r['files'][0]['uris'][0]['uri'] dlinfo['complete'] = r['completedLength'] dlinfo['fpath'] = des_path dlinfo['gid'] = r['gid'] dlinfo['total'] = r['totalLength'] redis_cli.set(key, json.dumps(dlinfo)) redis_cli.expire(key, 3600 * 1) if r['status'] in ["error", "removed", "complete"]: self.server.aria2.removeDownloadResult(r['gid']) else: self.server.aria2.remove(r['gid']) #清除GID到Music_NO的缓存映射信息。还需要一个定时清理多余项的定时任务 #以防止出现大量的无效映射信息 del_musictask_gid(mno, r['gid']) if os.path.exists(r['files'][0]['path'] ) and r['files'][0]['path'] != des_path: os.remove(r['files'][0]['path']) logger.debug("add music file to %s" % des_path)
def set_ktvinfo(self, ktvinfo): key = self.get_ktvinfo_key() ret = redis_cli.set(key, json.dumps(ktvinfo)) redis_cli.expire(key, 3600 * 24)