def __init__(self): """Init """ super().__init__() self.netease_req = NeteaseRequest() self.parser = LyricParser() self.spotifydbus = SpotifyDBus() self.not_found_text = 'Not Found Any Lyrics!' self.lyric = self.not_found_text self.current_playing = '' self.init_ui()
class SpotifyricsWin(QWidget): """Spotifyrics GUI Window.""" refresh_btn: QPushButton scroll_area: QScrollArea layout: QVBoxLayout lyric_label: QLabel def __init__(self): """Init """ super().__init__() self.netease_req = NeteaseRequest() self.parser = LyricParser() self.spotifydbus = SpotifyDBus() self.not_found_text = 'Not Found Any Lyrics!' self.lyric = self.not_found_text self.current_playing = '' self.init_ui() def init_ui(self): self.setGeometry(100, 100, 500, 600) self.setWindowTitle('Spotifyircs') self.lyric_label = QLabel() self.lyric_label.setText(self.lyric) self.scroll_area = QScrollArea() self.scroll_area.setWidgetResizable(True) self.scroll_area.setWidget(self.lyric_label) self.refresh_btn = QPushButton() self.refresh_btn.setText('Refresh') self.refresh_btn.clicked.connect(self.refresh) self.layout = QVBoxLayout() self.layout.addWidget(self.scroll_area) self.layout.addWidget(self.refresh_btn) self.setLayout(self.layout) self.show() @pyqtSlot() def refresh(self): keywords = self.spotifydbus.get_keywords() if keywords == self.current_playing: pass self.current_playing = keywords self.lyric = self.not_found_text original_lyric, transed_lyric = self.netease_req.get_lyrics(keywords) lyric_dict = self.parser.parse(original_lyric, transed_lyric) if lyric_dict: self.lyric = reduce(lambda x, y: x + y, lyric_dict.values()) self.lyric_label.setText(self.lyric)
def get_gifs_by_keyword(self, keyword, offset=0, count=40, dont_parse=False): if not dont_parse: #Pick the most gif friendly keyword available keyword = LyricParser.get_keyword(keyword) params = {'api_key': self.api_key} params['q'] = keyword params['limit'] = count params['offset'] = offset search_request_url = "%s?%s" % (self.search_by_keyword_endpoint, urllib.urlencode(params)) try: return simplejson.loads(requests.get(search_request_url, timeout=5).text) except Exception: return {}
def singif_api(): provided_song_name = request.args.get("song_name", "") provided_lyrics = request.args.get("song_lyrics", "") if not provided_song_name: json_response = simplejson.dumps(sample_response) else: song_response = {} song_gifs = [] song_lyrics = [] lyric_engine = MetroLyrics() gif_engine = Giphy() song_lyrics_meta = lyric_engine.get_song_lyrics_by_name(provided_song_name) song_meta = deepcopy(song_lyrics_meta) song_title = song_meta.get('title','') song_artist = song_meta.get('artist', '') #Find some generic gifs for the song using the song title or artist generic_gifs = [] if song_title: search_results = gif_engine.get_gifs_by_keyword(song_title) search_results = search_results.get('data', []) if search_results: for result in search_results: result_url = result.get('images', {}).get('original', {}).get('url', '') if result_url: generic_gifs.append(result_url) if not generic_gifs and song_artist: search_results = gif_engine.get_gifs_by_keyword(song_artist, dont_parse=True) search_results = search_results.get('data', []) if search_results: for result in search_results: result_url = result.get('images', {}).get('original', {}).get('url', '') if result_url: generic_gifs.append(result_url) song_meta['original_url'] = "https://www.youtube.com/watch?v=hroUeu4IvpE" song_meta['embed_type'] = 'youtube' song_meta['id'] = 'hroUeu4IvpE' song_meta['internal_id'] = 'test' if not provided_lyrics: full_song_lyrics = song_lyrics_meta.get('lyrics') else: full_song_lyrics = provided_lyrics individual_lyrics = LyricParser.get_lyric_lines(full_song_lyrics) current_timestamp = 0 for lyric in individual_lyrics: lyric_duration = LyricParser.estimate_duration(lyric) lyric_info = {} lyric_info['text'] = lyric lyric_info['ts'] = current_timestamp current_timestamp += lyric_duration song_lyrics.append(lyric_info) lyric_gifs = gif_engine.get_gif_for_line(lyric_info, duration=lyric_duration) if lyric_gifs: song_gifs.extend(lyric_gifs) elif generic_gifs: random_generic_gif_url = generic_gifs.pop() lyric_gifs = [{"url": random_generic_gif_url, "ts": lyric_info['ts']}] song_gifs.extend(lyric_gifs) song_response['gifs'] = song_gifs song_response['lines'] = song_lyrics song_response['meta'] = song_meta if song_gifs: song_response['status'] = 200 else: song_response['status'] = 404 song_response['error'] = {"code": 101, "mesg": "Song not found"} json_response = simplejson.dumps(song_response) resp = Response(json_response, status=200, mimetype='application/json') resp.headers['X-Creator'] = "W.A.S.T.E" resp.headers['Access-Control-Allow-Origin'] = "*" return resp
#!/usr/bin/python from netrequest import NeteaseRequest from lyric_parser import LyricParser if __name__ == '__main__': netease_req = NeteaseRequest() original_lyric, transed_lyric = netease_req.get_lyrics() parser = LyricParser() lyric_dict = parser.parse(original_lyric, transed_lyric) if lyric_dict: for value in lyric_dict.values(): print(value)