-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileFetcher.py
111 lines (100 loc) · 4.34 KB
/
fileFetcher.py
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
# -*- coding: utf-8 -*-
#
# FTV Guide
# Copyright (C) 2015 Thomas Geppert [bluezed]
#
# 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 2, 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; see the file LICENSE.txt. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
#
import xbmc
import xbmcvfs
import os
import urllib2
import datetime
import zlib
MAIN_URL = 'http://thaisatellite.tv/ftv/'
class FileFetcher(object):
INTERVAL_ALWAYS = 0
INTERVAL_12 = 1
INTERVAL_24 = 2
INTERVAL_48 = 3
FETCH_ERROR = -1
FETCH_NOT_NEEDED = 0
FETCH_OK = 1
TYPE_DEFAULT = 1
TYPE_REMOTE = 2
basePath = xbmc.translatePath(os.path.join('special://profile', 'addon_data', 'script.ftvguide'))
filePath = ''
fileUrl = ''
addon = None
fileType = TYPE_DEFAULT
def __init__(self, fileName, addon):
self.addon = addon
if fileName.startswith("http://") or fileName.startswith("sftp://") or fileName.startswith("ftp://") or \
fileName.startswith("https://") or fileName.startswith("ftps://") or fileName.startswith("smb://") or \
fileName.startswith("nfs://"):
self.fileType = self.TYPE_REMOTE
self.fileUrl = fileName
self.filePath = os.path.join(self.basePath, fileName.split('/')[-1])
else:
self.fileType = self.TYPE_DEFAULT
self.fileUrl = MAIN_URL + fileName
self.filePath = os.path.join(self.basePath, fileName)
# make sure the folder is actually there already!
if not os.path.exists(self.basePath):
os.makedirs(self.basePath)
def fetchFile(self):
retVal = self.FETCH_NOT_NEEDED
fetch = False
if not os.path.exists(self.filePath): # always fetch if file doesn't exist!
fetch = True
else:
interval = int(self.addon.getSetting('xmltv.interval'))
if interval != self.INTERVAL_ALWAYS:
modTime = datetime.datetime.fromtimestamp(os.path.getmtime(self.filePath))
td = datetime.datetime.now() - modTime
# need to do it this way cause Android doesn't support .total_seconds() :(
diff = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / 10 ** 6
if ((interval == self.INTERVAL_12 and diff >= 43200) or
(interval == self.INTERVAL_24 and diff >= 86400) or
(interval == self.INTERVAL_48 and diff >= 172800)):
fetch = True
else:
fetch = True
if fetch:
tmpFile = os.path.join(self.basePath, 'tmp')
if self.fileType == self.TYPE_REMOTE:
xbmc.log('[script.ftvguide] file is in remote location: %s' % self.fileUrl, xbmc.LOGDEBUG)
if not xbmcvfs.copy(self.fileUrl, tmpFile):
xbmc.log('[script.ftvguide] Remote file couldn\'t be copied: %s' % self.fileUrl, xbmc.LOGERROR)
else:
f = open(tmpFile, 'wb')
xbmc.log('[script.ftvguide] file is on the internet: %s' % self.fileUrl, xbmc.LOGDEBUG)
tmpData = urllib2.urlopen(self.fileUrl)
data = tmpData.read()
if tmpData.info().get('content-encoding') == 'gzip':
data = zlib.decompress(data, zlib.MAX_WBITS + 16)
f.write(data)
f.close()
if os.path.getsize(tmpFile) > 256:
if os.path.exists(self.filePath):
os.remove(self.filePath)
os.rename(tmpFile, self.filePath)
retVal = self.FETCH_OK
xbmc.log('[script.ftvguide] file %s was downloaded' % self.filePath, xbmc.LOGDEBUG)
else:
retVal = self.FETCH_ERROR
return retVal