add vimeo
[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 class UnicodeException(Exception):
24     def __str__(self):
25         """ Dirty workaround for Python Unicode handling problems. """
26         return unicode(self).encode('utf-8')
27
28     def __unicode__(self):
29         """ Dirty workaround for Python Unicode handling problems. """
30         args = self.args[0] if len(self.args) == 1 else self.args
31         try:
32             message = unicode(args)
33         except UnicodeDecodeError:
34             message = unicode(args, encoding='utf-8', errors='ignore')
35         return message
36
37
38 class ParseError(UnicodeException):
39     pass
40
41
42 class ValidationError(UnicodeException):
43     pass
44
45
46 # was deleted, but still used???
47 class NoDublinCore(ValidationError):
48     pass
49
50
51 class BuildError(Exception):
52     pass
53
54
55 class EmptyNamespace(XMLNamespace):
56     def __init__(self):
57         super(EmptyNamespace, self).__init__('')
58
59     def __call__(self, tag):
60         return tag
61
62 # some common namespaces we use
63 RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
64 DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
65 XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
66 XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
67 NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
68 OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
69
70 SSTNS = XMLNamespace('http://nowoczesnapolska.org.pl/sst#')
71
72
73 class WLURI(object):
74     """Represents a WL URI. Extracts slug from it."""
75     slug = None
76
77     example = 'http://wolnelektury.pl/katalog/lektura/template/'
78     _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/(?P<slug>[-a-z0-9]+)/?$')
79
80     def __init__(self, uri):
81         uri = unicode(uri)
82         self.uri = uri
83         self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
84
85     @classmethod
86     def strict(cls, uri):
87         match = cls._re_wl_uri.match(uri)
88         if not match:
89             raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
90                         uri, cls._re_wl_uri.pattern))
91         return cls(uri)
92
93     @classmethod
94     def from_slug(cls, slug):
95         """Contructs an URI from slug.
96
97         >>> WLURI.from_slug('a-slug').uri
98         u'http://wolnelektury.pl/katalog/lektura/a-slug/'
99
100         """
101         uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
102         return cls(uri)
103
104     def __unicode__(self):
105         return self.uri
106
107     def __str__(self):
108         return self.uri
109
110     def __eq__(self, other):
111         return self.slug == other.slug
112
113
114 class URLOpener(urllib.FancyURLopener):
115     version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
116 urllib._urlopener = URLOpener()