summaryrefslogtreecommitdiff
path: root/plugin.onedrive/addon.py
blob: c3ab9484cd75ce167c8d0978ba40022ba605f5f4 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#-------------------------------------------------------------------------------
# Copyright (C) 2017 Carlos Guzman (cguZZman) carlosguzmang@protonmail.com
# 
# This file is part of OneDrive for Kodi
# 
# OneDrive for Kodi 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.
# 
# Cloud Drive Common Module for Kodi 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, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------

import datetime
import urllib

from clouddrive.common.cache.simplecache import SimpleCache
from clouddrive.common.ui.addon import CloudDriveAddon
from clouddrive.common.utils import Utils
from resources.lib.provider.onedrive import OneDrive
from clouddrive.common.ui.utils import KodiUtils
from resources.lib.migration import MigrateAccounts


class OneDriveAddon(CloudDriveAddon):
    _provider = OneDrive()
    _extra_parameters = {'expand': 'thumbnails'}
    _cache = None
    
    def __init__(self):
        self._cache = SimpleCache()
        super(OneDriveAddon, self).__init__()
        
    def get_provider(self):
        return self._provider
    
    def get_custom_drive_folders(self, driveid):
        self._account_manager.load()
        drive = self._account_manager.get_drive_by_driveid(driveid)
        drive_folders = []
        if drive['type'] == 'personal':
            if self._content_type == 'image':
                path = 'special/photos'
                params = {'action': '_slideshow', 'content_type': self._content_type, 'driveid': driveid, 'path': path}
                context_options = [(self._common_addon.getLocalizedString(32032), 'RunPlugin('+self._addon_url + '?' + urllib.urlencode(params)+')')]
                drive_folders.append({'name' : self._addon.getLocalizedString(32007), 'path' : path, 'context_options': context_options})
                
                path = 'special/cameraroll'
                params['path'] = path
                context_options = [(self._common_addon.getLocalizedString(32032), 'RunPlugin('+self._addon_url + '?' + urllib.urlencode(params)+')')]
                drive_folders.append({'name' : self._addon.getLocalizedString(32008), 'path' : path, 'context_options': context_options})
            elif self._content_type == 'audio':
                drive_folders.append({'name' : self._addon.getLocalizedString(32009), 'path' : 'special/music'})
        drive_folders.append({'name' : self._common_addon.getLocalizedString(32053), 'path' : 'recent'})
        if drive['type'] != 'documentLibrary':
            drive_folders.append({'name' : self._common_addon.getLocalizedString(32058), 'path' : 'sharedWithMe'})
        return drive_folders

    def get_folder_items(self, driveid, item_driveid=None, item_id=None, path=None, on_items_page_completed=None):
        self._provider.configure(self._account_manager, driveid)
        item_driveid = Utils.default(item_driveid, driveid)
        if item_id:
            files = self._provider.get('/drives/'+item_driveid+'/items/' + item_id + '/children', parameters = self._extra_parameters)
        elif path == 'sharedWithMe' or path == 'recent':
            files = self._provider.get('/drives/'+driveid+'/' + path)
        else:
            if path == '/':
                path = 'root'
            else:
                parts = path.split('/')
                if len(parts) > 1 and not parts[0]:
                    path = 'root:'+path+':'
            files = self._provider.get('/drives/'+driveid+'/' + path + '/children', parameters = self._extra_parameters)
        if self.cancel_operation():
            return
        return self.process_files(driveid, files, on_items_page_completed)
    
    def search(self, query, driveid, item_driveid=None, item_id=None, on_items_page_completed=None):
        self._provider.configure(self._account_manager, driveid)
        item_driveid = Utils.default(item_driveid, driveid)
        url = '/drives/'
        if item_id:
            url += item_driveid+'/items/' + item_id
        else:
            url += driveid
        url += '/search(q=\''+urllib.quote(query)+'\')'
        self._extra_parameters['filter'] = 'file ne null'
        files = self._provider.get(url, parameters = self._extra_parameters)
        if self.cancel_operation():
            return
        return self.process_files(driveid, files, on_items_page_completed)
    
    def process_files(self, driveid, files, on_items_page_completed=None):
        items = []
        for f in files['value']:
            f = Utils.get_safe_value(f, 'remoteItem', f)
            item = self._extract_item(f)
            cache_key = self._addonid+'-drive-'+driveid+'-item_driveid-'+item['drive_id']+'-item_id-'+item['id']+'-path-None'
            self._cache.set(cache_key, f, expiration=datetime.timedelta(minutes=1))
            items.append(item)
        if on_items_page_completed:
            on_items_page_completed(items)
        if '@odata.nextLink' in files:
            next_files = self._provider.get(files['@odata.nextLink'])
            if self.cancel_operation():
                return
            items.extend(self.process_files(driveid, next_files, on_items_page_completed))
        return items
    
    def _extract_item(self, f, include_download_info=False):
        item = {
            'id': f['id'],
            'name': f['name'],
            'name_extension' : Utils.get_extension(f['name']),
            'drive_id' : Utils.get_safe_value(Utils.get_safe_value(f, 'parentReference', {}), 'driveId'),
            'mimetype' : Utils.get_safe_value(Utils.get_safe_value(f, 'file', {}), 'mimeType'),
            'last_modified_date' : Utils.get_safe_value(f,'lastModifiedDateTime'),
            'size': Utils.get_safe_value(f, 'size', 0),
            'description': Utils.get_safe_value(f, 'description', '')
        }
        if 'folder' in f:
            item['folder'] = {
                'child_count' : Utils.get_safe_value(f['folder'],'childCount',0)
            }
        if 'video' in f:
            video = f['video']
            item['video'] = {
                'width' : Utils.get_safe_value(video,'width', 0),
                'height' : Utils.get_safe_value(video, 'height', 0),
                'duration' : Utils.get_safe_value(video, 'duration', 0) /1000
            }
        if 'audio' in f:
            audio = f['audio']
            item['audio'] = {
                'tracknumber' : Utils.get_safe_value(audio, 'track'),
                'discnumber' : Utils.get_safe_value(audio, 'disc'),
                'duration' : int(Utils.get_safe_value(audio, 'duration') or '0') / 1000,
                'year' : Utils.get_safe_value(audio, 'year'),
                'genre' : Utils.get_safe_value(audio, 'genre'),
                'album': Utils.get_safe_value(audio, 'album'),
                'artist': Utils.get_safe_value(audio, 'artist'),
                'title': Utils.get_safe_value(audio, 'title')
            }
        if 'image' in f or 'photo' in f:
            item['image'] = {
                'size' : Utils.get_safe_value(f, 'size', 0)
            }
        if 'thumbnails' in f and type(f['thumbnails']) == list and len(f['thumbnails']) > 0:
            thumbnails = f['thumbnails'][0]
            item['thumbnail'] = Utils.get_safe_value(Utils.get_safe_value(thumbnails, 'large', {}), 'url', '')
        if include_download_info:
            item['download_info'] =  {
                'url' : Utils.get_safe_value(f,'@microsoft.graph.downloadUrl')
            }
        return item
    
    def get_item(self, driveid, item_driveid=None, item_id=None, path=None, find_subtitles=False, include_download_info=False):
        self._provider.configure(self._account_manager, driveid)
        item_driveid = Utils.default(item_driveid, driveid)
        cache_key = self._addonid+'-drive-'+driveid+'-item_driveid-'+Utils.str(item_driveid)+'-item_id-'+Utils.str(item_id)+'-path-'+Utils.str(path)
        f = self._cache.get(cache_key)
        if not f :
            if item_id:
                f = self._provider.get('/drives/'+item_driveid+'/items/' + item_id, parameters = self._extra_parameters)
            elif path == 'sharedWithMe' or path == 'recent':
                return
            else:
                if path == '/':
                    path = 'root'
                else:
                    parts = path.split('/')
                    if len(parts) > 1 and not parts[0]:
                        path = 'root:'+path+':'
                f = self._provider.get('/drives/'+driveid+'/' + path, parameters = self._extra_parameters)
            self._cache.set(cache_key, f, expiration=datetime.timedelta(seconds=59))
        item = self._extract_item(f, include_download_info)
        if find_subtitles:
            subtitles = []
            parent_id = Utils.get_safe_value(Utils.get_safe_value(f, 'parentReference', {}), 'id')
            search_url = '/drives/'+item_driveid+'/items/' + parent_id + '/search(q=\'{'+urllib.quote(Utils.remove_extension(item['name']))+'}\')'
            files = self._provider.get(search_url)
            for f in files['value']:
                subtitle = self._extract_item(f, include_download_info)
                if subtitle['name_extension'] == 'srt' or subtitle['name_extension'] == 'sub' or subtitle['name_extension'] == 'sbv':
                    subtitles.append(subtitle)
            if subtitles:
                item['subtitles'] = subtitles
        return item
    
    def _rename_action(self):
        self._action = Utils.get_safe_value({
            'open_folder': '_list_folder',
            'open_drive': '_list_drive',
        }, self._action, self._action)

if __name__ == '__main__':
    onedrive_addon = OneDriveAddon()
    if not KodiUtils.get_addon_setting('migrated'):
        try:
            MigrateAccounts()
        except Exception as e:
            onedrive_addon._handle_exception(e, False)
    onedrive_addon.route()