def __export_folder(self, driveid, folder, export_folder, export, items_info, root_id): folder_id = Utils.str(folder['id']) folder_name = Utils.unicode(folder['name']) folder_path = os.path.join(os.path.join(export_folder, folder_name), '') if not xbmcvfs.exists(folder_path): try: xbmcvfs.mkdirs(folder_path) except: if self._system_monitor.waitForAbort(3): return xbmcvfs.mkdirs(folder_path) items = self.get_provider().get_folder_items(Utils.default(Utils.get_safe_value(folder, 'drive_id'), driveid), folder['id']) if self.cancel_operation(): return for item in items: if 'folder' in item: if self._child_count_supported: self._exporting_target += int(item['folder']['child_count']) else: self._exporting_target += 1 for item in items: is_folder = 'folder' in item item_id = Utils.str(item['id']) item_name = Utils.unicode(item['name']) item_name_extension = item['name_extension'] file_path = os.path.join(folder_path, item_name) if is_folder: ExportManager.add_item_info(items_info, item_id, item_name, os.path.join(file_path, ''), folder_id,'folder') self.__export_folder(driveid, item, folder_path, export, items_info, root_id) elif (('video' in item or item_name_extension in self._video_file_extensions) and export['content_type'] == 'video') or ('audio' in item and export['content_type'] == 'audio'): item_name += ExportManager._strm_extension file_path += ExportManager._strm_extension if self._addon.getSetting('skip_unmodified') and KodiUtils.file_exists(file_path) and KodiUtils.file(file_path).size() == item["size"]: continue ExportManager.create_strm(driveid, item, file_path, export['content_type'], self._addon_url) ExportManager.add_item_info(items_info, item_id, item_name, file_path, folder_id,'file') elif 'nfo_export' in export and export['nfo_export'] and ('nfo' in item_name_extension or 'text/x-nfo' in item.get("mimetype")): nfo_path = os.path.join(folder_path, Utils.unicode(item_name)) if self._addon.getSetting('skip_unmodified') and KodiUtils.file_exists(nfo_path) and KodiUtils.file(nfo_path).size() == item["size"]: continue ExportManager.create_nfo(item_id, export["item_driveid"], nfo_path, self.get_provider()) ExportManager.add_item_info(items_info, item_id, item_name, nfo_path, folder_id,'file') self._export_manager.save_items_info(root_id, items_info) self._exporting_count += 1 p = int(self._exporting_count/float(self._exporting_target)*100) if self._exporting_percent < p: self._exporting_percent = p self._export_progress_dialog_bg.update(self._exporting_percent, self._addon_name + ' ' + self._common_addon.getLocalizedString(32024), file_path[len(export['destination_folder']):])
def process_change(self, change, items_info, export): change_type = None changed_item_id = change['id'] Logger.debug('Change: %s' % Utils.str(change)) if changed_item_id != export['id']: changed_item_name = Utils.get_safe_value(change, 'name', '') deleted = Utils.get_safe_value(change, 'removed') parent_id = Utils.get_safe_value(change, 'parent', '') if changed_item_id in items_info: item_info = items_info[changed_item_id] item_type = item_info['type'] is_folder = item_type == 'folder' Logger.debug('item_info: %s' % Utils.str(item_info)) item_info_path = item_info['full_local_path'] if KodiUtils.file_exists(item_info_path): if deleted: change_type = self.process_change_delete( items_info, changed_item_id, is_folder) elif parent_id != item_info[ 'parent'] or changed_item_name != item_info['name']: if parent_id in items_info: change_type = 'move' Logger.debug('Change is move') parent_item_info = items_info[parent_id] parent_item_path = parent_item_info[ 'full_local_path'] new_path = os.path.join( parent_item_path, Utils.unicode(changed_item_name)) if is_folder: new_path = os.path.join(new_path, '') if KodiUtils.file_rename(item_info_path, new_path): ExportManager.remove_item_info( items_info, changed_item_id) ExportManager.add_item_info( items_info, changed_item_id, Utils.unicode(changed_item_name), new_path, parent_id, item_type) else: change_type = 'retry' else: Logger.debug( 'Change is move but parent not in item list. Change is delete' ) change_type = self.process_change_delete( items_info, changed_item_id, is_folder) else: Logger.debug( 'Invalid state. Changed item not found: %s. Deleting from item list.' % item_info_path) change_type = self.process_change_delete( items_info, changed_item_id, is_folder) elif parent_id in items_info and not deleted: is_folder = 'application/vnd.google-apps.folder' in change.get( 'mimetype') content_type = export['content_type'] item_name_extension = change['name_extension'] is_stream_file = ( ('video' in change or item_name_extension in self._video_file_extensions) and content_type == 'video') or ('audio' in change and content_type == 'audio') item_type = 'folder' if is_folder else 'file' if is_folder or is_stream_file or ( export['nfo_export'] and ('nfo' in item_name_extension or 'text/x-nfo' in change.get("mimetype"))): change_type = 'add' Logger.debug('Change is new item') parent_item_info = items_info[parent_id] parent_item_path = parent_item_info['full_local_path'] new_path = os.path.join(parent_item_path, Utils.unicode(changed_item_name)) if is_folder: new_path = os.path.join(new_path, '') if not KodiUtils.mkdirs(new_path): change_type = 'retry' elif is_stream_file: new_path += '.strm' ExportManager.create_strm( export['driveid'], change, new_path, content_type, 'plugin://%s/' % self.addonid) else: ExportManager.create_nfo( export['driveid'], change, Utils.unicode(changed_item_name), new_path) if change_type != 'retry': ExportManager.add_item_info( items_info, changed_item_id, Utils.unicode(changed_item_name), new_path, parent_id, item_type) Logger.debug('change type: %s ' % Utils.str(change_type)) return change_type