forked from lkiesow/python-feedgen
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcategory.py
304 lines (281 loc) · 9.71 KB
/
category.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -*- coding: utf-8 -*-
"""
podgen.categories
~~~~~~~~~~~~~~~
This module contains Category, which represents a single iTunes categories.
:copyright: 2016, Thorben Dahl <thorben@sjostrom.no>
:license: FreeBSD and LGPL, see license.* for more details.
"""
# Support for Python 2.7
from __future__ import absolute_import, division, print_function, unicode_literals
from builtins import *
import warnings
from podgen.warnings import LegacyCategoryWarning
class Category(object):
"""Immutable class representing an Apple Podcasts categories.
By using this class, you can be sure that the chosen categories is a
valid categories, that it is formatted correctly and you will be warned
when using an old categories.
See https://help.apple.com/itc/podcasts_connect/#/itc9267a2f12 for an
overview of the available categories and their subcategories.
.. versionchanged:: 1.1.0
Updated to reflect `the new categories <https://podnews.net/article/apple-changed-podcast-categories-2019>`_
as of August 9th 2019 and yield a
:class:`~podgen.warnings.LegacyCategoryWarning` when using one of the
old categories.
.. note::
The categories are case-insensitive, and you may escape ampersands.
The categories and subcategory will end up properly capitalized and
with unescaped ampersands.
Example::
>> from podgen import Category
>> c = Category("Music")
>> c.categories
Music
>> c.subcategory
None
>>
>> d = Category("games & hobbies", "Video games")
>> d.categories
Games & Hobbies
>> d.subcategory
Video Games
"""
_legacy_categories = {
'Arts': ['Design', 'Fashion & Beauty', 'Food', 'Literature',
'Performing Arts', 'Visual Arts'],
'Business': ['Business News', 'Careers', 'Investing',
'Management & Marketing', 'Shopping'],
'Comedy': [],
'Education': ['Education', 'Education Technology',
'Higher Education', 'K-12', 'Language Courses', 'Training'],
'Games & Hobbies': ['Automotive', 'Aviation', 'Hobbies',
'Other Games', 'Video Games'],
'Government & Organizations': ['Local', 'National', 'Non-Profit',
'Regional'],
'Health': ['Alternative Health', 'Fitness & Nutrition', 'Self-Help',
'Sexuality'],
'Kids & Family': [],
'Music': [],
'News & Politics': [],
'Religion & Spirituality': ['Buddhism', 'Christianity', 'Hinduism',
'Islam', 'Judaism', 'Other', 'Spirituality'],
'Science & Medicine': ['Medicine', 'Natural Sciences',
'Social Sciences'],
'Society & Culture': ['History', 'Personal Journals', 'Philosophy',
'Places & Travel'],
'Sports & Recreation': ['Amateur', 'College & High School',
'Outdoor', 'Professional'],
'Technology': ['Gadgets', 'Tech News', 'Podcasting',
'Software How-To'],
'TV & Film': []
}
_categories = {
'Arts': [
'Books',
'Design',
'Fashion & Beauty',
'Food',
'Performing Arts',
'Visual Arts',
],
'Business': [
'Careers',
'Entrepreneurship',
'Investing',
'Management',
'Marketing',
'Non-Profit',
],
'Comedy': [
'Comedy Interviews',
'Improv',
'Stand-up',
],
'Education': [
'Courses',
'How To',
'Language Learning',
'Self-Improvement',
],
'Fiction': [
'Comedy Fiction',
'Drama',
'Science Fiction',
],
'Government': [],
'History': [],
'Health & Fitness': [
'Alternative Health',
'Fitness',
'Medicine',
'Mental Health',
'Nutrition',
'Sexuality',
],
'Kids & Family': [
'Education for Kids',
'Parenting',
'Pets & Animals',
'Stories for Kids',
],
'Leisure': [
'Animation & Manga',
'Automotive',
'Aviation',
'Crafts',
'Games',
'Hobbies',
'Home & Garden',
'Video Games',
],
'Music': [
'Music Commentary',
'Music History',
'Music Interviews',
],
'News': [
'Business News',
'Daily News',
'Entertainment News',
'News Commentary',
'Politics',
'Sports News',
'Tech News',
],
'Religion & Spirituality': [
'Buddhism',
'Christianity',
'Hinduism',
'Islam',
'Judaism',
'Religion',
'Spirituality',
],
'Science': [
'Astronomy',
'Chemistry',
'Earth Sciences',
'Life Sciences',
'Mathematics',
'Natural Sciences',
'Nature',
'Physics',
'Social Sciences',
],
'Society & Culture': [
'Documentary',
'Personal Journals',
'Philosophy',
'Places & Travel',
'Relationships',
],
'Sports': [
'Baseball',
'Basketball',
'Cricket',
'Fantasy Sports',
'Football',
'Golf',
'Hockey',
'Rugby',
'Running',
'Soccer',
'Swimming',
'Tennis',
'Volleyball',
'Wilderness',
'Wrestling',
],
'Technology': [],
'True Crime': [],
'TV & Film': [
'After Shows',
'Film History',
'Film Interviews',
'Film Reviews',
'TV Reviews',
],
}
def __init__(self, categories):
"""Create new Category object. See the class description of
:class:´~podgen.category.Category`.
:param categories: Categories and subcategories of the podcast.
:type categories: list of tuples
"""
if not categories:
raise TypeError("categories must be provided, was \"%s\"" % categories)
canonical_categories = []
try:
for category in categories:
try:
subcategory = category[1]
except IndexError:
subcategory = None
canonical_category, canonical_subcategory = self._look_up_category(
category[0],
subcategory,
self._categories,
)
canonical_categories.append((canonical_category, canonical_subcategory))
except ValueError:
# Maybe this is a legacy categories?
for category in categories:
try:
subcategory = category[1]
except IndexError:
subcategory = None
canonical_category, canonical_subcategory = self._look_up_category(
category[0],
subcategory,
self._legacy_categories,
)
# Okay, it is, warn about this
warnings.warn(
'The categories ("%s", "%s") is a legacy categories. Please switch '
'to one of the new Apple Podcast categories.' %
(canonical_category, canonical_subcategory),
category=LegacyCategoryWarning,
stacklevel=2
)
canonical_categories.append((canonical_category, canonical_subcategory))
self.__categories = canonical_categories
# self.__subcategory = canonical_subcategory
def _look_up_category(
self,
category,
subcategory,
available_categories
):
# Do a case-insensitive search for the categories
search_category = category.strip().replace("&", "&").lower()
for actual_category in available_categories:
if actual_category.lower() == search_category:
# We found it
canonical_category = actual_category
break
else: # no break
raise ValueError('Invalid categories "%s"' % category)
# Do a case-insensitive search for the subcategory, if provided
canonical_subcategory = None
if subcategory is not None:
search_subcategory = subcategory.strip().replace("&", "&")\
.lower()
for actual_subcategory in available_categories[canonical_category]:
if actual_subcategory.lower() == search_subcategory:
canonical_subcategory = actual_subcategory
break
else: # no break
raise ValueError('Invalid subcategory "%s" under categories "%s"'
% (subcategory, canonical_category))
return canonical_category, canonical_subcategory
@property
def categories(self):
"""The categories represented by this object. Read-only.
:type: :obj:`list`
"""
return self.__categories
# Make this attribute read-only by not implementing setter
def __repr__(self):
return 'Category(categories=%s)' % \
self.categories