-
Notifications
You must be signed in to change notification settings - Fork 11
/
lyrics
executable file
·140 lines (120 loc) · 4.77 KB
/
lyrics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
"""
Copyright 2009~2018 Bart Nagel ([email protected]) and other authors
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 3 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 <https://www.gnu.org/licenses/>.
"""
from __future__ import print_function
from lyrics import *
import optparse
optionparser = optparse.OptionParser(
usage="%prog [options] [<artist> <title>]",
version="%prog " + VERSION,
description="Get data from lyrics.wikia.com for a particular song. If "
"the artist and song are not given as arguments the currently "
"playing song is taken from MPD via the mpc command.",
epilog="To open a browser, sensible-browser is used.")
group = optparse.OptionGroup(optionparser, "Modes")
group.add_option("--mode", default="output",
choices=["output", "viewurl", "view", "editurl", "edit"],
help="Mode of operation. Default is %default, can alternatively be any "
"of the following.")
group.add_option("--output", action="store_const", dest="mode",
const="output", help="Shorthand for --mode=output (default). Output "
"the lyrics as plain text to stdout.")
group.add_option("--viewurl", action="store_const", dest="mode",
const="viewurl", help="Shorthand for --mode=viewurl. Print the URL of "
"the lyrics page.")
group.add_option("--view", action="store_const", dest="mode",
const="view", help="Shorthand for --mode=view. Open a browser at the "
"lyrics page.")
group.add_option("--editurl", action="store_const", dest="mode",
const="editurl", help="Shorthand for --mode=editurl. Print the URL of "
"the edit lyrics page.")
group.add_option("--edit", action="store_const", dest="mode",
const="edit", help="Shorthand for --mode=edit. Open a browser at the "
"edit lyrics page.")
optionparser.add_option_group(group)
group = optparse.OptionGroup(optionparser, "Options")
group.add_option("--suppresstitle", "-s", action="store_true",
help="Don't output the artist name and song title")
def set_encoding(option, opt_str, value, parser):
try:
"test".encode(value)
except LookupError as e:
raise optparse.OptionValueError(str(e))
parser.values.encoding = value
group.add_option("--encoding", action="callback", callback=set_encoding,
type="string", default=None,
help="Force the output encoding. The default is to detect the tty "
"encoding if stdout is a tty, otherwise to use UTF-8. Allowed "
"values are the standard Python encodings and their aliases, "
"listed at https://docs.python.org/3/library/"
"codecs.html#standard-encodings")
optionparser.add_option_group(group)
# parse the commandline
(options, args) = optionparser.parse_args()
if len(args) == 0:
artist = None
title = None
elif len(args) == 2:
(artist, title) = args
else:
optionparser.error("Expected either two non-option arguments (artist and "
"title) or zero")
# get currently playing song if one wasn't on the commandline
if title is None:
try:
(artist, title) = currentlyplaying()
except OSError:
print("Couldn't get currently playing song -- ensure "
"you have mpc", file=sys.stderr)
sys.exit(8)
except TypeError:
# None was returned since no song is playing
print("No song is currently playing", file=sys.stderr)
sys.exit(1)
# do our action
if options.mode == "output":
try:
lyrics = getlyrics(artist, title)
except IOError:
print("Lyrics not found", file=sys.stderr)
sys.exit(4)
except IndexError:
print("No lyrics tag", file=sys.stderr)
sys.exit(2)
instrumental = lyrics is None
output = ""
if not options.suppresstitle:
output += "%s -- %s\n\n" % (artist, title)
if lyrics is None:
output += "(instrumental)"
else:
output += str(lyrics)
# decide encoding if it wasn't specified
if options.encoding is None:
if sys.stdout.isatty():
options.encoding = sys.stdout.encoding
else:
options.encoding = "utf-8"
# print the output, with a warning if it couldn't be encoded fully
try:
print(output.encode(options.encoding).decode('utf-8'))
except UnicodeEncodeError:
print("warning: could not encode to encoding '{}'; "
"some characters are replaced".format(options.encoding), file=sys.stderr)
print(output.encode(options.encoding, "replace").decode('utf-8'))
sys.exit(0)
url = lyricwikiurl(artist, title, edit=options.mode[0:4] != "view")
if options.mode[4:] == "url":
print(url)
else:
subprocess.Popen(["sensible-browser", url])