hydrus/hydrus/client/importing/ClientImportGallery.py

1612 lines
55 KiB
Python
Raw Normal View History

2020-05-20 21:36:02 +00:00
import threading
import time
2020-07-29 20:52:44 +00:00
from hydrus.core import HydrusConstants as HC
from hydrus.core import HydrusData
from hydrus.core import HydrusExceptions
from hydrus.core import HydrusGlobals as HG
from hydrus.core import HydrusSerialisable
2020-04-22 21:00:35 +00:00
from hydrus.client import ClientConstants as CC
from hydrus.client import ClientDownloading
from hydrus.client.importing import ClientImportFileSeeds
from hydrus.client.importing import ClientImportGallerySeeds
from hydrus.client.importing import ClientImporting
2021-06-30 21:27:35 +00:00
from hydrus.client.importing.options import FileImportOptions
from hydrus.client.importing.options import TagImportOptions
2020-04-22 21:00:35 +00:00
from hydrus.client.networking import ClientNetworkingJobs
2018-07-18 21:07:15 +00:00
class GalleryImport( HydrusSerialisable.SerialisableBase ):
SERIALISABLE_TYPE = HydrusSerialisable.SERIALISABLE_TYPE_GALLERY_IMPORT
SERIALISABLE_NAME = 'Gallery Import'
2018-09-05 20:52:32 +00:00
SERIALISABLE_VERSION = 2
2018-07-18 21:07:15 +00:00
2018-11-28 22:31:04 +00:00
def __init__( self, query = None, source_name = None, initial_search_urls = None, start_file_queue_paused = False, start_gallery_queue_paused = False ):
2018-08-01 20:44:57 +00:00
if query is None:
query = 'samus_aran'
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
if source_name is None:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
source_name = 'unknown'
if initial_search_urls is None:
initial_search_urls = []
2018-07-18 21:07:15 +00:00
initial_search_urls = HydrusData.DedupeList( initial_search_urls )
2018-07-18 21:07:15 +00:00
HydrusSerialisable.SerialisableBase.__init__( self )
self._creation_time = HydrusData.GetNow()
2018-08-01 20:44:57 +00:00
self._gallery_import_key = HydrusData.GenerateKey()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._query = query
2018-09-05 20:52:32 +00:00
self._source_name = source_name
2018-07-18 21:07:15 +00:00
self._page_key = 'initialising page key'
self._publish_to_page = False
self._current_page_index = 0
2018-08-01 20:44:57 +00:00
self._num_new_urls_found = 0
2018-07-18 21:07:15 +00:00
self._num_urls_found = 0
self._file_limit = HC.options[ 'gallery_file_limit' ]
2018-11-28 22:31:04 +00:00
self._files_paused = start_file_queue_paused
self._gallery_paused = start_gallery_queue_paused
2018-07-18 21:07:15 +00:00
self._file_import_options = HG.client_controller.new_options.GetDefaultFileImportOptions( 'loud' )
2021-06-30 21:27:35 +00:00
self._tag_import_options = TagImportOptions.TagImportOptions( is_default = True )
2018-07-18 21:07:15 +00:00
self._gallery_seed_log = ClientImportGallerySeeds.GallerySeedLog()
2018-09-05 20:52:32 +00:00
gallery_seeds = [ ClientImportGallerySeeds.GallerySeed( url ) for url in initial_search_urls ]
self._gallery_seed_log.AddGallerySeeds( gallery_seeds )
2018-07-18 21:07:15 +00:00
self._file_seed_cache = ClientImportFileSeeds.FileSeedCache()
self._no_work_until = 0
self._no_work_until_reason = ''
self._lock = threading.Lock()
2019-03-06 23:06:22 +00:00
self._file_status = ''
2018-07-18 21:07:15 +00:00
self._gallery_status = ''
self._gallery_status_can_change_timestamp = 0
2019-03-06 23:06:22 +00:00
self._all_work_finished = False
2018-07-18 21:07:15 +00:00
2021-07-07 20:48:57 +00:00
self._have_started = False
2018-08-01 20:44:57 +00:00
self._file_network_job = None
self._gallery_network_job = None
2018-07-18 21:07:15 +00:00
self._files_repeating_job = None
self._gallery_repeating_job = None
HG.client_controller.sub( self, 'NotifyFileSeedsUpdated', 'file_seed_cache_file_seeds_updated' )
2018-09-12 21:36:26 +00:00
HG.client_controller.sub( self, 'NotifyGallerySeedsUpdated', 'gallery_seed_log_gallery_seeds_updated' )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def _AmOverFileLimit( self ):
if self._file_limit is not None and self._num_new_urls_found >= self._file_limit:
return True
return False
2018-07-18 21:07:15 +00:00
def _DelayWork( self, time_delta, reason ):
self._no_work_until = HydrusData.GetNow() + time_delta
self._no_work_until_reason = reason
def _GetSerialisableInfo( self ):
2019-01-09 22:59:03 +00:00
serialisable_gallery_import_key = self._gallery_import_key.hex()
2018-08-01 20:44:57 +00:00
2018-07-18 21:07:15 +00:00
serialisable_file_import_options = self._file_import_options.GetSerialisableTuple()
serialisable_tag_import_options = self._tag_import_options.GetSerialisableTuple()
serialisable_gallery_seed_log = self._gallery_seed_log.GetSerialisableTuple()
serialisable_file_seed_cache = self._file_seed_cache.GetSerialisableTuple()
2018-09-05 20:52:32 +00:00
return ( serialisable_gallery_import_key, self._creation_time, self._query, self._source_name, self._current_page_index, self._num_urls_found, self._num_new_urls_found, self._file_limit, self._gallery_paused, self._files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_seed_log, serialisable_file_seed_cache, self._no_work_until, self._no_work_until_reason )
2018-07-18 21:07:15 +00:00
def _InitialiseFromSerialisableInfo( self, serialisable_info ):
2018-09-05 20:52:32 +00:00
( serialisable_gallery_import_key, self._creation_time, self._query, self._source_name, self._current_page_index, self._num_urls_found, self._num_new_urls_found, self._file_limit, self._gallery_paused, self._files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_seed_log, serialisable_file_seed_cache, self._no_work_until, self._no_work_until_reason ) = serialisable_info
2018-08-01 20:44:57 +00:00
2019-01-09 22:59:03 +00:00
self._gallery_import_key = bytes.fromhex( serialisable_gallery_import_key )
2018-07-18 21:07:15 +00:00
self._file_import_options = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_file_import_options )
self._tag_import_options = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_tag_import_options )
self._gallery_seed_log = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_gallery_seed_log )
self._file_seed_cache = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_file_seed_cache )
def _FileNetworkJobPresentationContextFactory( self, network_job ):
def enter_call():
with self._lock:
2018-08-01 20:44:57 +00:00
self._file_network_job = network_job
2018-07-18 21:07:15 +00:00
def exit_call():
with self._lock:
2018-08-01 20:44:57 +00:00
self._file_network_job = None
2018-07-18 21:07:15 +00:00
return ClientImporting.NetworkJobPresentationContext( enter_call, exit_call )
def _GalleryNetworkJobPresentationContextFactory( self, network_job ):
def enter_call():
with self._lock:
2018-08-01 20:44:57 +00:00
self._gallery_network_job = network_job
2018-07-18 21:07:15 +00:00
def exit_call():
with self._lock:
2018-08-01 20:44:57 +00:00
self._gallery_network_job = None
2018-07-18 21:07:15 +00:00
return ClientImporting.NetworkJobPresentationContext( enter_call, exit_call )
2018-08-15 20:40:30 +00:00
def _NetworkJobFactory( self, *args, **kwargs ):
network_job = ClientNetworkingJobs.NetworkJobDownloader( self._gallery_import_key, *args, **kwargs )
return network_job
2018-09-05 20:52:32 +00:00
def _UpdateSerialisableInfo( self, version, old_serialisable_info ):
if version == 1:
( serialisable_gallery_import_key, self._creation_time, self._query, serialisable_gallery_identifier, self._current_page_index, self._num_urls_found, self._num_new_urls_found, self._file_limit, self._gallery_paused, self._files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_seed_log, serialisable_file_seed_cache, self._no_work_until, self._no_work_until_reason ) = old_serialisable_info
gallery_identifier = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_gallery_identifier )
source_name = ClientDownloading.ConvertGalleryIdentifierToGUGName( gallery_identifier )
new_serialisable_info = ( serialisable_gallery_import_key, self._creation_time, self._query, source_name, self._current_page_index, self._num_urls_found, self._num_new_urls_found, self._file_limit, self._gallery_paused, self._files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_seed_log, serialisable_file_seed_cache, self._no_work_until, self._no_work_until_reason )
return ( 2, new_serialisable_info )
2018-08-01 20:44:57 +00:00
def _WorkOnFiles( self ):
2018-07-18 21:07:15 +00:00
file_seed = self._file_seed_cache.GetNextFileSeed( CC.STATUS_UNKNOWN )
if file_seed is None:
return
did_substantial_work = False
try:
2018-09-05 20:52:32 +00:00
def status_hook( text ):
2018-08-01 20:44:57 +00:00
2018-07-18 21:07:15 +00:00
with self._lock:
2019-12-05 05:29:32 +00:00
if len( text ) > 0:
text = text.splitlines()[0]
2019-11-20 23:10:46 +00:00
2019-03-06 23:06:22 +00:00
self._file_status = text
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
did_substantial_work = file_seed.WorkOnURL( self._file_seed_cache, status_hook, self._NetworkJobFactory, self._FileNetworkJobPresentationContextFactory, self._file_import_options, self._tag_import_options )
with self._lock:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
should_present = self._publish_to_page and file_seed.ShouldPresent( self._file_import_options )
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
page_key = self._page_key
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
if should_present:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
file_seed.PresentToPage( page_key )
2018-08-01 20:44:57 +00:00
2018-09-05 20:52:32 +00:00
did_substantial_work = True
2018-07-18 21:07:15 +00:00
except HydrusExceptions.VetoException as e:
status = CC.STATUS_VETOED
2019-01-09 22:59:03 +00:00
note = str( e )
2018-07-18 21:07:15 +00:00
file_seed.SetStatus( status, note = note )
if isinstance( e, HydrusExceptions.CancelledException ):
time.sleep( 2 )
except HydrusExceptions.NotFoundException:
status = CC.STATUS_VETOED
note = '404'
file_seed.SetStatus( status, note = note )
time.sleep( 2 )
except Exception as e:
status = CC.STATUS_ERROR
file_seed.SetStatus( status, exception = e )
time.sleep( 3 )
finally:
self._file_seed_cache.NotifyFileSeedsUpdated( ( file_seed, ) )
2018-08-01 20:44:57 +00:00
with self._lock:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._file_network_job = None
2018-07-18 21:07:15 +00:00
if did_substantial_work:
time.sleep( ClientImporting.DID_SUBSTANTIAL_FILE_WORK_MINIMUM_SLEEP_TIME )
2018-08-01 20:44:57 +00:00
def _WorkOnGallery( self ):
2018-07-18 21:07:15 +00:00
gallery_seed = self._gallery_seed_log.GetNextGallerySeed( CC.STATUS_UNKNOWN )
if gallery_seed is None:
return
with self._lock:
2018-08-01 20:44:57 +00:00
if self._AmOverFileLimit():
self._gallery_paused = True
return
2018-07-18 21:07:15 +00:00
2018-08-22 21:10:59 +00:00
self._gallery_status = 'checking next page'
2018-07-18 21:07:15 +00:00
2018-08-22 21:10:59 +00:00
2018-09-05 20:52:32 +00:00
def file_seeds_callable( file_seeds ):
2018-08-22 21:10:59 +00:00
2018-09-05 20:52:32 +00:00
if self._file_limit is None:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
max_new_urls_allowed = None
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
else:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
max_new_urls_allowed = self._file_limit - self._num_new_urls_found
2018-07-18 21:07:15 +00:00
2018-10-24 21:34:02 +00:00
return ClientImporting.UpdateFileSeedCacheWithFileSeeds( self._file_seed_cache, file_seeds, max_new_urls_allowed = max_new_urls_allowed )
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
def status_hook( text ):
with self._lock:
2018-08-01 20:44:57 +00:00
2019-12-05 05:29:32 +00:00
if len( text ) > 0:
text = text.splitlines()[0]
2019-11-20 23:10:46 +00:00
2018-09-05 20:52:32 +00:00
self._gallery_status = text
2018-08-01 20:44:57 +00:00
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
def title_hook( text ):
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
return
2018-08-01 20:44:57 +00:00
2018-09-05 20:52:32 +00:00
try:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
( num_urls_added, num_urls_already_in_file_seed_cache, num_urls_total, result_404, added_new_gallery_pages, stop_reason ) = gallery_seed.WorkOnURL( 'download page', self._gallery_seed_log, file_seeds_callable, status_hook, title_hook, self._NetworkJobFactory, self._GalleryNetworkJobPresentationContextFactory, self._file_import_options )
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
self._num_new_urls_found += num_urls_added
self._num_urls_found += num_urls_total
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
if num_urls_added > 0:
2018-08-01 20:44:57 +00:00
2018-09-05 20:52:32 +00:00
ClientImporting.WakeRepeatingJob( self._files_repeating_job )
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
self._current_page_index += 1
except HydrusExceptions.NetworkException as e:
with self._lock:
2018-07-18 21:07:15 +00:00
2018-11-28 22:31:04 +00:00
delay = HG.client_controller.new_options.GetInteger( 'downloader_network_error_delay' )
2019-01-09 22:59:03 +00:00
self._DelayWork( delay, str( e ) )
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
return
except Exception as e:
gallery_seed_status = CC.STATUS_ERROR
2019-01-09 22:59:03 +00:00
gallery_seed_note = str( e )
2018-09-05 20:52:32 +00:00
2018-07-18 21:07:15 +00:00
gallery_seed.SetStatus( gallery_seed_status, note = gallery_seed_note )
2018-09-05 20:52:32 +00:00
HydrusData.PrintException( e )
with self._lock:
self._gallery_paused = True
2018-08-01 20:44:57 +00:00
self._gallery_seed_log.NotifyGallerySeedsUpdated( ( gallery_seed, ) )
return True
2018-10-03 21:00:15 +00:00
def CanRetryFailed( self ):
with self._lock:
return self._file_seed_cache.GetFileSeedCount( CC.STATUS_ERROR ) > 0
2020-01-02 03:05:35 +00:00
def CanRetryIgnored( self ):
with self._lock:
return self._file_seed_cache.GetFileSeedCount( CC.STATUS_VETOED ) > 0
2018-08-01 20:44:57 +00:00
def CurrentlyWorking( self ):
with self._lock:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
finished = not self._file_seed_cache.WorkToDo()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return not finished and not self._files_paused
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
2021-02-11 01:59:52 +00:00
def FilesFinished( self ):
with self._lock:
return not self._file_seed_cache.WorkToDo()
2018-08-01 20:44:57 +00:00
def FilesPaused( self ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
return self._files_paused
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def GalleryFinished( self ):
with self._lock:
return not self._gallery_seed_log.WorkToDo()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def GalleryPaused( self ):
with self._lock:
return self._gallery_paused
2018-07-18 21:07:15 +00:00
2019-08-21 21:34:01 +00:00
def GetAPIInfoDict( self, simple ):
with self._lock:
d = {}
d[ 'query_text' ] = self._query
d[ 'source' ] = self._source_name
d[ 'gallery_key' ] = self._gallery_import_key.hex()
d[ 'files_paused' ] = self._files_paused
d[ 'gallery_paused' ] = self._gallery_paused
d[ 'imports' ] = self._file_seed_cache.GetAPIInfoDict( simple )
d[ 'gallery_log' ] = self._gallery_seed_log.GetAPIInfoDict( simple )
return d
2018-08-15 20:40:30 +00:00
def GetCreationTime( self ):
with self._lock:
return self._creation_time
2018-08-01 20:44:57 +00:00
def GetFileImportOptions( self ):
with self._lock:
return self._file_import_options
2018-07-18 21:07:15 +00:00
def GetFileSeedCache( self ):
2018-08-01 20:44:57 +00:00
with self._lock:
return self._file_seed_cache
def GetFileLimit( self ):
with self._lock:
return self._file_limit
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def GetGalleryImportKey( self ):
with self._lock:
return self._gallery_import_key
2018-07-18 21:07:15 +00:00
def GetGallerySeedLog( self ):
2018-08-01 20:44:57 +00:00
with self._lock:
return self._gallery_seed_log
def GetGalleryStatus( self ):
with self._lock:
2018-10-17 21:00:09 +00:00
if HydrusData.TimeHasPassed( self._no_work_until ):
gallery_status = self._gallery_status
else:
2020-11-11 22:20:16 +00:00
no_work_text = '{}: {}'.format( HydrusData.ConvertTimestampToPrettyExpires( self._no_work_until ), self._no_work_until_reason )
2018-10-17 21:00:09 +00:00
gallery_status = no_work_text
return gallery_status
2018-08-01 20:44:57 +00:00
2018-11-21 22:22:36 +00:00
def GetHashes( self ):
with self._lock:
fsc = self._file_seed_cache
2018-11-21 22:22:36 +00:00
return fsc.GetHashes()
2018-11-21 22:22:36 +00:00
2018-08-01 20:44:57 +00:00
def GetNetworkJobs( self ):
with self._lock:
return ( self._file_network_job, self._gallery_network_job )
2018-07-18 21:07:15 +00:00
2018-11-21 22:22:36 +00:00
def GetNewHashes( self ):
with self._lock:
fsc = self._file_seed_cache
2018-11-21 22:22:36 +00:00
2021-06-30 21:27:35 +00:00
file_import_options = FileImportOptions.FileImportOptions()
file_import_options.SetPresentationOptions( True, False, False )
return fsc.GetPresentedHashes( file_import_options )
2018-11-21 22:22:36 +00:00
2019-10-16 20:47:55 +00:00
def GetNumSeeds( self ):
with self._lock:
return len( self._file_seed_cache ) + len( self._gallery_seed_log )
2018-07-18 21:07:15 +00:00
def GetOptions( self ):
with self._lock:
return ( self._file_import_options, self._tag_import_options, self._file_limit )
2018-08-01 20:44:57 +00:00
def GetPresentedHashes( self ):
with self._lock:
fsc = self._file_seed_cache
fio = self._file_import_options
2018-08-01 20:44:57 +00:00
return fsc.GetPresentedHashes( fio )
2018-08-01 20:44:57 +00:00
def GetQueryText( self ):
with self._lock:
return self._query
2020-07-29 20:52:44 +00:00
def GetSimpleStatus( self ):
with self._lock:
2021-02-11 01:59:52 +00:00
gallery_work_to_do = self._gallery_seed_log.WorkToDo()
files_work_to_do = self._file_seed_cache.WorkToDo()
gallery_go = gallery_work_to_do and not self._gallery_paused
files_go = files_work_to_do and not self._files_paused
if not ( gallery_work_to_do or files_work_to_do ):
2020-07-29 20:52:44 +00:00
2021-02-11 01:59:52 +00:00
return ( ClientImporting.DOWNLOADER_SIMPLE_STATUS_DONE, 'DONE' )
2020-07-29 20:52:44 +00:00
2021-02-11 01:59:52 +00:00
elif self._gallery_status != '' or self._file_status != '':
2020-07-29 20:52:44 +00:00
2021-02-11 01:59:52 +00:00
return ( ClientImporting.DOWNLOADER_SIMPLE_STATUS_WORKING, 'working' )
elif gallery_go or files_go:
return ( ClientImporting.DOWNLOADER_SIMPLE_STATUS_PENDING, 'pending' )
2020-07-29 20:52:44 +00:00
else:
2021-02-11 01:59:52 +00:00
return ( ClientImporting.DOWNLOADER_SIMPLE_STATUS_PAUSED, '' )
2020-07-29 20:52:44 +00:00
2018-09-05 20:52:32 +00:00
def GetSourceName( self ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-09-05 20:52:32 +00:00
return self._source_name
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
def GetStatus( self ):
2018-08-01 20:44:57 +00:00
with self._lock:
2018-10-17 21:00:09 +00:00
if HydrusData.TimeHasPassed( self._no_work_until ):
gallery_status = self._gallery_status
2019-03-06 23:06:22 +00:00
file_status = self._file_status
2018-10-17 21:00:09 +00:00
else:
2020-11-11 22:20:16 +00:00
no_work_text = '{}: {}'.format( HydrusData.ConvertTimestampToPrettyExpires( self._no_work_until ), self._no_work_until_reason )
2018-10-17 21:00:09 +00:00
gallery_status = no_work_text
2019-03-06 23:06:22 +00:00
file_status = no_work_text
2018-10-17 21:00:09 +00:00
2019-03-06 23:06:22 +00:00
return ( gallery_status, file_status, self._files_paused, self._gallery_paused )
2018-08-01 20:44:57 +00:00
2018-09-05 20:52:32 +00:00
def GetTagImportOptions( self ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-09-05 20:52:32 +00:00
return self._tag_import_options
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
def GetValueRange( self ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-09-05 20:52:32 +00:00
return self._file_seed_cache.GetValueRange()
2018-07-18 21:07:15 +00:00
def NotifyFileSeedsUpdated( self, file_seed_cache_key, file_seeds ):
if file_seed_cache_key == self._file_seed_cache.GetFileSeedCacheKey():
ClientImporting.WakeRepeatingJob( self._files_repeating_job )
2018-09-12 21:36:26 +00:00
def NotifyGallerySeedsUpdated( self, gallery_seed_log_key, gallery_seeds ):
if gallery_seed_log_key == self._gallery_seed_log.GetGallerySeedLogKey():
ClientImporting.WakeRepeatingJob( self._gallery_repeating_job )
2018-07-18 21:07:15 +00:00
def PausePlayFiles( self ):
with self._lock:
self._files_paused = not self._files_paused
ClientImporting.WakeRepeatingJob( self._files_repeating_job )
def PausePlayGallery( self ):
with self._lock:
self._gallery_paused = not self._gallery_paused
ClientImporting.WakeRepeatingJob( self._gallery_repeating_job )
2018-08-01 20:44:57 +00:00
def PublishToPage( self, publish_to_page ):
2018-07-18 21:07:15 +00:00
with self._lock:
self._publish_to_page = publish_to_page
2018-08-01 20:44:57 +00:00
def Repage( self, page_key ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
self._page_key = page_key
2018-07-18 21:07:15 +00:00
2018-10-03 21:00:15 +00:00
def RetryFailed( self ):
with self._lock:
2020-06-11 12:01:08 +00:00
self._file_seed_cache.RetryFailed()
2018-10-03 21:00:15 +00:00
2021-07-14 20:42:19 +00:00
def RetryIgnored( self, ignored_regex = None ):
2020-01-02 03:05:35 +00:00
with self._lock:
2021-07-14 20:42:19 +00:00
self._file_seed_cache.RetryIgnored( ignored_regex = ignored_regex )
2020-01-02 03:05:35 +00:00
2018-07-18 21:07:15 +00:00
def SetFileLimit( self, file_limit ):
with self._lock:
self._file_limit = file_limit
def SetFileImportOptions( self, file_import_options ):
with self._lock:
self._file_import_options = file_import_options
2018-08-01 20:44:57 +00:00
def SetFileSeedCache( self, file_seed_cache ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
self._file_seed_cache = file_seed_cache
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def SetGallerySeedLog( self, gallery_seed_log ):
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
with self._lock:
self._gallery_seed_log = gallery_seed_log
def SetTagImportOptions( self, tag_import_options ):
with self._lock:
self._tag_import_options = tag_import_options
def Start( self, page_key, publish_to_page ):
2021-07-07 20:48:57 +00:00
with self._lock:
if self._have_started:
return
self._page_key = page_key
self._publish_to_page = publish_to_page
self._files_repeating_job = HG.client_controller.CallRepeating( ClientImporting.GetRepeatingJobInitialDelay(), ClientImporting.REPEATING_JOB_TYPICAL_PERIOD, self.REPEATINGWorkOnFiles )
self._gallery_repeating_job = HG.client_controller.CallRepeating( ClientImporting.GetRepeatingJobInitialDelay(), ClientImporting.REPEATING_JOB_TYPICAL_PERIOD, self.REPEATINGWorkOnGallery )
self._files_repeating_job.SetThreadSlotType( 'gallery_files' )
self._gallery_repeating_job.SetThreadSlotType( 'gallery_search' )
self._have_started = True
2019-01-16 22:40:53 +00:00
2018-07-18 21:07:15 +00:00
def REPEATINGWorkOnFiles( self ):
with self._lock:
if ClientImporting.PageImporterShouldStopWorking( self._page_key ):
self._files_repeating_job.Cancel()
return
2018-10-31 21:41:14 +00:00
files_paused = self._files_paused or HG.client_controller.new_options.GetBoolean( 'pause_all_file_queues' )
work_pending = self._file_seed_cache.WorkToDo() and not files_paused
2018-07-18 21:07:15 +00:00
no_delays = HydrusData.TimeHasPassed( self._no_work_until )
page_shown = not HG.client_controller.PageClosedButNotDestroyed( self._page_key )
2018-09-19 21:54:51 +00:00
network_engine_good = not HG.client_controller.network_engine.IsBusy()
2018-07-18 21:07:15 +00:00
2018-09-19 21:54:51 +00:00
ok_to_work = work_pending and no_delays and page_shown and network_engine_good
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
try:
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
while ok_to_work:
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
try:
self._WorkOnFiles()
HG.client_controller.WaitUntilViewFree()
except Exception as e:
HydrusData.ShowException( e )
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
with self._lock:
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
if ClientImporting.PageImporterShouldStopWorking( self._page_key ):
self._files_repeating_job.Cancel()
return
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
files_paused = self._files_paused or HG.client_controller.new_options.GetBoolean( 'pause_all_file_queues' )
work_pending = self._file_seed_cache.WorkToDo() and not files_paused
no_delays = HydrusData.TimeHasPassed( self._no_work_until )
page_shown = not HG.client_controller.PageClosedButNotDestroyed( self._page_key )
network_engine_good = not HG.client_controller.network_engine.IsBusy()
ok_to_work = work_pending and no_delays and page_shown and network_engine_good
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
finally:
with self._lock:
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
self._file_status = ''
2018-07-18 21:07:15 +00:00
def REPEATINGWorkOnGallery( self ):
with self._lock:
if ClientImporting.PageImporterShouldStopWorking( self._page_key ):
self._gallery_repeating_job.Cancel()
return
2018-10-31 21:41:14 +00:00
gallery_paused = self._gallery_paused or HG.client_controller.new_options.GetBoolean( 'pause_all_gallery_searches' )
work_pending = self._gallery_seed_log.WorkToDo() and not gallery_paused
2018-07-18 21:07:15 +00:00
no_delays = HydrusData.TimeHasPassed( self._no_work_until )
page_shown = not HG.client_controller.PageClosedButNotDestroyed( self._page_key )
2018-09-19 21:54:51 +00:00
network_engine_good = not HG.client_controller.network_engine.IsBusy()
2018-07-18 21:07:15 +00:00
2018-09-19 21:54:51 +00:00
ok_to_work = work_pending and no_delays and page_shown and network_engine_good
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
try:
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
while ok_to_work:
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
try:
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
self._WorkOnGallery()
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
time.sleep( 1 )
HG.client_controller.WaitUntilViewFree()
except Exception as e:
HydrusData.ShowException( e )
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
with self._lock:
if ClientImporting.PageImporterShouldStopWorking( self._page_key ):
self._gallery_repeating_job.Cancel()
return
gallery_paused = self._gallery_paused or HG.client_controller.new_options.GetBoolean( 'pause_all_gallery_searches' )
work_pending = self._gallery_seed_log.WorkToDo() and not gallery_paused
no_delays = HydrusData.TimeHasPassed( self._no_work_until )
page_shown = not HG.client_controller.PageClosedButNotDestroyed( self._page_key )
network_engine_good = not HG.client_controller.network_engine.IsBusy()
ok_to_work = work_pending and no_delays and page_shown and network_engine_good
2018-07-18 21:07:15 +00:00
2021-02-11 01:59:52 +00:00
finally:
2018-08-15 20:40:30 +00:00
2021-02-11 01:59:52 +00:00
with self._lock:
self._gallery_status = ''
2018-08-15 20:40:30 +00:00
2018-07-18 21:07:15 +00:00
HydrusSerialisable.SERIALISABLE_TYPES_TO_OBJECT_TYPES[ HydrusSerialisable.SERIALISABLE_TYPE_GALLERY_IMPORT ] = GalleryImport
class MultipleGalleryImport( HydrusSerialisable.SerialisableBase ):
SERIALISABLE_TYPE = HydrusSerialisable.SERIALISABLE_TYPE_MULTIPLE_GALLERY_IMPORT
SERIALISABLE_NAME = 'Multiple Gallery Import'
2019-01-16 22:40:53 +00:00
SERIALISABLE_VERSION = 7
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
def __init__( self, gug_key_and_name = None ):
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
if gug_key_and_name is None:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
gug_key_and_name = ( HydrusData.GenerateKey(), 'unknown source' )
2018-07-18 21:07:15 +00:00
HydrusSerialisable.SerialisableBase.__init__( self )
2018-08-01 20:44:57 +00:00
self._lock = threading.Lock()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._page_key = 'initialising page key'
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
self._gug_key_and_name = gug_key_and_name
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._highlighted_gallery_import_key = None
2018-07-18 21:07:15 +00:00
self._file_limit = HC.options[ 'gallery_file_limit' ]
2018-11-28 22:31:04 +00:00
self._start_file_queues_paused = False
self._start_gallery_queues_paused = False
2019-01-16 22:40:53 +00:00
self._merge_simultaneous_pends_to_one_importer = False
2018-11-28 22:31:04 +00:00
2018-07-18 21:07:15 +00:00
self._file_import_options = HG.client_controller.new_options.GetDefaultFileImportOptions( 'loud' )
2021-06-30 21:27:35 +00:00
self._tag_import_options = TagImportOptions.TagImportOptions( is_default = True )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._gallery_imports = HydrusSerialisable.SerialisableList()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._gallery_import_keys_to_gallery_imports = {}
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._status_dirty = True
2020-06-11 12:01:08 +00:00
self._status_cache = ClientImportFileSeeds.FileSeedCacheStatus()
2018-07-18 21:07:15 +00:00
2018-08-08 20:29:54 +00:00
self._last_time_imports_changed = HydrusData.GetNowPrecise()
2021-07-07 20:48:57 +00:00
self._have_started = False
2018-08-01 20:44:57 +00:00
self._last_pubbed_value_range = ( 0, 0 )
self._next_pub_value_check_time = 0
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._importers_repeating_job = None
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def _AddGalleryImport( self, gallery_import ):
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_import.PublishToPage( False )
gallery_import.Repage( self._page_key )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._gallery_imports.append( gallery_import )
2018-07-18 21:07:15 +00:00
2018-08-08 20:29:54 +00:00
self._last_time_imports_changed = HydrusData.GetNowPrecise()
2018-08-01 20:44:57 +00:00
gallery_import_key = gallery_import.GetGalleryImportKey()
self._gallery_import_keys_to_gallery_imports[ gallery_import_key ] = gallery_import
2019-01-16 22:40:53 +00:00
if len( self._gallery_imports ) == 1:
2018-08-01 20:44:57 +00:00
self._highlighted_gallery_import_key = gallery_import_key
2018-07-18 21:07:15 +00:00
def _GetSerialisableInfo( self ):
2018-09-05 20:52:32 +00:00
( gug_key, gug_name ) = self._gug_key_and_name
2019-01-09 22:59:03 +00:00
serialisable_gug_key_and_name = ( gug_key.hex(), gug_name )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
if self._highlighted_gallery_import_key is None:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
serialisable_highlighted_gallery_import_key = self._highlighted_gallery_import_key
2018-07-18 21:07:15 +00:00
else:
2019-01-09 22:59:03 +00:00
serialisable_highlighted_gallery_import_key = self._highlighted_gallery_import_key.hex()
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
pend_options = ( self._start_file_queues_paused, self._start_gallery_queues_paused, self._merge_simultaneous_pends_to_one_importer )
2018-07-18 21:07:15 +00:00
serialisable_file_import_options = self._file_import_options.GetSerialisableTuple()
serialisable_tag_import_options = self._tag_import_options.GetSerialisableTuple()
2018-08-01 20:44:57 +00:00
serialisable_gallery_imports = self._gallery_imports.GetSerialisableTuple()
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
return ( serialisable_gug_key_and_name, serialisable_highlighted_gallery_import_key, self._file_limit, pend_options, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports )
2018-07-18 21:07:15 +00:00
def _InitialiseFromSerialisableInfo( self, serialisable_info ):
2019-01-16 22:40:53 +00:00
( serialisable_gug_key_and_name, serialisable_highlighted_gallery_import_key, self._file_limit, pend_options, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports ) = serialisable_info
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
( serialisable_gug_key, gug_name ) = serialisable_gug_key_and_name
2019-01-09 22:59:03 +00:00
self._gug_key_and_name = ( bytes.fromhex( serialisable_gug_key ), gug_name )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
if serialisable_highlighted_gallery_import_key is None:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._highlighted_gallery_import_key = None
2018-07-18 21:07:15 +00:00
else:
2019-01-09 22:59:03 +00:00
self._highlighted_gallery_import_key = bytes.fromhex( serialisable_highlighted_gallery_import_key )
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
( self._start_file_queues_paused, self._start_gallery_queues_paused, self._merge_simultaneous_pends_to_one_importer ) = pend_options
2018-07-18 21:07:15 +00:00
self._file_import_options = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_file_import_options )
self._tag_import_options = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_tag_import_options )
2018-08-01 20:44:57 +00:00
self._gallery_imports = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_gallery_imports )
self._gallery_import_keys_to_gallery_imports = { gallery_import.GetGalleryImportKey() : gallery_import for gallery_import in self._gallery_imports }
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def _RegenerateStatus( self ):
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
file_seed_caches = [ gallery_import.GetFileSeedCache() for gallery_import in self._gallery_imports ]
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._status_cache = ClientImportFileSeeds.GenerateFileSeedCachesStatus( file_seed_caches )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._status_dirty = False
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def _RemoveGalleryImport( self, gallery_import_key ):
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
if gallery_import_key not in self._gallery_import_keys_to_gallery_imports:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_import = self._gallery_import_keys_to_gallery_imports[ gallery_import_key ]
gallery_import.PublishToPage( False )
gallery_import.Repage( 'dead page key' )
self._gallery_imports.remove( gallery_import )
2018-08-08 20:29:54 +00:00
self._last_time_imports_changed = HydrusData.GetNowPrecise()
2018-08-01 20:44:57 +00:00
del self._gallery_import_keys_to_gallery_imports[ gallery_import_key ]
def _SetDirty( self ):
self._status_dirty = True
2018-07-18 21:07:15 +00:00
def _UpdateSerialisableInfo( self, version, old_serialisable_info ):
if version == 1:
( serialisable_gallery_identifier, serialisable_gallery_stream_identifiers, serialisable_current_query_stuff, pending_queries, get_tags_if_url_recognised_and_file_redundant, file_limit, gallery_paused, files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_file_seed_cache ) = old_serialisable_info
new_serialisable_info = ( serialisable_gallery_identifier, serialisable_gallery_stream_identifiers, serialisable_current_query_stuff, pending_queries, file_limit, gallery_paused, files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_file_seed_cache )
return ( 2, new_serialisable_info )
if version == 2:
( serialisable_gallery_identifier, serialisable_gallery_stream_identifiers, serialisable_current_query_stuff, pending_queries, file_limit, gallery_paused, files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_file_seed_cache ) = old_serialisable_info
gallery_seed_log = ClientImportGallerySeeds.GallerySeedLog()
serialisable_gallery_seed_log = gallery_seed_log.GetSerialisableTuple()
new_serialisable_info = ( serialisable_gallery_identifier, serialisable_gallery_stream_identifiers, serialisable_current_query_stuff, pending_queries, file_limit, gallery_paused, files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_seed_log, serialisable_file_seed_cache )
return ( 3, new_serialisable_info )
2018-08-01 20:44:57 +00:00
if version == 3:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
( serialisable_gallery_identifier, serialisable_gallery_stream_identifiers, serialisable_current_query_stuff, pending_queries, file_limit, gallery_paused, files_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_seed_log, serialisable_file_seed_cache ) = old_serialisable_info
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
( current_query, current_query_num_new_urls, serialisable_current_gallery_stream_identifier, current_gallery_stream_identifier_page_index, serialisable_current_gallery_stream_identifier_found_urls, serialisable_pending_gallery_stream_identifiers ) = serialisable_current_query_stuff
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
highlighted_gallery_import_key = None
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
serialisable_highlighted_gallery_import_key = highlighted_gallery_import_key
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_imports = HydrusSerialisable.SerialisableList()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
file_import_options = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_file_import_options )
tag_import_options = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_tag_import_options )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_seed_log = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_gallery_seed_log )
file_seed_cache = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_file_seed_cache )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
if len( file_seed_cache ) > 0:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
current_query = 'queue brought from old page'
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
gallery_import = GalleryImport( query = current_query, source_name = 'updated from old system', initial_search_urls = [] )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_import.PausePlayGallery()
gallery_import.PausePlayFiles()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_import.SetFileLimit( file_limit )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_import.SetFileImportOptions( file_import_options )
gallery_import.SetTagImportOptions( tag_import_options )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_import.SetFileSeedCache( file_seed_cache )
gallery_import.SetGallerySeedLog( gallery_seed_log )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
gallery_imports.append( gallery_import )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
serialisable_gallery_imports = gallery_imports.GetSerialisableTuple()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
new_serialisable_info = ( serialisable_gallery_identifier, serialisable_highlighted_gallery_import_key, file_limit, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return ( 4, new_serialisable_info )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
2018-09-05 20:52:32 +00:00
if version == 4:
( serialisable_gallery_identifier, serialisable_highlighted_gallery_import_key, file_limit, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports ) = old_serialisable_info
gallery_identifier = HydrusSerialisable.CreateFromSerialisableTuple( serialisable_gallery_identifier )
( gug_key, gug_name ) = ClientDownloading.ConvertGalleryIdentifierToGUGKeyAndName( gallery_identifier )
2019-01-09 22:59:03 +00:00
serialisable_gug_key_and_name = ( HydrusData.GenerateKey().hex(), gug_name )
2018-09-05 20:52:32 +00:00
new_serialisable_info = ( serialisable_gug_key_and_name, serialisable_highlighted_gallery_import_key, file_limit, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports )
return ( 5, new_serialisable_info )
2018-11-28 22:31:04 +00:00
if version == 5:
( serialisable_gug_key_and_name, serialisable_highlighted_gallery_import_key, file_limit, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports ) = old_serialisable_info
start_file_queues_paused = False
start_gallery_queues_paused = False
new_serialisable_info = ( serialisable_gug_key_and_name, serialisable_highlighted_gallery_import_key, file_limit, start_file_queues_paused, start_gallery_queues_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports )
return ( 6, new_serialisable_info )
2019-01-16 22:40:53 +00:00
if version == 6:
( serialisable_gug_key_and_name, serialisable_highlighted_gallery_import_key, file_limit, start_file_queues_paused, start_gallery_queues_paused, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports ) = old_serialisable_info
merge_simultaneous_pends_to_one_importer = False
pend_options = ( start_file_queues_paused, start_gallery_queues_paused, merge_simultaneous_pends_to_one_importer )
new_serialisable_info = ( serialisable_gug_key_and_name, serialisable_highlighted_gallery_import_key, file_limit, pend_options, serialisable_file_import_options, serialisable_tag_import_options, serialisable_gallery_imports )
return ( 7, new_serialisable_info )
2018-08-01 20:44:57 +00:00
def CurrentlyWorking( self ):
with self._lock:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return True in ( gallery_import.CurrentlyWorking() for gallery_import in self._gallery_imports )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
2019-08-21 21:34:01 +00:00
def GetAPIInfoDict( self, simple ):
with self._lock:
d = {}
d[ 'gallery_imports' ] = [ gallery_import.GetAPIInfoDict( simple ) for gallery_import in self._gallery_imports ]
if self._highlighted_gallery_import_key is None:
d[ 'highlight' ] = None
else:
d[ 'highlight' ] = self._highlighted_gallery_import_key.hex()
return d
2018-08-01 20:44:57 +00:00
def GetFileLimit( self ):
with self._lock:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return self._file_limit
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def GetFileImportOptions( self ):
with self._lock:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return self._file_import_options
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
2018-09-05 20:52:32 +00:00
def GetGalleryImports( self ):
2018-08-01 20:44:57 +00:00
with self._lock:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
return list( self._gallery_imports )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
2018-09-05 20:52:32 +00:00
def GetGUGKeyAndName( self ):
2018-08-01 20:44:57 +00:00
with self._lock:
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
return self._gug_key_and_name
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def GetHighlightedGalleryImport( self ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
if self._highlighted_gallery_import_key is not None:
if self._highlighted_gallery_import_key in self._gallery_import_keys_to_gallery_imports:
return self._gallery_import_keys_to_gallery_imports[ self._highlighted_gallery_import_key ]
self._highlighted_gallery_import_key = None
return None
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
2018-09-05 20:52:32 +00:00
def GetInitialSearchText( self ):
return HG.client_controller.network_engine.domain_manager.GetInitialSearchText( self._gug_key_and_name )
2018-08-08 20:29:54 +00:00
def GetLastTimeImportsChanged( self ):
with self._lock:
return self._last_time_imports_changed
def GetNumGalleryImports( self ):
with self._lock:
return len( self._gallery_imports )
2019-10-16 20:47:55 +00:00
def GetNumSeeds( self ):
with self._lock:
return sum( ( gallery_import.GetNumSeeds() for gallery_import in self._gallery_imports ) )
2018-11-28 22:31:04 +00:00
def GetQueueStartSettings( self ):
with self._lock:
2019-01-16 22:40:53 +00:00
return ( self._start_file_queues_paused, self._start_gallery_queues_paused, self._merge_simultaneous_pends_to_one_importer )
2018-11-28 22:31:04 +00:00
2018-08-01 20:44:57 +00:00
def GetTagImportOptions( self ):
with self._lock:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return self._tag_import_options
2018-07-18 21:07:15 +00:00
2020-06-11 12:01:08 +00:00
def GetTotalStatus( self ) -> ClientImportFileSeeds.FileSeedCacheStatus:
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
if self._status_dirty:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._RegenerateStatus()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return self._status_cache
def GetValueRange( self ):
with self._lock:
total_value = 0
total_range = 0
for gallery_import in self._gallery_imports:
2021-06-09 20:28:09 +00:00
( v, r ) = gallery_import.GetValueRange()
2018-07-18 21:07:15 +00:00
2021-06-09 20:28:09 +00:00
if v != r:
2018-07-18 21:07:15 +00:00
2021-06-09 20:28:09 +00:00
total_value += v
total_range += r
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
return ( total_value, total_range )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
2021-06-09 20:28:09 +00:00
def PendSubscriptionGapDownloader( self, gug_key_and_name, query_text, file_limit ):
with self._lock:
2021-06-23 21:11:38 +00:00
gug = HG.client_controller.network_engine.domain_manager.GetGUG( gug_key_and_name )
2021-06-09 20:28:09 +00:00
if gug is None:
HydrusData.ShowText( 'Could not find a Gallery URL Generator for "{}"!'.format( self._gug_key_and_name[1] ) )
return
initial_search_urls = gug.GenerateGalleryURLs( query_text )
if len( initial_search_urls ) == 0:
HydrusData.ShowText( 'The Gallery URL Generator "{}" did not produce any URLs!'.format( self._gug_key_and_name[1] ) )
return
2021-06-23 21:11:38 +00:00
gallery_import = GalleryImport( query = query_text, source_name = gug_key_and_name[1], initial_search_urls = initial_search_urls, start_file_queue_paused = self._start_file_queues_paused, start_gallery_queue_paused = self._start_gallery_queues_paused )
2021-06-09 20:28:09 +00:00
gallery_import.SetFileLimit( file_limit )
gallery_import.SetFileImportOptions( self._file_import_options )
gallery_import.SetTagImportOptions( self._tag_import_options )
publish_to_page = False
gallery_import.Start( self._page_key, publish_to_page )
self._AddGalleryImport( gallery_import )
ClientImporting.WakeRepeatingJob( self._importers_repeating_job )
self._SetDirty()
2019-01-16 22:40:53 +00:00
def PendQueries( self, query_texts ):
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
created_importers = []
2018-08-08 20:29:54 +00:00
2018-07-18 21:07:15 +00:00
with self._lock:
2018-09-05 20:52:32 +00:00
gug = HG.client_controller.network_engine.domain_manager.GetGUG( self._gug_key_and_name )
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
if gug is None:
2018-07-18 21:07:15 +00:00
2020-11-11 22:20:16 +00:00
HydrusData.ShowText( 'Could not find a Gallery URL Generator for "{}"!'.format( self._gug_key_and_name[1] ) )
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
return created_importers
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
self._gug_key_and_name = gug.GetGUGKeyAndName() # just a refresher, to keep up with any changes
2019-01-16 22:40:53 +00:00
groups_of_query_data = []
2018-09-05 20:52:32 +00:00
2019-01-16 22:40:53 +00:00
for query_text in query_texts:
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
initial_search_urls = gug.GenerateGalleryURLs( query_text )
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
if len( initial_search_urls ) == 0:
2020-11-11 22:20:16 +00:00
HydrusData.ShowText( 'The Gallery URL Generator "{}" did not produce any URLs!'.format( self._gug_key_and_name[1] ) )
2019-01-16 22:40:53 +00:00
return created_importers
groups_of_query_data.append( ( query_text, initial_search_urls ) )
2018-08-08 20:29:54 +00:00
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
if self._merge_simultaneous_pends_to_one_importer and len( groups_of_query_data ) > 1:
# flatten these groups down to one
all_search_urls_flat = []
for ( query_text, initial_search_urls ) in groups_of_query_data:
all_search_urls_flat.extend( initial_search_urls )
query_text = HydrusData.ToHumanInt( len( groups_of_query_data ) ) + ' queries'
groups_of_query_data = [ ( query_text, all_search_urls_flat ) ]
2018-09-05 20:52:32 +00:00
2019-01-16 22:40:53 +00:00
for ( query_text, initial_search_urls ) in groups_of_query_data:
gallery_import = GalleryImport( query = query_text, source_name = self._gug_key_and_name[1], initial_search_urls = initial_search_urls, start_file_queue_paused = self._start_file_queues_paused, start_gallery_queue_paused = self._start_gallery_queues_paused )
gallery_import.SetFileLimit( self._file_limit )
gallery_import.SetFileImportOptions( self._file_import_options )
gallery_import.SetTagImportOptions( self._tag_import_options )
publish_to_page = False
gallery_import.Start( self._page_key, publish_to_page )
self._AddGalleryImport( gallery_import )
created_importers.append( gallery_import )
2018-09-05 20:52:32 +00:00
2018-08-01 20:44:57 +00:00
ClientImporting.WakeRepeatingJob( self._importers_repeating_job )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._SetDirty()
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
return created_importers
2018-08-08 20:29:54 +00:00
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def RemoveGalleryImport( self, gallery_import_key ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
self._RemoveGalleryImport( gallery_import_key )
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._SetDirty()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def SetFileLimit( self, file_limit ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
self._file_limit = file_limit
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def SetFileImportOptions( self, file_import_options ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
self._file_import_options = file_import_options
2018-07-18 21:07:15 +00:00
2018-09-05 20:52:32 +00:00
def SetGUGKeyAndName( self, gug_key_and_name ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-09-05 20:52:32 +00:00
self._gug_key_and_name = gug_key_and_name
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def SetHighlightedGalleryImport( self, highlighted_gallery_import ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
if highlighted_gallery_import is None:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._highlighted_gallery_import_key = None
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
else:
self._highlighted_gallery_import_key = highlighted_gallery_import.GetGalleryImportKey()
2018-07-18 21:07:15 +00:00
2019-01-16 22:40:53 +00:00
def SetQueueStartSettings( self, start_file_queues_paused, start_gallery_queues_paused, merge_simultaneous_pends_to_one_importer ):
2018-11-28 22:31:04 +00:00
with self._lock:
self._start_file_queues_paused = start_file_queues_paused
self._start_gallery_queues_paused = start_gallery_queues_paused
2019-01-16 22:40:53 +00:00
self._merge_simultaneous_pends_to_one_importer = merge_simultaneous_pends_to_one_importer
2018-11-28 22:31:04 +00:00
2018-08-01 20:44:57 +00:00
def SetTagImportOptions( self, tag_import_options ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
self._tag_import_options = tag_import_options
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def Start( self, page_key ):
2018-07-18 21:07:15 +00:00
with self._lock:
2021-07-07 20:48:57 +00:00
if self._have_started:
return
2018-08-01 20:44:57 +00:00
self._page_key = page_key
2018-07-18 21:07:15 +00:00
2021-07-07 20:48:57 +00:00
# set a 2s period so the page value/range is breddy snappy
self._importers_repeating_job = HG.client_controller.CallRepeating( ClientImporting.GetRepeatingJobInitialDelay(), 2.0, self.REPEATINGWorkOnImporters )
2018-07-18 21:07:15 +00:00
2021-07-07 20:48:57 +00:00
for gallery_import in self._gallery_imports:
publish_to_page = gallery_import.GetGalleryImportKey() == self._highlighted_gallery_import_key
gallery_import.Start( page_key, publish_to_page )
2018-07-18 21:07:15 +00:00
2021-07-07 20:48:57 +00:00
self._have_started = True
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
def REPEATINGWorkOnImporters( self ):
2018-07-18 21:07:15 +00:00
with self._lock:
2018-08-01 20:44:57 +00:00
if ClientImporting.PageImporterShouldStopWorking( self._page_key ):
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._importers_repeating_job.Cancel()
2018-07-18 21:07:15 +00:00
return
2018-08-01 20:44:57 +00:00
if not self._status_dirty: # if we think we are clean
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
for gallery_import in self._gallery_imports:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
file_seed_cache = gallery_import.GetFileSeedCache()
2018-07-18 21:07:15 +00:00
2020-06-11 12:01:08 +00:00
if file_seed_cache.GetStatus().GetGenerationTime() > self._status_cache.GetGenerationTime(): # has there has been an update?
2018-08-01 20:44:57 +00:00
self._SetDirty()
break
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
if HydrusData.TimeHasPassed( self._next_pub_value_check_time ):
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._next_pub_value_check_time = HydrusData.GetNow() + 5
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
current_value_range = self.GetValueRange()
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
if current_value_range != self._last_pubbed_value_range:
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
self._last_pubbed_value_range = current_value_range
2018-07-18 21:07:15 +00:00
2018-08-01 20:44:57 +00:00
HG.client_controller.pub( 'refresh_page_name', self._page_key )
2018-07-18 21:07:15 +00:00
HydrusSerialisable.SERIALISABLE_TYPES_TO_OBJECT_TYPES[ HydrusSerialisable.SERIALISABLE_TYPE_MULTIPLE_GALLERY_IMPORT ] = MultipleGalleryImport