From e3293d425115510e43fab9b83744566c72c00d8d Mon Sep 17 00:00:00 2001 From: Chloe Date: Wed, 18 Nov 2020 18:20:06 +0100 Subject: [PATCH 1/2] option to print pdf file in current working directory --- .idea/.gitignore | 3 +++ .idea/inspectionProfiles/Project_Default.xml | 18 +++++++++++++ .../inspectionProfiles/profiles_settings.xml | 6 +++++ .idea/misc.xml | 4 +++ .idea/modules.xml | 8 ++++++ .idea/tswift.iml | 14 +++++++++++ .idea/vcs.xml | 6 +++++ requirements.txt | 2 ++ tswift.py | 25 ++++++++++++++++++- 9 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/tswift.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..4d308d1 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,18 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a157278 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2ff871d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/tswift.iml b/.idea/tswift.iml new file mode 100644 index 0000000..c299a89 --- /dev/null +++ b/.idea/tswift.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index bf4bf74..98f9335 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ requests>=2.7.0 twine>=1.5.0 wheel>=0.24.0 google>=1.9.1 +setuptools~=50.3.2 +fpdf~=1.7.2 \ No newline at end of file diff --git a/tswift.py b/tswift.py index 977df12..cfa104b 100755 --- a/tswift.py +++ b/tswift.py @@ -16,7 +16,7 @@ import re import random import sys - +from fpdf import FPDF ARTIST_URL = "https://www.metrolyrics.com/{artist}-alpage-{n}.html" SONG_URL = "https://www.metrolyrics.com/{title}-lyrics-{artist}.html" @@ -84,6 +84,20 @@ def load(self): return self + def printf(self): + """Print the lyrics to pdf file in current working directory""" + pdf = FPDF() + pdf.add_page() + pdf.set_font("Helvetica", size=12) + lines = self.format().split("\n") + for line in lines: + # deal with western european letters + line = line.encode(encoding='cp1252') + line = line.decode(encoding='latin-1', errors='replace') + pdf.cell(200, 5, txt=line, ln=1) + pdf.output(slugify(self.title) + '_' + slugify(self.artist) + '.pdf') + + @property def lyrics(self): if self._lyrics is None: @@ -190,6 +204,12 @@ def main(): help='Search for song by lyrics', required=False, ) + parser.add_argument( + '-f', '--to_pdf', + help='Print lyrics to pdf in current working directory', + action='store_true', + required=False + ) args = parser.parse_args() if args.lyrics: @@ -209,6 +229,9 @@ def main(): .format(args.artist)) sys.exit(1) + if song and args.to_pdf: + song.printf() + print(song.format()) From d506032e17177878e24eb4407a37e54bc66bdc82 Mon Sep 17 00:00:00 2001 From: Chloe Date: Wed, 18 Nov 2020 18:28:26 +0100 Subject: [PATCH 2/2] PEP8 remove one blank line --- tswift.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tswift.py b/tswift.py index cfa104b..feedd7f 100755 --- a/tswift.py +++ b/tswift.py @@ -97,7 +97,6 @@ def printf(self): pdf.cell(200, 5, txt=line, ln=1) pdf.output(slugify(self.title) + '_' + slugify(self.artist) + '.pdf') - @property def lyrics(self): if self._lyrics is None: