-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parser for reading from the song's tags or from a local .lrc/.lyric file #79
Open
alpemwarrior
wants to merge
5
commits into
timoloewe:master
Choose a base branch
from
alpemwarrior:LocalFileLyrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb948ad
Adds a new parser that gets the lyrics from a local .lrc or .lyric file
87a03e6
Some bugfixes on LocalParser.py
6cc0899
Removed unnecessary library and edited the README to mention the use …
d0a9be9
Cleaning and rewriting stuff
dc454a5
Cleaning and changing stuff
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 2 of the License, or | ||
# (at your option) any later version. | ||
|
||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
|
||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
""" | ||
Parses lyrics from a .lyric or .lrc file or from the tags of the track. | ||
""" | ||
|
||
import urllib | ||
import os.path as path | ||
from mutagen.oggvorbis import OggVorbis | ||
from mutagen.oggopus import OggOpus | ||
from mutagen.flac import FLAC | ||
from mutagen.id3 import ID3 | ||
|
||
|
||
class Parser(object): | ||
def __init__(self, artist, title, track_path): | ||
self.artist = artist | ||
self.title = title | ||
self.track_path = track_path | ||
|
||
def parse(self): | ||
# convert the song url to a usable path | ||
path = self.track_path | ||
path = urllib.parse.unquote(path) | ||
path = path.replace("file://", "") | ||
dir = path[0:((path.rfind("/")) + 1)] | ||
file_name = path[((path.rfind("/")) + 1):path.rfind(".")] | ||
out = "" | ||
if path.endswith("mp3"): | ||
out = self.get_id3_lyrics(path) | ||
elif path.endswith("ogg"): | ||
out = self.get_ogg_lyrics(path) | ||
elif path.endswith("opus"): | ||
out = self.get_opus_lyrics(path) | ||
elif path.endswith("flac"): | ||
out = self.get_flac_lyrics(path) | ||
if out is None: | ||
file_path = self.check_for_file(dir, file_name) | ||
if file_path is None: | ||
return "" | ||
with open(file_path) as file: | ||
return file.read() | ||
return out | ||
|
||
def check_for_file(self, dir, file_name): | ||
""" | ||
This only checks for .lrc or .lyric files with the same name as the track or with the same "cleaned" name as | ||
the track. If you have files in any other format, please add it to this function. | ||
""" | ||
if path.isfile(dir + file_name + ".lrc"): | ||
return dir + file_name + ".lrc" | ||
elif path.isfile(dir + file_name + ".lyric"): | ||
return dir + file_name + ".lyric" | ||
elif path.isfile(dir + self.title + ".lrc"): | ||
return dir + self.title + ".lrc" | ||
elif path.isfile(dir + self.title + ".lyric"): | ||
return dir + self.title + ".lyric" | ||
else: | ||
return None | ||
|
||
def get_id3_lyrics(self, path): | ||
try: | ||
file = ID3(path) | ||
except: | ||
return None | ||
if len(file.getall("USLT")) > 0: | ||
out = file.getall("USLT")[0] | ||
return out.text | ||
else: | ||
return None | ||
|
||
def get_opus_lyrics(self, path): | ||
file = OggOpus(path) | ||
return self.get_vorbis_style_lyrics(file) | ||
|
||
def get_ogg_lyrics(self, path): | ||
file = OggVorbis(path) | ||
return self.get_vorbis_style_lyrics(file) | ||
|
||
def get_flac_lyrics(self, path): | ||
file = FLAC(path) | ||
return self.get_vorbis_style_lyrics(file) | ||
|
||
def get_vorbis_style_lyrics(self, file_tags): | ||
""" | ||
Returns lyrics from a mutagen file object that uses vorbis-like tags (FLAC, Vorbis and opus) | ||
""" | ||
out = "" | ||
try: | ||
out = file_tags["LYRICS"] | ||
except: | ||
try: | ||
out = file_tags["UNSYNCEDLYRICS"] | ||
except: | ||
pass | ||
if out != "": | ||
return out[0] | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
from gi.repository import GdkPixbuf | ||
from gi.repository import GLib | ||
|
||
import LocalParser | ||
import ChartlyricsParser | ||
import LyricwikiParser | ||
import MetrolyricsParser | ||
|
@@ -94,7 +95,7 @@ | |
LYRICS_ARTIST_REPLACE = [("/", "-"), (" & ", " and ")] | ||
|
||
LYRICS_SOURCES = ["Lyricwiki.org", "Letras.terra.com.br", "Metrolyrics.com", "AZLyrics.com", "Lyricsmania.com", | ||
"Vagalume.com.br", "Genius.com", "Darklyrics.com", "Chartlyrics.com"] | ||
"Vagalume.com.br", "Genius.com", "Darklyrics.com", "Chartlyrics.com", "Local File"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any i18n opportunity for plugins? |
||
|
||
|
||
class lLyrics(GObject.Object, Peas.Activatable): | ||
|
@@ -117,7 +118,7 @@ def do_activate(self): | |
"Metrolyrics.com": MetrolyricsParser, "AZLyrics.com": AZLyricsParser, | ||
"Lyricsmania.com": LyricsmaniaParser, "Chartlyrics.com": ChartlyricsParser, | ||
"Darklyrics.com": DarklyricsParser, "Genius.com": GeniusParser, | ||
"Vagalume.com.br": VagalumeParser}) | ||
"Vagalume.com.br": VagalumeParser, "Local File": LocalParser}) | ||
self.add_builtin_lyrics_sources() | ||
|
||
# Get the user preferences | ||
|
@@ -198,6 +199,7 @@ def do_deactivate(self): | |
self.lyrics_before_edit = None | ||
self.edit_event = None | ||
self.path_before_edit = None | ||
self.song_url = None | ||
self.sources = None | ||
self.cache = None | ||
self.lyrics_folder = None | ||
|
@@ -472,6 +474,7 @@ def search_lyrics(self, player, entry): | |
# get the song data | ||
self.artist = entry.get_string(RB.RhythmDBPropType.ARTIST) | ||
self.title = entry.get_string(RB.RhythmDBPropType.TITLE) | ||
self.song_url = entry.get_string(RB.RhythmDBPropType.LOCATION) | ||
|
||
print("search lyrics for " + self.artist + " - " + self.title) | ||
|
||
|
@@ -812,8 +815,10 @@ def get_lyrics_from_source(self, source, artist, title): | |
|
||
print("source: " + source) | ||
self.current_source = source | ||
|
||
parser = self.dict[source].Parser(artist, title) | ||
if source == "Local File": | ||
parser = self.dict[source].Parser(artist, title, self.song_url) | ||
else: | ||
parser = self.dict[source].Parser(artist, title) | ||
try: | ||
lyrics = parser.parse() | ||
except Exception as e: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW there's a modern pathlib out there, but otherwise it's not really much different.