1 from collections
import defaultdict
5 from django
.utils
.translation
import ugettext_lazy
as _
8 # If 20% of the episodes of a podcast are of a given type,
9 # then the podcast is considered to be of that type, too
13 CONTENT_TYPES
= (_('image'), _('audio'), _('video'))
15 def get_podcast_types(episodes
):
16 """Returns the types of a podcast
18 A podcast is considered to be of a given types if the ratio of episodes that are of that type equals TYPE_THRESHOLD
20 has_mimetype
= lambda e
: e
.mimetypes
21 episodes
= filter(has_mimetype
, episodes
)
24 for mimetype
in e
.mimetypes
:
25 t
= get_type(mimetype
)
28 types
[t
] = types
.get(t
, 0) + 1
30 max_episodes
= sum(types
.values())
31 l
= list(types
.items())
32 l
.sort(key
=lambda x
: x
[1], reverse
=True)
35 filter(lambda x
: max_episodes
/ float(x
[1]) >= TYPE_THRESHOLD
, l
)]
38 def get_type(mimetype
):
39 """Returns the simplified type for the given mimetype
41 All "wanted" mimetypes are mapped to one of audio/video/image
42 Everything else returns None
48 category
, type = mimetype
.split('/', 1)
49 if category
in ('audio', 'video', 'image'):
53 elif type == 'x-youtube':
55 elif type == 'x-vimeo':
60 def get_mimetype(mimetype
, url
):
61 """Returns the mimetype; if None is given it tries to guess it"""
64 mimetype
, _encoding
= mimetypes
.guess_type(url
)