1 from mygpo
.api
.models
import Episode
2 from collections
import defaultdict
5 # If 20% of the episodes of a podcast are of a given type,
6 # then the podcast is considered to be of that type, too
12 CONTENT_TYPES
= (_('image'), _('audio'), _('video'))
14 def get_podcast_types(podcast
):
15 """Returns the types of a podcast
17 A podcast is considered to be of a given types if the ratio of episodes that are of that type equals TYPE_THRESHOLD
19 episodes
= Episode
.objects
.filter(podcast
=podcast
, mimetype__isnull
=False)
22 t
= get_type(e
.mimetype
)
23 types
[t
] = types
.get(t
, 0) + 1
25 max_episodes
= sum(types
.itervalues())
26 l
= list(types
.iteritems())
27 l
.sort(key
=lambda x
: x
[1], reverse
=True)
29 return [x
[0] for x
in filter(lambda x
: max_episodes
/ float(x
[1]) >= TYPE_THRESHOLD
, l
)]
32 def get_type(mimetype
):
33 """Returns the simplified type for the given mimetype
35 All "wanted" mimetypes are mapped to one of audio/video/image
36 Everything else returns None
42 category
, type = mimetype
.split('/', 1)
43 if category
in ('audio', 'video', 'image'):
47 elif type == 'x-youtube':
51 def check_mimetype(mimetype
):
52 """Checks if the given mimetype can be processed by mygpo
55 category
, type = mimetype
.split('/', 1)
56 if category
in ('audio', 'video', 'image'):
59 # application/ogg is a valid mime type for Ogg files
60 # but we do not want to accept all files with application category
69 def get_mimetype(mimetype
, url
):
70 """Returns the mimetype; if None is given it tries to guess it"""
73 mimetype
, _encoding
= mimetypes
.guess_type(url
)