def __init__(self): self.mode = AES.MODE_CBC self.key = self.Pad_Key(r"%s3(1LPJQEjszQIL") if int(config.getConfig("open_database", "redis")) == 1: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=host, port=int(port), decode_responses=True, db=6)
def requests_play_url(self, music_id, proxies=''): self.post_data = AES.encrypted_request(self.play_default % (music_id, self.br)) if proxies == '': resp = self.session.post(url=self.play_url, data=self.post_data, headers=self.headers) else: resp = self.session.post(url=self.play_url, data=self.post_data, headers=self.headers, proxies=proxies) try: resp = resp.json() except: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=str(host), port=int(port), db=5) random_int = random.sample(range(0, self.r.dbsize()), 1) proxies = self.r.get(str(random_int[0])) self.requests_play_url(music_id, eval(proxies)) play_url = resp["data"][0]['url'] if play_url == None: play_url = self.url_ % (music_id) self.requ_date["song"]["list"][0].update({"play_url": play_url})
def __init__(self): self.requ_date = {"song": {"totalnum": "", "list": [{}]}} self.search_url = "http://music.163.com/api/search/get/web?csrf_token=" # 通过歌曲名称获得歌曲的ID信息(GET请求) self.play_url = "http://music.163.com/weapi/song/enhance/player/url?csrf_token=" # 通过加密数据POST请求得到播放地址 # https://api.imjad.cn/cloudmusic/?type=song&id=%s&br=320000 self.url_ = "http://music.163.com/song/media/outer/url?id=%s.mp3" # 网易根据api直接请求到下载音乐地址(%s均为歌曲id) self.comment_url = "https://music.163.com/weapi/v1/resource/comments/R_SO_4_%s?csrf_token=" # 通过加密数据POST请求得到评论内容 self.lyric_url = "http://music.163.com/api/song/lyric?id=%s&lv=1&kv=1&tv=-1" # 通过id获得歌曲的歌词信息(json包) 只需要(GET请求) self.session = requests.session() self.headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0', 'Referer': 'http://music.163.com/', 'Content-Type': 'application/x-www-form-urlencoded' } self.play_default = "{\"ids\":\"[%s]\",\"br\":%s\ ,\"csrf_token\":\"\"}" if int(config.getConfig("open_database", "redis")) == 1: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=host, port=int(port), decode_responses=True) self.br = "128000"
def __init__(self): """ 预处理包括连接mongodb数据库(利用config方法),设置用户代理等信息 以及self.Sync_NEM_Url 是获得用户歌单的网易API具体地址 连接的数据表时mydb, 然后选择mydb中的test_set集合 """ host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=host, port=int(port), decode_responses=True, db=2) host = config.getConfig("mongodb", "mongodbhost") port = config.getConfig("mongodb", "mongodbport") self.conn = MongoClient(str(host), int(port)) self.db = self.conn.mydb self.my_set = self.db.test_set self.session = requests.session() self.headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13;rv:57.0) Gecko/20100101 Firefox/57.0', 'Referer': "http://music.163.com/api/user/playlist/?offset=0&limit=100&uid=%s" } self.Sync_NEM_Url = "http://music.163.com/api/user/playlist/?offset=0&limit=100&uid=%s"
def __init__(self): if int(config.getConfig("open_database", "redis")) == 1: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=host, port=int(port), decode_responses=True, db=7)
def __init__(self): if int(config.getConfig("open_database", "redis")) == 1: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=host, port=int(port), decode_responses=True, db=4) # 四号表用于维护用户的密码盐值 host = config.getConfig("mongodb", "mongodbhost") port = config.getConfig("mongodb", "mongodbport") self.mong = MongoClient(str(host), int(port)) self.db = self.mong.mydb self.user_db = self.db.User
def pre_request(self, url, proxies=''): """ 这个类用于维护redis-1更新 更新的内容为热门歌单数据 储存格式范例已经在类的说明中说明 """ global Page_Start_Url, Page_Start, Raw_Page_Sum if proxies == '': resp = self.session.get(url=self.NEMurl + url, headers=self.headers) else: resp = self.session.get(url=self.NEMurl + url, headers=self.headers, proxies=proxies) try: regex = re.compile( r"\<img class=\"[\w0-9\-]+\" src=\"(.+)\"\/>\n<a title=\"([\|\✞\♪\(\)\?\?\♡\【\¼\】\/\[\]\丨\s\「\」\|\『\』\——\•\★\"\u4e00-\u9fa5\w\d\s\,\.]+)\" href=\"(.+)\" class=" ) result = regex.findall(resp.text) regex = re.compile(r"<a href=\"(.+)\" class=\"zpgi\">\d{1,3}</a>") Page_Url = regex.findall(resp.text) Limit_Max_Page = int( re.findall(r'offset=(\d{2,5})', Page_Url[-1])[0]) except: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=str(host), port=int(port), db=4) random_int = random.sample(range(0, self.r.dbsize()), 1) proxies = self.r.get(str(random_int[0])) self.pre_request(url, eval(proxies)) Raw_Page_Sum = 0 for i in range(Raw_Page_Sum, Raw_Page_Sum + len(result)): self.r.set( str(i), result[i - Raw_Page_Sum][0] + "user_song_list" + result[i - Raw_Page_Sum][1] + "user_song_list" + result[i - Raw_Page_Sum][2]) print("Update") Raw_Page_Sum += len(result) print(Raw_Page_Sum) regex = re.compile(r"<a href=\"(.+)\" class=\"zpgi\">\d{1,3}</a>") Page_Url = regex.findall(resp.text) Limit_Max_Page = int(re.findall(r'offset=(\d{2,5})', Page_Url[-1])[0]) if Page_Start <= Limit_Max_Page: Page_Start += 35 url = Page_Start_Url + str(Page_Start) time.sleep(30) test.pre_request(url) else: return 1
def new_requests_play_url(self, music_id): global music_data new_music_id = [] if int(config.getConfig("open_database", "redis")) == 1: Search_Db = "NEM" + str(music_id) exist_bool = self.r.get(Search_Db) if not exist_bool: play_url = self.url_ % (music_id) self.r.set(Search_Db, play_url) else: play_url = exist_bool new_music_id = re.findall(r"url\?id=(\d{1,20})", exist_bool) else: play_url = self.url_ % (music_id) music_data = {} try: music_data.update({"play_url": play_url, "music_id": music_id[0]}) except: music_data.update({"play_url": play_url, "music_id": music_id}) try: self.requ_date["song"]["list"][0].update(music_data) except IndexError: self.requ_date["song"]["list"][0] = {} self.requ_date["song"]["list"][0].update(music_data)
def music_detail(self, music_id, id_flag=0, proxies=''): music_data = {} url = "http://music.163.com/api/song/detail?ids=[%s]" if proxies == '': resp = self.session.get(url=url % music_id, headers=self.headers) else: resp = self.session.get(url=url % music_id, headers=self.headers, proxies=proxies) try: content = resp.json() except: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=str(host), port=int(port), db=4) random_int = random.sample(range(0, self.r.dbsize()), 1) proxies = self.r.get(str(random_int[0])) self.music_detail(music_id, eval(proxies)) try: name = content['songs'][0]["name"] artists = content['songs'][0]["artists"][0]["name"] image_url = content['songs'][0]["album"]["picUrl"] except: music_data['code'] = ReturnStatus.ERROR_SEVER else: music_data.update({ "image_url": image_url, "music_name": name, "artists": artists }) try: if id_flag == 1: self.requ_date["song"]["list"][0].update(music_data) try: self.requ_date["song"]["list"][0]["image_url"] except KeyError: self.requ_date["song"]["list"][0].update(music_data) else: self.requ_date["song"]["list"].append(music_data) except: self.requ_date["song"]["list"] = {} self.requ_date["song"]["list"].append(music_data) return self.requ_date["song"]["list"][0]
def __init__(self): self.top_list_all = { 0: ['云音乐新歌榜', '/discover/toplist?id=3779629'], 1: ['云音乐热歌榜', '/discover/toplist?id=3778678'], 2: ['网易原创歌曲榜', '/discover/toplist?id=2884035'], 3: ['云音乐飙升榜', '/discover/toplist?id=19723756'], 4: ['云音乐电音榜', '/discover/toplist?id=10520166'], 5: ['UK排行榜周榜', '/discover/toplist?id=180106'], 6: ['美国Billboard周榜', '/discover/toplist?id=60198'], 7: ['KTV嗨榜', '/discover/toplist?id=21845217'], 8: ['iTunes榜', '/discover/toplist?id=11641012'], 9: ['Hit FM Top榜', '/discover/toplist?id=120001'], 10: ['日本Oricon周榜', '/discover/toplist?id=60131'], 11: ['韩国Melon排行榜周榜', '/discover/toplist?id=3733003'], 12: ['韩国Mnet排行榜周榜', '/discover/toplist?id=60255'], 13: ['韩国Melon原声周榜', '/discover/toplist?id=46772709'], 14: ['中国TOP排行榜(港台榜)', '/discover/toplist?id=112504'], 15: ['中国TOP排行榜(内地榜)', '/discover/toplist?id=64016'], 16: ['香港电台中文歌曲龙虎榜', '/discover/toplist?id=10169002'], 17: ['华语金曲榜', '/discover/toplist?id=4395559'], 18: ['中国嘻哈榜', '/discover/toplist?id=1899724'], 19: ['法国 NRJ EuroHot 30周榜', '/discover/toplist?id=27135204'], 20: ['台湾Hito排行榜', '/discover/toplist?id=112463'], 21: ['Beatport全球电子舞曲榜', '/discover/toplist?id=3812895'] } self.User_List_All = ["用户热门歌单", "/discover/playlist"] host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=host, port=int(port), decode_responses=True) self.session = requests.session() self.headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0', 'Referer': 'http://music.163.com/', 'Content-Type': 'application/x-www-form-urlencoded' } self.play_url = "http://music.163.com/weapi/song/enhance/player/url?csrf_token=" self.br = "128000" self.play_default = "{\"ids\":\"[%s]\",\"br\":%s\
def requests_comment(self, music_id, proxies=''): if proxies == '': self.post_data = AES.encrypted_request(self.play_default % (music_id, self.br)) resp = self.session.post(url=self.comment_url % (music_id), data=self.post_data, headers=self.headers) else: resp = self.session.post(url=self.comment_url % (music_id), data=self.post_data, headers=self.headers, proxies=proxies) try: resp = resp.json() except: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=str(host), port=int(port), db=4) random_int = random.sample(range(0, self.r.dbsize()), 1) proxies = self.r.get(str(random_int[0])) self.requests_comment(music_id, eval(proxies)) self.requ_date["song"]["comments"] = [] for item in resp["hotComments"]: like = item["likedCount"] username = item['user']["nickname"] comment_content = item["content"] comment_timestamp = int(str(item["time"])[:-3]) dateArray = datetime.datetime.utcfromtimestamp(comment_timestamp) comment_time = dateArray.strftime("%Y--%m--%d %H:%M:%S") self.requ_date["song"]["comments"].append({ "comment_time": comment_time, "comment_content": comment_content, "likecount": like, "username": username }) return self.requ_date["song"]["comments"]
def requests_comment(self, music_id, proxies=''): if proxies == '': resp = self.session.post(url=self.comment_url % (music_id), data=self.post_data, headers=self.headers) else: resp = self.session.post(url=self.comment_url % (music_id), data=self.post_data, headers=self.headers, proxies=proxies) try: resp = resp.json() except: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=str(host), port=int(port), db=4) random_int = random.sample(range(0, self.r.dbsize()), 1) proxies = self.r.get(str(random_int[0])) self.requests_comment(music_id, eval(proxies)) try: self.like = resp["hotComments"][0]["likedCount"] self.username = resp["hotComments"][0]['user']["nickname"] self.comment_content = resp["hotComments"][0]["content"] self.comment_timestamp = int( str(resp["hotComments"][0]["time"])[:-3]) dateArray = datetime.datetime.utcfromtimestamp( self.comment_timestamp) self.comment_time = dateArray.strftime("%Y--%m--%d %H:%M:%S") self.requ_date["song"]["list"][0].update({ "comment_time": self.comment_time, "comment_content": self.comment_content, "likecount": self.like, "username": self.username }) except: self.requ_date["song"]["list"][0].update({"detail": "本首歌曲还没有评论~"})
def __init__(self): self.session = requests.session() self.headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13;rv:57.0) Gecko/20100101 Firefox/57.0', 'Referer': "http://music.163.com" } self.post_headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13;rv:57.0) Gecko/20100101 Firefox/57.0', 'Referer': "http://music.163.com", 'Content-Type': "application/x-www-form-urlencoded" } self.User_List_All = ["用户热门歌单",\ "/discover/playlist"] if int(config.getConfig("open_database", "redis")) == 1: host = config.getConfig("database", "dbhost") port = config.getConfig("database", "dbport") self.r = redis.Redis(host=host, port=int(port), decode_responses=True, db=1) # 连接到redis-1号数据库, 用于储存网易云音乐的用户热门歌单信息 self.NEMurl = "http://music.163.com"
def new_requests_play_url(self, music_id): global music_data new_music_id = [] if int(config.getConfig("open_database", "redis")) == 1: Search_Db = "NEM" + str(music_id) exist_bool = self.r.get(Search_Db) if not exist_bool: play_url = self.url_ % (music_id) self.r.set(Search_Db, play_url) else: play_url = exist_bool new_music_id = re.findall(r"url\?id=(\d{1,20})", exist_bool) else: play_url = self.url_ % (music_id) return play_url