reformatting
formatting_check / formatting_check (push) Failing after 3s
Python Test / test (.ps1, windows-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, macos-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, ubuntu-latest, 3.9) (push) Has been cancelled

This commit is contained in:
2026-07-14 21:32:55 +12:00
parent 3d0658b483
commit 0157f462cc
17 changed files with 439 additions and 377 deletions
+24 -50
View File
@@ -19,20 +19,16 @@ sys.path.insert(0, str(Path(__file__).parent.parent))
from bdfr.api import (
BDFRManager,
DownloadType,
DownloadStatus,
ProgressEvent,
ProgressCallback,
DownloadType,
LoggingCallback,
get_bdfr_manager
ProgressCallback,
ProgressEvent,
get_bdfr_manager,
)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s] %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
logger = logging.getLogger(__name__)
@@ -53,7 +49,7 @@ class WebSocketCallback(ProgressCallback):
async def on_error(self, event: ProgressEvent):
"""Send error update to WebSocket"""
print(f"❌ [{self.websocket_id}] ERROR: {event.message}")
if event.data.get('exception'):
if event.data.get("exception"):
print(f" Exception: {event.data['exception']}")
async def on_completed(self, event: ProgressEvent):
@@ -95,9 +91,9 @@ async def example_basic_usage():
# Create a download for a subreddit
download_id = manager.download_subreddit(
"python", # subreddit name
limit=10, # download 10 posts
sort="hot", # sort by hot
no_dupes=True # avoid duplicates
limit=10, # download 10 posts
sort="hot", # sort by hot
no_dupes=True, # avoid duplicates
)
print(f"📋 Created download with ID: {download_id}")
@@ -111,7 +107,7 @@ async def example_basic_usage():
print(f"📊 Status: {status['status']} | Progress: {int(round(status['progress']))}%")
if status['status'] in ['completed', 'failed', 'cancelled']:
if status["status"] in ["completed", "failed", "cancelled"]:
print(f"🏁 Download finished with status: {status['status']}")
break
@@ -126,11 +122,7 @@ async def example_advanced_usage():
print("=" * 50)
# Create custom callbacks
callbacks = [
LoggingCallback("web_interface"),
WebSocketCallback("user_123"),
DatabaseCallback()
]
callbacks = [LoggingCallback("web_interface"), WebSocketCallback("user_123"), DatabaseCallback()]
# Create manager with custom download directory
manager = BDFRManager("./custom_downloads")
@@ -140,11 +132,7 @@ async def example_advanced_usage():
download_ids = []
for subreddit in subreddits:
download_id = manager.create_download(
DownloadType.SUBREDDIT,
subreddit,
progress_callbacks=callbacks
)
download_id = manager.create_download(DownloadType.SUBREDDIT, subreddit, progress_callbacks=callbacks)
# Start the download
manager.start_download(download_id)
@@ -164,7 +152,7 @@ async def example_advanced_usage():
print(f"📊 {download_id}: {status['status']} ({int(round(status['progress']))}%)")
if status['status'] in ['completed', 'failed', 'cancelled']:
if status["status"] in ["completed", "failed", "cancelled"]:
print(f"🏁 Download {download_id} finished")
download_ids.remove(download_id)
else:
@@ -187,11 +175,7 @@ async def example_user_download():
# Download user's submitted posts
download_id = manager.download_user(
"testuser", # username
limit=25, # 25 posts
submitted=True,
upvoted=False,
saved=False
"testuser", limit=25, submitted=True, upvoted=False, saved=False # username # 25 posts
)
print(f"📋 Created user download: {download_id}")
@@ -205,7 +189,7 @@ async def example_user_download():
print(f"📊 Status: {status['status']} | Progress: {int(round(status['progress']))}%")
if status['status'] in ['completed', 'failed']:
if status["status"] in ["completed", "failed"]:
break
await asyncio.sleep(2)
@@ -221,11 +205,7 @@ async def example_archive_operation():
manager = BDFRManager("./archives")
# Archive subreddit data (metadata only)
download_id = manager.archive_subreddit(
"dataisbeautiful",
format_type="json",
limit=50
)
download_id = manager.archive_subreddit("dataisbeautiful", format_type="json", limit=50)
print(f"📋 Created archive operation: {download_id}")
@@ -238,7 +218,7 @@ async def example_archive_operation():
print(f"📊 Archive status: {status['status']} | Progress: {int(round(status['progress']))}%")
if status['status'] in ['completed', 'failed']:
if status["status"] in ["completed", "failed"]:
print(f"🏁 Archive finished with status: {status['status']}")
break
@@ -265,11 +245,7 @@ async def example_web_integration():
callback = WebSocketCallback(f"ws_{user_id}")
# Create and start download
download_id = self.manager.download_subreddit(
subreddit,
limit=limit,
progress_callbacks=[callback]
)
download_id = self.manager.download_subreddit(subreddit, limit=limit, progress_callbacks=[callback])
# Track for this user session
if user_id not in self.active_sessions:
@@ -279,7 +255,7 @@ async def example_web_integration():
return {
"success": True,
"download_id": download_id,
"message": f"Started download of r/{subreddit} (limit: {limit})"
"message": f"Started download of r/{subreddit} (limit: {limit})",
}
async def get_user_downloads(self, user_id: str):
@@ -329,7 +305,7 @@ async def example_web_integration():
# Cancel one download
if user_downloads:
cancel_result = await app.cancel_user_download(user_id, user_downloads[0]['id'])
cancel_result = await app.cancel_user_download(user_id, user_downloads[0]["id"])
print(f"Cancel result: {cancel_result}")
return len(user_downloads)
@@ -343,10 +319,7 @@ async def example_error_handling():
manager = BDFRManager("./test_downloads")
# Try to download from a non-existent subreddit
download_id = manager.download_subreddit(
"this_subreddit_does_not_exist",
limit=5
)
download_id = manager.download_subreddit("this_subreddit_does_not_exist", limit=5)
print(f"📋 Created download for non-existent subreddit: {download_id}")
@@ -359,7 +332,7 @@ async def example_error_handling():
print(f"📊 Status: {status['status']}")
if status['status'] == 'failed':
if status["status"] == "failed":
print(f"🏁 Download failed as expected: {status.get('error', 'Unknown error')}")
break
@@ -401,9 +374,10 @@ async def main():
except Exception as e:
print(f"\n❌ Error running examples: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
# Run the examples
asyncio.run(main())
asyncio.run(main())