~osclone-developers/flubtitles/alpha

« back to all changes in this revision

Viewing changes to api/common.py

  • Committer: swick at 2flub
  • Date: 2011-02-22 15:12:51 UTC
  • Revision ID: swick@2flub.org-20110222151251-nsjsh0yrn1nolqa2
Moving to REST

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from lib import timeprofiler
8
8
from lib import logger
9
9
from lib import systemload
10
 
from data import languages
11
 
from data import statuscodes
12
10
from auth import Authentication
13
11
from lib.validate import Validation
14
 
from guess_language import guess_language
15
12
 
16
13
from couchdbkit import *
17
14
server = Server()
29
26
        # allowed useragents
30
27
        self.useragents = ["flubdownloader"]
31
28
 
32
 
    def GetStatusCodes(self, token):
33
 
        """
34
 
           Returns all existing status codes and their corresponding status texts
35
 
        """
36
 
 
37
 
        self.profiler.mark()
38
 
        reply = {}
39
 
 
40
 
        if self.auth.session_exists(token):
41
 
            reply['statusCode'] = 200
42
 
            reply['statusText'] = statuscodes.statuscodes['200']
43
 
            reply['data']       = statuscodes.statuscodes
44
 
            reply['time']       = self.profiler.elapsed()
45
 
            reply['serverLoad'] = systemload.getLoad()
46
 
 
47
 
        else:
48
 
            reply['statusCode']     = "406"
49
 
            reply['statusText']     = statuscodes.statuscodes['406']
50
 
            reply['time']           = self.profiler.elapsed()
51
 
 
52
 
        return reply
53
 
 
54
 
 
55
 
    def DetectLanguage(self, token, *text ):
56
 
        self.profiler.mark()
57
 
        reply = {}
58
 
        
59
 
        if self.auth.session_exists(token):
60
 
            languages = {}
61
 
            for base64_data in text:
62
 
                compressed_data = base64.b64decode(base64_data)
63
 
                data = zlib.decompress(compressed_data)
64
 
                md5 = hashlib.md5()
65
 
                md5.update(data)
66
 
                md5_data = md5.hexdigest()
67
 
                languages[md5_data] = guess_language.guessLanguageInfo(data)[0]
68
 
 
69
 
            reply['statusCode']     = "200"
70
 
            reply['statusText']     = statuscodes.statuscodes['200']
71
 
            reply['time']           = self.profiler.elapsed()
72
 
            reply['languages']      = languages
73
 
 
74
 
        else:
75
 
            reply['statusCode']     = "406"
76
 
            reply['statusText']     = statuscodes.statuscodes['406']
77
 
            reply['time']           = self.profiler.elapsed()
78
 
 
79
 
        return reply
80
 
 
81
 
    def GetSubtitleLanguages(self, token):
82
 
        self.profiler.mark()
83
 
        reply = {}
84
 
        if self.auth.session_exists(token):
85
 
            reply['statusCode']     = "200"
86
 
            reply['statusText']     = statuscodes.statuscodes['200']
87
 
            reply['time']           = self.profiler.elapsed()
88
 
            reply['languages']      = languages.iso_codes
89
 
        else:
90
 
            reply['statusCode']     = "406"
91
 
            reply['statusText']     = statuscodes.statuscodes['406']
92
 
            reply['time']           = self.profiler.elapsed()
93
 
        return reply
94
 
 
95
29
    def DownloadSubtitles(self, token, *subhash):
96
30
        self.profiler.mark()
97
31
        reply = {}