~cben/subdownloader/upload

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#    Copyright (C) 2007 Ivan Garcia capiscua@gmail.com
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    see <http://www.gnu.org/licenses/>.


import os
import languages.Languages as languages
import platform
if platform.python_version_tuple()[:2] == ['2','5']:
    #this is deprecated since python 2.6
    from md5 import md5
else:
    from hashlib import md5

SUBTITLES_EXT = ["srt","sub","txt","ssa", "smi", "ass", "mpl"]
SELECT_SUBTITLES = "Subtitle Files (*.%s)"% " *.".join(SUBTITLES_EXT)

class SubtitleFile(object):
    """Contains the class that represents a SubtitleFile (SRT,SUB,etc)
    and provides easy methods to retrieve its attributes (Sizebytes, HASH, Validation,etc)
    """
    def __init__(self, online, id=None):
        self._language = None
        self._video = None
        self._online = online
        self._path = ""
        self._onlineId = ""
        self._id_file_online = ""
        self._extraInfo = {}
        if online:
            self._onlineId = id
        else:
            self._path = id
            self.setFileName(os.path.basename(self._path))
        self._uploader = None
        self._languageXX = None
        self._languageXXX = None
        self._languageName = None
        self.rating = 0
        if not online:
            self._size = os.path.getsize(self._path)
            self._hash = md5(file(self._path,mode='rb').read()).hexdigest()

#    def __repr__(self):
#        return "<SubtitleFile online: %s, local: %s, path: %s, file: %s, size: %s, uploader: %s, onlineId: %s, hash: %s, language: %s, rating: %f>"% (self.isOnline(), self.isLocal(), self.getFilePath(), self.getFileName(), self.getSize(), self.getUploader(), self.getIdOnline(), self.getHash(), self.getLanguageXXX(), self.getRating())

    def setFileName(self,filename):
        self._filename = filename

    def getFileName(self):
        return self._filename

    def setVideo(self, _video):
        self._video = _video

    def getVideo(self):
        return self._video

    def setUploader(self,uploader):
        self._uploader = uploader

    def getUploader(self):
        return self._uploader

    def setIdOnline(self,_id_online):
        self._onlineId = _id_online

    def getIdOnline(self):
        return self._onlineId

    def setIdFileOnline(self,_id_file_online):
        self._onlineFileId = _id_file_online

    def getIdFileOnline(self):
        return self._onlineFileId

    def getFilePath(self):
        return self._path

    #It could be the case when isLocal and isOnline are both true
    def isOnline(self):
        return self._onlineId != None and len(self._onlineId)

    def isLocal(self):
        return self._path != None and  len(self._path)

    def getSize(self):
        return self._size

    def setHash(self, hash):
        self._hash = hash

    def getHash(self):
        return self._hash

    def setLanguage(self,language):
        self.setLanguageXXX(language)

    def getLanguage(self):
        return self.getLanguageXXX()

    def setLanguageXX(self,xx):
        if xx == 'gr': #greek officially ISO639-1 is 'el'  , but opensubtitles is buggy
            xx = 'el'
        self._languageXX = xx
        self._languageXXX = languages.xx2xxx(xx)
        self._languageName= languages.xx2name(xx)

    def getLanguageXX(self):
        if self._languageXX:
            return self._languageXX.lower()
        else:
            return None

    def setLanguageXXX(self,xxx):
        self._languageXXX = xxx
        self._languageXX = languages.xxx2xx(xxx)
        self._languageName= languages.xxx2name(xxx)

    def getLanguageXXX(self):
        if self._languageXXX:
            return self._languageXXX.lower()
        else:
            return None

    def getLanguageName(self):
        if self._languageName:
            return self._languageName
        else:
            return None

    def setLanguageName(self,language):
        self._languageName = language
        self._languageXXX = languages.name2xxx(language)
        self._languageXX = languages.name2xx(language)

    def setRating(self, rating):
        self.rating = rating

    def getRating(self):
        return self.rating

    def setExtraInfo(self, info, data):
        self._extraInfo[info] = data

    def getExtraInfo(self, info):
        return self._extraInfo[info]