Fix byte/str mix in get_git_head()
[mygpo.git] / mygpo / data / mimetype.py
blobdb298f24e3d875e4ca72beb28d2a2685d64277e9
1 from collections import defaultdict
3 import mimetypes
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
10 TYPE_THRESHOLD=.2
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
19 """
20 has_mimetype = lambda e: e.mimetypes
21 episodes = filter(has_mimetype, episodes)
22 types = defaultdict()
23 for e in episodes:
24 for mimetype in e.mimetypes:
25 t = get_type(mimetype)
26 if not t:
27 continue
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)
34 return [x[0] for x in
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
44 >>> get_type('audio/mpeg3')
45 'audio'
47 >>> get_type('video/mpeg')
48 'video'
50 >>> get_type('image/jpeg')
51 'image'
53 >>> get_type('application/ogg')
54 'audio'
56 >>> get_type('application/x-youtube')
57 'video'
59 >>> get_type('application/x-vimeo')
60 'video'
62 >>> get_type('application/octet-stream') == None
63 True
65 >>> get_type('') == None
66 True
68 >>> get_type('music') == None
69 True
70 """
71 if not mimetype:
72 return None
74 if '/' not in mimetype:
75 return None
77 category, type = mimetype.split('/', 1)
78 if category in ('audio', 'video', 'image'):
79 return category
80 elif type == 'ogg':
81 return 'audio'
82 elif type == 'x-youtube':
83 return 'video'
84 elif type == 'x-vimeo':
85 return 'video'
88 def get_mimetype(mimetype, url):
89 """Returns the mimetype; if None is given it tries to guess it"""
91 if not mimetype:
92 mimetype, _encoding = mimetypes.guess_type(url)
94 return mimetype