-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Oaphi/caching
Option for skipping download of files that are already on disk
- Loading branch information
Showing
9 changed files
with
329 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
selenium==4.23.1 | ||
desktop-notifier==5.0.1 | ||
watchdog==4.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# for most sites, dump names correspond to domain name. | ||
# However, due to some subdomain shenanigans, a couple of sites differ: | ||
files_map: dict[str, str] = { | ||
'alcohol.stackexchange.com': 'beer.stackexchange.com', | ||
'alcohol.meta.stackexchange.com': 'beer.meta.stackexchange.com', | ||
'mattermodeling.stackexchange.com': 'materials.stackexchange.com', | ||
'mattermodeling.meta.stackexchange.com': 'materials.meta.stackexchange.com', | ||
'communitybuilding.stackexchange.com': 'moderators.stackexchange.com', | ||
'communitybuilding.meta.stackexchange.com': 'moderators.meta.stackexchange.com', | ||
'medicalsciences.stackexchange.com': 'health.stackexchange.com', | ||
'medicalsciences.meta.stackexchange.com': 'health.meta.stackexchange.com', | ||
'psychology.stackexchange.com': 'cogsci.stackexchange.com', | ||
'psychology.meta.stackexchange.com': 'cogsci.meta.stackexchange.com', | ||
'writing.stackexchange.com': 'writers.stackexchange.com', | ||
'writing.meta.stackexchange.com': 'writers.meta.stackexchange.com', | ||
'video.stackexchange.com': 'avp.stackexchange.com', | ||
'video.meta.stackexchange.com': 'avp.meta.stackexchange.com', | ||
'meta.es.stackoverflow.com': 'es.meta.stackoverflow.com', | ||
'meta.ja.stackoverflow.com': 'ja.meta.stackoverflow.com', | ||
'meta.pt.stackoverflow.com': 'pt.meta.stackoverflow.com', | ||
'meta.ru.stackoverflow.com': 'ru.meta.stackoverflow.com', | ||
} | ||
|
||
inverse_files_map: dict[str, str] = {v: k for k, v in files_map.items()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
|
||
from watchdog.observers.api import BaseObserverSubclassCallable | ||
from watchdog.events import FileSystemEventHandler | ||
|
||
from .state import DownloadState | ||
from ..utils import is_dump_file | ||
|
||
|
||
class CleanupHandler(FileSystemEventHandler): | ||
download_state: DownloadState | ||
observer: BaseObserverSubclassCallable | ||
|
||
def __init__(self, observer: BaseObserverSubclassCallable, state: DownloadState): | ||
super() | ||
|
||
self.download_state = state | ||
self.observer = observer | ||
|
||
def on_created(self, event): | ||
file_name = os.path.basename(event.src_path) | ||
|
||
# # we can safely ignore part file creations | ||
if file_name.endswith('.part'): | ||
return | ||
|
||
if is_dump_file(file_name): | ||
print(f"Download started: {file_name}") | ||
self.download_state.add(file_name) | ||
|
||
def on_moved(self, event): | ||
file_name: str = os.path.basename(event.dest_path) | ||
|
||
# we can safely ignore part file removals | ||
if file_name.endswith('.part'): | ||
return | ||
|
||
if is_dump_file(file_name): | ||
print(f"Download finished: {file_name}") | ||
self.download_state.remove(file_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from threading import current_thread, main_thread | ||
from watchdog.observers import Observer | ||
|
||
from .handler import CleanupHandler | ||
from .state import DownloadState | ||
|
||
|
||
def register_pending_downloads_observer(output_dir: str): | ||
if current_thread() is main_thread(): | ||
observer = Observer() | ||
state = DownloadState() | ||
handler = CleanupHandler(observer, state) | ||
|
||
observer.schedule(handler, output_dir, recursive=True) | ||
observer.start() | ||
|
||
return state, observer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import Set | ||
|
||
|
||
class DownloadState: | ||
# list of filenames pending download | ||
pending: Set[str] = set() | ||
|
||
def size(self): | ||
return len(self.pending) | ||
|
||
def empty(self): | ||
return self.size() == 0 | ||
|
||
def add(self, file: str): | ||
self.pending.add(file) | ||
|
||
def remove(self, file: str): | ||
self.pending.remove(file) | ||
|
||
|
||
download_state = DownloadState() |