fix video
[librarian.git] / librarian / __init__.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import re
7 import urllib
8 from .utils import XMLNamespace
9
10
11 VIDEO_PROVIDERS = {
12     'youtube': {
13         'url': 'https://www.youtube.com/watch?v=%s',
14         'embed': '//www.youtube.com/embed/%s?controls=2&rel=0&showinfo=0&theme=light',
15     },
16     'vimeo': {
17         'url': 'https://vimeo.com/%s',
18         'embed': '//player.vimeo.com/video/%s',
19     },
20 }
21
22
23 def get_provider(provider_id):
24     return VIDEO_PROVIDERS.get(provider_id, VIDEO_PROVIDERS['youtube'])
25
26
27 class UnicodeException(Exception):
28     def __str__(self):
29         """ Dirty workaround for Python Unicode handling problems. """
30         return unicode(self).encode('utf-8')
31
32     def __unicode__(self):
33         """ Dirty workaround for Python Unicode handling problems. """
34         args = self.args[0] if len(self.args) == 1 else self.args
35         try:
36             message = unicode(args)
37         except UnicodeDecodeError:
38             message = unicode(args, encoding='utf-8', errors='ignore')
39         return message
40
41
42 class ParseError(UnicodeException):
43     pass
44
45
46 class ValidationError(UnicodeException):
47     pass
48
49
50 # was deleted, but still used???
51 class NoDublinCore(ValidationError):
52     pass
53
54
55 class BuildError(Exception):
56     pass
57
58
59 class EmptyNamespace(XMLNamespace):
60     def __init__(self):
61         super(EmptyNamespace, self).__init__('')
62
63     def __call__(self, tag):
64         return tag
65
66 # some common namespaces we use
67 RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
68 DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
69 XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
70 XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
71 NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
72 OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
73
74 SSTNS = XMLNamespace('http://nowoczesnapolska.org.pl/sst#')
75
76
77 class WLURI(object):
78     """Represents a WL URI. Extracts slug from it."""
79     slug = None
80
81     example = 'http://wolnelektury.pl/katalog/lektura/template/'
82     _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/(?P<slug>[-a-z0-9]+)/?$')
83
84     def __init__(self, uri):
85         uri = unicode(uri)
86         self.uri = uri
87         self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
88
89     @classmethod
90     def strict(cls, uri):
91         match = cls._re_wl_uri.match(uri)
92         if not match:
93             raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
94                         uri, cls._re_wl_uri.pattern))
95         return cls(uri)
96
97     @classmethod
98     def from_slug(cls, slug):
99         """Contructs an URI from slug.
100
101         >>> WLURI.from_slug('a-slug').uri
102         u'http://wolnelektury.pl/katalog/lektura/a-slug/'
103
104         """
105         uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
106         return cls(uri)
107
108     def __unicode__(self):
109         return self.uri
110
111     def __str__(self):
112         return self.uri
113
114     def __eq__(self, other):
115         return self.slug == other.slug
116
117
118 class URLOpener(urllib.FancyURLopener):
119     version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
120 urllib._urlopener = URLOpener()