|
24 | 24 | from . import imdb |
25 | 25 | from . import musicbrainz as mb |
26 | 26 | from . import imagehosting |
| 27 | +from . import goodreads |
| 28 | +from .googlebooks import find_cover, find_categories |
27 | 29 | from .ffmpeg import FFMpeg |
28 | 30 | from . import templating as bb |
29 | 31 | from .submission import (Submission, form_field, finalize, cat_map, |
@@ -150,6 +152,7 @@ def copy(source, target): |
150 | 152 | 'movie': ['hard', 'sym', 'copy', 'move'], |
151 | 153 | 'tv': ['hard', 'sym', 'copy', 'move'], |
152 | 154 | 'music': ['copy', 'move'], |
| 155 | + 'book': ['copy', 'move'], |
153 | 156 | } |
154 | 157 |
|
155 | 158 | method_map = {'hard': os.link, |
@@ -930,6 +933,140 @@ def _render_form_description(self): |
930 | 933 | return self['description'] |
931 | 934 |
|
932 | 935 |
|
| 936 | +class BookSubmission(BbSubmission): |
| 937 | + |
| 938 | + _cat_id = 'book' |
| 939 | + _form_type = 'E-Books' |
| 940 | + |
| 941 | + def _desc(self): |
| 942 | + s = self['summary'] |
| 943 | + return re.sub('<[^<]+?>', '', s['description']) |
| 944 | + |
| 945 | + @form_field('book_retail', 'checkbox') |
| 946 | + def _render_retail(self): |
| 947 | + return bool( |
| 948 | + input('Is this a retail release? [y/N] ').lower() |
| 949 | + == 'y') |
| 950 | + |
| 951 | + @form_field('book_language') |
| 952 | + def _render_language(self): |
| 953 | + return self['summary']['language'] |
| 954 | + |
| 955 | + @form_field('book_publisher') |
| 956 | + def _render_publisher(self): |
| 957 | + return self['summary']['publisher'] |
| 958 | + |
| 959 | + @form_field('book_author') |
| 960 | + def _render_author(self): |
| 961 | + return self['summary']['authors'][0]['name'] |
| 962 | + |
| 963 | + @form_field('book_format') |
| 964 | + def _render_format(self): |
| 965 | + book_format = { |
| 966 | + 'EPUB': 'EPUB', |
| 967 | + 'MOBI': 'MOBI', |
| 968 | + 'PDF': 'PDF', |
| 969 | + 'HTML': 'HTML', |
| 970 | + 'TXT': 'TXT', |
| 971 | + 'DJVU': 'DJVU', |
| 972 | + 'CHM': 'CHM', |
| 973 | + 'CBR': 'CBR', |
| 974 | + 'CBZ': 'CBZ', |
| 975 | + 'CB7': 'CB7', |
| 976 | + 'TXT': 'TXT', |
| 977 | + 'AZW3': 'AZW3', |
| 978 | + } |
| 979 | + |
| 980 | + _, ext = os.path.splitext(self['path']) |
| 981 | + return book_format[ext.replace('.', '').upper()] |
| 982 | + |
| 983 | + def _render_summary(self): |
| 984 | + gr = goodreads.Goodreads() |
| 985 | + return gr.search(self['path']) |
| 986 | + |
| 987 | + @form_field('book_year') |
| 988 | + def _render_year(self): |
| 989 | + if 'summary' in self.fields: |
| 990 | + return self['summary']['publication_year'] |
| 991 | + else: |
| 992 | + while True: |
| 993 | + year = input('Please enter year: ') |
| 994 | + try: |
| 995 | + year = int(year) |
| 996 | + except ValueError: |
| 997 | + pass |
| 998 | + else: |
| 999 | + return year |
| 1000 | + |
| 1001 | + @form_field('book_isbn') |
| 1002 | + def _render_isbn(self): |
| 1003 | + if 'summary' in self.fields: |
| 1004 | + return self['summary'].get('isbn', '') |
| 1005 | + |
| 1006 | + @form_field('title') |
| 1007 | + def _render_form_title(self): |
| 1008 | + if 'summary' in self.fields: |
| 1009 | + return self['summary'].get('title', '') |
| 1010 | + |
| 1011 | + @form_field('tags') |
| 1012 | + def _render_tags(self): |
| 1013 | + categories = find_categories(self['summary']['isbn']) |
| 1014 | + authors = self['summary']['authors'] |
| 1015 | + return ",".join(uniq(list(format_tag(a['name']) for a in authors) + |
| 1016 | + list(format_tag(a) for a in categories))) |
| 1017 | + |
| 1018 | + def _render_section_information(self): |
| 1019 | + def gr_author_link(gra): |
| 1020 | + return bb.link(gra['name'], gra['link']) |
| 1021 | + |
| 1022 | + book = self['summary'] |
| 1023 | + links = [("Goodreads", book['url'])] |
| 1024 | + |
| 1025 | + return dedent("""\ |
| 1026 | + [b]Title[/b]: {title} ({links}) |
| 1027 | + [b]ISBN[/b]: {isbn} |
| 1028 | + [b]Publisher[/b]: {publisher} |
| 1029 | + [b]Publication Year[/b]: {publication_year} |
| 1030 | + [b]Rating[/b]: {rating} [size=1]({ratings_count} ratings)[/size] |
| 1031 | + [b]Author(s)[/b]: {authors}""").format( |
| 1032 | + links=", ".join(bb.link(*l) for l in links), |
| 1033 | + title=book['title'], |
| 1034 | + isbn=book['isbn'], |
| 1035 | + publisher=book['publisher'], |
| 1036 | + publication_year=book['publication_year'], |
| 1037 | + rating=bb.format_rating(float(book['average_rating']), |
| 1038 | + max=5), |
| 1039 | + ratings_count=book['ratings_count'], |
| 1040 | + authors=" | ".join(gr_author_link(a) for a in book['authors']) |
| 1041 | + ) |
| 1042 | + |
| 1043 | + def _render_section_description(self): |
| 1044 | + return self._desc() |
| 1045 | + |
| 1046 | + @form_field('desc') |
| 1047 | + def _render_description(self): |
| 1048 | + sections = [("Description", self['section_description']), |
| 1049 | + ("Information", self['section_information'])] |
| 1050 | + |
| 1051 | + description = "\n".join(bb.section(*s) for s in sections) |
| 1052 | + description += bb.release |
| 1053 | + |
| 1054 | + return description |
| 1055 | + |
| 1056 | + @finalize |
| 1057 | + @form_field('image') |
| 1058 | + def _render_cover(self): |
| 1059 | + # Goodreads usually won't give you a cover image as they don't have the |
| 1060 | + # the right to distribute them |
| 1061 | + if 'nophoto' in self['summary']['image_url']: |
| 1062 | + return find_cover(self['summary']['isbn']) |
| 1063 | + else: |
| 1064 | + return self['summary']['image_url'] |
| 1065 | + |
| 1066 | + def _finalize_cover(self): |
| 1067 | + return imagehosting.upload(self['cover']) |
| 1068 | + |
| 1069 | + |
933 | 1070 | class AudioSubmission(BbSubmission): |
934 | 1071 | default_fields = ("description", "form_tags", "year", "cover", |
935 | 1072 | "title", "format", "bitrate") |
|
0 commit comments