def fetch(self, id): proxy = random.choice(self.proxy_list) proxies = { "http": 'http://%s:%s' % (proxy.ip, proxy.port) } headers={ 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36' } url = 'http://www.xiami.com/song/%s' % id res = requests.get(url, headers=headers, proxies=proxies) parser = etree.HTMLParser() tree = etree.parse(StringIO(res.text), parser) # album_name & album_id album_a = tree.xpath('//table[@id="albums_info"]//tr[1]/td[2]//a') album_name = album_a[0].text album_id = album_a[0].get('href')[7:] album_id = int(album_id) # artist_name & artist_id artist_a = tree.xpath('//table[@id="albums_info"]//tr[2]/td[2]//a') artist_name = artist_a[0].text artist_id = artist_a[0].get('href')[8:] artist_id = int(artist_id) # play_count & share_count & comment_count url = 'http://www.xiami.com/count/getplaycount?id=%s&type=song' % id res = requests.get(url, headers=headers) play_count = res.json()['plays'] share_count = tree.xpath('//div[@class="music_counts"]//li[2]/text()') share_count = share_count[0] comment_count = tree.xpath('//div[@class="music_counts"]//li[3]/a/text()') comment_count = int(comment_count[0]) name = tree.xpath('//*[@id="title"]/h1/text()') name = name[0] dd = { "album_name": album_name, "album_id": album_id, "artist_name": artist_name, "artist_id": artist_id, "play_count": play_count, "share_count": share_count, "comment_count": comment_count, "name": name, "create_time": datetime.now(), "update_time": datetime.now() } Song.get_or_create(id=id, defaults=dd) logger.info('http://www.xiami.com/song/%s', id)
def create_song(user_name): users = User.query.filter_by(name=user_name).first() if not users: return json.dumps({'success': False, 'error': 'User not found!'}), 404 post_body = json.loads(request.data) song = Song( title = post_body.get('title', ''), reason = post_body.get('reason', ''), detail = post_body.get('detail', ''), vote = post_body.get('vote', '') ) users.songs.append(song) db.session.add(song) db.session.commit() return json.dumps({'success': True, 'data': song.serialize()}), 200
def route_api_songs_single(id): if request.method == "GET": try: song = Song.get(Song.id == id) except Song.DoesNotExist: raise APIError("Invalid Song ID") return APIResponse(song.to_dict()) else: try: song = Song.get(Song.id == id) print request.values song.title = request.values.get("title") song.artist = request.values.get("artist") song.album = request.values.get("album") song.save() except Song.DoesNotExist: raise APIError("Invalid Song ID") return APIResponse(song.to_dict())
def route_api_songs(): page = int(request.values.get("page", 1)) songs = Song.select(User.username, *map(lambda i: getattr(Song, i), Song.DICT_FIELDS)).join( User).paginate(page, 100).order_by(Song.added_date) return APIResponse({ "page": page, "songs": map(lambda i: i.to_dict(), list(songs)) })
def get_singer_data(mid, singer_name): #获取歌手mid,进入歌手详情页,也就是每一个歌手歌曲所在页面 #找出歌手的歌曲信息页 data = '{"comm":{"ct":24,"cv":0},"singerSongList":{"method":"GetSingerSongList","param":'\ '{"order":1,"singerMid":"%s","begin":0,"num":10}'\ ',"module":"musichall.song_list_server"}}'%(str(mid)) sign = get_sign(data) url = 'https://u.y.qq.com/cgi-bin/musics.fcg?-=getSingerSong4707786209273719'\ '&g_tk=5381&sign={}&loginUin=0'\ '&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'\ '&data={}'.format(sign,parse.quote(data)) html = requests.get(url, headers=headers).json() songs_num = html['singerSongList']['data']['totalNum'] #获取歌曲总数 for number in range(0, songs_num, 100): data = '{"comm":{"ct":24,"cv":0},"singerSongList":{"method":"GetSingerSongList","param":'\ '{"order":1,"singerMid":"%s","begin":%s,"num":%s}'\ ',"module":"musichall.song_list_server"}}'%(str(mid),str(number),str(songs_num)) sign = get_sign(data) url = 'https://u.y.qq.com/cgi-bin/musics.fcg?-=getSingerSong4707786209273719'\ '&g_tk=5381&sign={}&loginUin=0'\ '&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'\ '&data={}'.format(sign,parse.quote(data)) html = requests.get(url, headers=headers).json() datas = html['singerSongList']['data']['songList'] for d in datas: sing_name = d['songInfo']['title'] song_mid = d['songInfo']['mid'] try: lock.acquire() #锁上 session.add( Song(song_name=sing_name, song_singer=singer_name, song_mid=song_mid)) session.commit() lock.release() #解锁 print('commit') except: session.rollback() print('rollbeak') print('歌手名字:{}\t歌曲名字:{}\t歌曲ID:{}'.format(singer_name, sing_name, song_mid)) download(song_mid, sing_name, singer_name)
def switch_mode(self, mode): self.mode = Controller.Mode.RANDOM if mode == Controller.Mode.RANDOM: self.cli.consume(0) self.cli.random(1) self.cli.repeat(1) self.cli.single(0) self.cli.clear() # Load up a ton of random songs playlist = Song.as_mpd_playlist(Song.select()) map(self.cli.add, playlist) if mode == Controller.Mode.QUEUE: self.cli.consume(1) self.cli.random(0) self.cli.repeat(0) self.cli.single(0) self.cli.clear()
def route_player_queue_song(): # If we're in random mode we need to empty the playlist and start over if app.controller.mode == Controller.Mode.RANDOM: app.controller.switch_mode(Controller.Mode.QUEUE) try: app.controller.add_song(Song.get(Song.id == request.values.get("song"))) except Song.DoesNotExist: return APIError("Invalid Song ID") return APIResponse()
def status(self): current_song = self.cli.currentsong() status = self.cli.status().items() + current_song.items() if 'title' in current_song: try: s = Song.get(Song.title == current_song['title']) status += s.to_dict().items() except Song.DoesNotExist: pass status = dict(status) status['playlist'] = self.cli.playlistinfo() return status
def get_singer_data(mid, singer_name): #获取歌手mid,进入歌手详情页,也就是每一个歌手歌曲所在页面 #找出歌手的歌曲信息页 params = '{"comm":{"ct":24,"cv":0},"singerSongList":{"method":"GetSingerSongList",'\ '"param":{"order":1,"singerMid":"%s","begin":0,"num":10},'\ '"module":"musichall.song_list_server"}}'%str(mid) url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?-=getSingerSong9513357793133783&'\ 'g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8'\ '¬ice=0&platform=yqq.json&needNewCode=0*&data={}'.format(parse.quote(params)) html = requests.session() content = html.get(url, headers=headers).json() songs_num = content['singerSongList']['data']['totalNum'] for a in range(0, songs_num, 100): params = '{"comm":{"ct":24,"cv":0},"singerSongList":{"method":"GetSingerSongList",' \ '"param":{"order":1,"singerMid":"%s","begin":%s,"num":%s},' \ '"module":"musichall.song_list_server"}}' % (str(mid), int(a),int(songs_num)) url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?-=getSingerSong9513357793133783&' \ 'g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8' \ '¬ice=0&platform=yqq.json&needNewCode=0*&data={}'.format(parse.quote(params)) html = requests.session() content = html.get(url, headers=headers).json() datas = content['singerSongList']['data']['songList'] for d in datas: sing_name = d['songInfo']['title'] songmid = d['songInfo']['mid'] try: lock.acquire() #锁上 session.add( Song(song_name=sing_name, song_singer=singer_name, song_mid=songmid)) session.commit() lock.release() #解锁 print('commit') except: session.rollback() print('rollbeak') print('歌手名字:{}\t歌曲名字:{}\t歌曲ID:{}'.format(singer_name, sing_name, mid)) download(songmid, sing_name, singer_name)
def create_song(song: RequestSongObject) -> ResultWithData[str]: song_id = short_unique_id() song_numbers = list(select(s.number for s in Song)) if song_numbers: highest_song_nbr = max(song_numbers) + 1 else: highest_song_nbr = 1 song_created = Song( song_id=song_id, number=highest_song_nbr, title=song.title, melody=song.melody, author=song.author, text=song.text ) return get_result_with_data(song_created.song_id)
def route_api_playlist_modify(id, action): try: playlist = Playlist.get(Playlist.id == id) except Playlist.DoesNotExist: raise APIError("Invalid Playlist ID") if not playlist.can_user_modify(g.user): raise AuthException(msg="Cannot edit private playlist we do not own") try: song = Song.get(Song.id == request.values.get("song")) except Song.DoesNotExist: raise APIError("Invalid Song ID") # Try adding to the playlist if action == "add": playlist.add_song(song) return APIResponse({"pos": playlist.get_songs().count()}) if action == "remove": playlist.rmv_song(song) return APIResponse({})
def post(self): delete = self.request.get('delete-all') if (delete != ""): entities = Song.query().fetch() for entity in entities: entity.key.delete() song = self.request.get('song-name') club = self.request.get('club-name') ancestor_key = ndb.Key('club', club) songs = Song.query().fetch() songs_d = {"songsss": []} found = False if (song != ""): for song2 in songs: if song == song2.song_name: logging.info(song) logging.info(song2.key) update_song = song2.key.get() logging.info(update_song) update_song.current_score += 1 logging.info(song2.key) update_song.put() found = True break if (not found): song_record = Song(song_name=song, club_num=club, current_score=1) song_record.put() songs_d["songsss"].append(song) for song2 in songs: if (song2.club_num == club): songs_d["songsss"].append(song2.song_name) if (len(songs_d) == 0): songs_d["songsss"].append( "Please add a song to see your playlist!") template = template_env.get_template('html/playlist.html') self.response.write(template.render(songs_d))
def remove_song(song_id: str) -> ResultWithData: Song.get(song_id=song_id).delete() return get_result_with_data({})
def get_song_by_name(title: str) -> ResultWithData[SongObject]: song = Song.get(title=title) if song is None: return get_result_with_error(SONG_TITLE_NOT_EXIST) else: return get_result_with_data(db_song_to_song_object(song))
def get_songs() -> List[SongObject]: songs = Song.select(lambda t: True) return [db_song_to_song_object(song) for song in songs]
def route_upload(): f = request.files["file"] if f and allowed_file(f.filename): return jsonify({"success": Song.new_from_file(g.user, f)}) raise APIError("No or invalid file specified")
def get_song_by_id(song_id: str) -> ResultWithData[SongObject]: song = Song.get(song_id=song_id) if song is None: return get_result_with_error(SONG_ID_NOT_EXIST) else: return get_result_with_data(db_song_to_song_object(song))
def getSongsData(self, test): songs = Song.query().fetch() d = [] for song in sorted(songs): d.append((song.song - name, song.current_score)) logging.info(d)