import sys sys.path.append('..') #必须要, 设置project为源程序的包顶 ''' 功能测试脚本 ''' from project.Scrawl.QQMusic import QQMusic app1 = QQMusic.QQMusic() print(app1.search_by_keyword('纸短情长')) from project.Scrawl.NeteasyMusic import NeteasyMusic app2 = NeteasyMusic.Netmusic() print(app2.pre_response_neteasymusic('大鱼')) from project.Scrawl.XiamiMusic.XiamiMusic import Search_xiami app3 = Search_xiami() print(app3.search_xiami('纸短情长'))
def search_json(): """ 用于接受各类前端的歌曲名字的api请求 分为POST/GET请求 如果是POST则又分为 三大platform平台不同而调起不同的爬虫脚本 有关更多错误码信息请查阅SDK文档 """ global re_dict if request.method == 'POST': re_dict = {} data = request.get_data() # 获得json数据包. try: dict_data = json.loads(data) # 解析json数据包. except: re_dict = _Return_Error_Post(code=ReturnStatus.ERROR_PSOT_DATA, status="Failed", detail="ERROR_PSOT_DATA") try: music_title = dict_data["title"] music_platform = dict_data["platform"] try: music_page = dict_data["page"] except: music_page = 1 # 获得请求的歌曲名字和选择的音乐平台 except: re_dict = _Return_Error_Post(code=ReturnStatus.ERROR_PARAMS, status="Failed", detail="ERROR_PARAMS") else: if music_page > 10: re_dict = _Return_Error_Post(code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") else: if music_title != '' or music_title != None: if music_platform == "Neteasymusic": neteasymusic_id = neteasy_scrawl.Netmusic() re_dict = neteasymusic_id.pre_response_neteasymusic( music_title, music_page) try: re_dict["code"] except KeyError: if re_dict: re_dict.update({ "code": ReturnStatus.SUCCESS, "status": "Success", "now_page": music_page, "next_page": music_page + 1, "before_page": music_page - 1 }) else: re_dict = _Return_Error_Post( code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") else: re_dict = _Return_Error_Post( code=ReturnStatus.ERROR_SEVER, status="Failed", detail="ERROR_SEVER") elif music_platform == "Xiamimusic": xiamimusic_search = xiami_scrawl.Search_xiami() re_dict = xiamimusic_search.search_xiami( music_title, music_page) if re_dict: re_dict.update({ "code": ReturnStatus.SUCCESS, "status": "Success", "now_page": music_page, "next_page": music_page + 1, "before_page": music_page - 1 }) else: re_dict = _Return_Error_Post( code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") elif music_platform == "QQmusic": qqmusic_search = qq_scrawl.QQMusic() re_dict = qqmusic_search.search_by_keyword( music_title, music_page) try: re_dict["code"] except KeyError: if re_dict: re_dict.update({ "code": ReturnStatus.SUCCESS, "status": "Success", "now_page": music_page, "next_page": music_page + 1, "before_page": music_page - 1 }) else: re_dict = _Return_Error_Post( code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") else: pass elif music_platform == "Kugoumusic": kugou_search = kugou_scrawl.Kugou() re_dict = kugou_search.Search_List( music_title, music_page) try: re_dict["code"] except KeyError: if re_dict: re_dict.update({ "code": ReturnStatus.SUCCESS, "status": "Success", "now_page": music_page, "next_page": music_page + 1, "before_page": music_page - 1 }) else: re_dict = _Return_Error_Post( code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") else: pass finally: re_dict.update({ "now_page": music_page, "next_page": music_page + 1, "before_page": music_page - 1 }) else: re_dict = _Return_Error_Post( code=ReturnStatus.NO_SUPPORT, status="Failed", detail="NO_SUPPORT") else: re_dict = _Return_Error_Post( code=ReturnStatus.ERROR_PARAMS, status="Failed", detail="ERROR_PARAMS") finally: if re_dict == "": re_dict = _Return_Error_Post(code=ReturnStatus.NOT_SAFE, status="Failed", detail="NOT_SAFE") response = Response(json.dumps(re_dict), mimetype='application/json') response.headers.add('Server', 'python flask') response.headers['Access-Control-Allow-Origin'] = '*' response.headers[ 'Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers[ 'Access-Control-Allow-Headers'] = 'x-requested-with' return response else: re_dict = _Return_Error_Post(code=ReturnStatus.ERROR_METHOD, status="Failed", detail="ERROR_METHOD") response = Response(json.dumps(re_dict), mimetype='application/json') response.headers.add('Server', 'python flask') response.headers['Access-Control-Allow-Origin'] = '*' response.headers[ 'Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers['Access-Control-Allow-Headers'] = 'x-requested-with' return response
def play_id(): """ 用于前端请求歌曲id时服务器针对性的反馈方法 基本内容如上. """ global re_dict if request.method == 'POST': data = request.get_data() dict_data = json.loads(data) try: music_platform = dict_data['platform'] except: re_dict = _Return_Error_Post(code=ReturnStatus.ERROR_PARAMS, status="Failed", detail="ERROR_PARAMS") else: if music_platform != '' or music_platform != None: if music_platform == "Neteasymusic": neteasymusic_id = neteasy_scrawl.Netmusic() music_id = dict_data["id"] re_dict = neteasymusic_id.music_id_requests(music_id) if re_dict: re_dict.update({ "code": ReturnStatus.SUCCESS, "status": "Success" }) else: re_dict = _Return_Error_Post( code=ReturnStatus.NO_MUSIC_DETAIL, status="Failed", detail="NO_MUSIC_DETAIL") elif music_platform == "Xiamimusic": try: music_id = dict_data["id"] except KeyError: re_dict = _Return_Error_Post( code=ReturnStatus.ERROR_PARAMS, status="Failed", detail="ERROR_PARAMS") else: re_dict = xiami_scrawl.Search_xiami.id_req(music_id) print(re_dict) if re_dict: re_dict.update({ "code": ReturnStatus.SUCCESS, "status": "Success" }) else: re_dict = _Return_Error_Post( code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") elif music_platform == "QQmusic": qqmusic_id = qq_scrawl.QQMusic() re_dict = qqmusic_id.search_by_id(dict_data["id"]) if re_dict: re_dict.update({ "code": ReturnStatus.SUCCESS, "status": "Success" }) else: re_dict = _Return_Error_Post( code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") elif music_platform == "Kugoumusic": kugou = kugou_scrawl.Kugou() re_dict = kugou.hash_search(dict_data["id"]) if re_dict: re_dict.update({ "code": ReturnStatus.SUCCESS, "status": "Success" }) else: re_dict = _Return_Error_Post( code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") else: re_dict = _Return_Error_Post(code=ReturnStatus.NO_SUPPORT, status="Failed", detail="NO_SUPPORT") finally: response = Response(json.dumps(re_dict), mimetype='application/json') response.headers.add('Server', 'python flask') response.headers['Access-Control-Allow-Origin'] = '*' response.headers[ 'Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers[ 'Access-Control-Allow-Headers'] = 'x-requested-with' return response else: re_dict = _Return_Error_Post(code=ReturnStatus.ERROR_METHOD, status="Failed", detail="ERROR_METHOD") response = Response(json.dumps(re_dict), mimetype='application/json') response.headers.add('Server', 'python flask') response.headers['Access-Control-Allow-Origin'] = '*' response.headers[ 'Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers['Access-Control-Allow-Headers'] = 'x-requested-with' return response
def search_json(): """ 用于接受各类前端的歌曲名字的api请求 分为POST/GET请求 如果是POST则又分为 三大platform平台不同而调起不同的爬虫脚本 有关更多错误码信息请查阅SDK文档 """ global re_dict if request.method == 'POST': re_dict = {} data = request.get_data() # 获得json数据包. try: dict_data = json.loads(data) # 解析json数据包. except: re_dict = _Return_Error_Post(code=ReturnStatus.ERROR_PSOT_DATA, status="Failed", detail="ERROR_PSOT_DATA") try: music_title = dict_data["title"] music_platform = dict_data["platform"] try: music_page = dict_data["page"] except: music_page = 1 # 获得请求的歌曲名字和选择的音乐平台 except: re_dict = _Return_Error_Post(code=ReturnStatus.ERROR_PARAMS, status="Failed", detail="ERROR_PARAMS") else: if music_page > 10: re_dict = _Return_Error_Post(code=ReturnStatus.OVER_MAXPAGE, status="Failed", detail="OVER_MAXPAGE") else: if music_title != '' or music_title != None: if music_platform == "Neteasemusic": neteasymusic_id = neteasy_scrawl.Netmusic() re_dict = neteasymusic_id.pre_response_neteasymusic( music_title, music_page) elif music_platform == "Xiamimusic": xiamimusic_search = xiami_scrawl.Search_xiami() re_dict = xiamimusic_search.search_xiami( music_title, music_page) elif music_platform == "QQmusic": qqmusic_search = qq_scrawl.QQMusic() re_dict = qqmusic_search.search_by_keyword( music_title, music_page) elif music_platform == "Kugoumusic": kugou_search = kugou_scrawl.Kugou() re_dict = kugou_search.Search_List( music_title, music_page) elif music_platform == "Kuwomusic": kuwo_search = kuwo_scrawl.KuwoMusic() re_dict = kuwo_search.Search_List( music_title, music_page) elif music_platform == "Migumusic": migu_search = migu_scrawl.Migu() re_dict = migu_search.search(music_title, music_page) elif music_platform == "Baidumusic": baidu_search = baidu_scrawl.BaiduMusic() re_dict = baidu_search.search_by_keyword( keyword=music_title, page_no=music_page, page_num=10) else: logger.logger.warning("用户请求了一个不被支持的平台") re_dict = _Return_Error_Post( code=ReturnStatus.NO_SUPPORT, status="Failed", detail="NO_SUPPORT") else: logger.logger.warning("用户的请求有参数错误" + dict_data) re_dict = _Return_Error_Post( code=ReturnStatus.ERROR_PARAMS, status="Failed", detail="ERROR_PARAMS") finally: if re_dict == "": re_dict = _Return_Error_Post(code=ReturnStatus.NOT_SAFE, status="Failed", detail="NOT_SAFE") elif re_dict == ReturnStatus.NO_EXISTS: re_dict = _Return_Error_Post(code=ReturnStatus.NO_EXISTS, status="Failed", detail="NO_EXISTS") logger.logger.warning("用户的请求不存在。" + dict_data) response = Response(json.dumps(re_dict), mimetype='application/json') response.headers.add('Server', 'python flask') response.headers['Access-Control-Allow-Origin'] = '*' response.headers[ 'Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers[ 'Access-Control-Allow-Headers'] = 'x-requested-with' return response else: logger.logger.warning("请求search接口使用了错误的方法") re_dict = _Return_Error_Post(code=ReturnStatus.ERROR_METHOD, status="Failed", detail="ERROR_METHOD") response = Response(json.dumps(re_dict), mimetype='application/json') response.headers.add('Server', 'python flask') response.headers['Access-Control-Allow-Origin'] = '*' response.headers[ 'Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers['Access-Control-Allow-Headers'] = 'x-requested-with' return response