Created
June 23, 2025 04:07
-
-
Save ThinkSalat/fb9801e3d4402d388e44e603fbfd668e to your computer and use it in GitHub Desktop.
Tube Archivist import
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| """ | |
| Script to bulk import YouTube channels into TubeArchivist from a text file. | |
| """ | |
| import sys | |
| import time | |
| import subprocess | |
| def subscribe_to_channel(channel_identifier): | |
| """Subscribe to a channel in TubeArchivist using the identifier (URL, handle, or ID).""" | |
| try: | |
| # Clean up the identifier | |
| channel = channel_identifier.strip() | |
| # Format the identifier based on the debug info we observed | |
| # If it's a URL with a handle, extract just the handle | |
| if '@' in channel and ('youtube.com' in channel or 'youtu.be' in channel): | |
| parts = channel.split('@') | |
| channel = '@' + parts[1].split('/')[0] | |
| # If it's a standard channel ID URL, extract the ID | |
| elif '/channel/' in channel: | |
| channel = channel.split('/channel/')[1].split('/')[0].strip() | |
| # Use the subscribe_to function with the identifier | |
| cmd = ['docker', 'exec', 'tube-archivist', 'bash', '-c', | |
| f'cd /app && python manage.py shell -c "from task.tasks import subscribe_to; subscribe_to.delay(\\"{channel}\\")"'] | |
| result = subprocess.run(cmd, capture_output=True, text=True) | |
| if result.returncode == 0: | |
| return True | |
| else: | |
| print(f"Error output: {result.stderr}") | |
| return False | |
| except Exception as e: | |
| print(f"Error subscribing to channel {channel_identifier}: {e}") | |
| return False | |
| def main(): | |
| if len(sys.argv) != 2: | |
| print(f"Usage: {sys.argv[0]} channels_file.txt") | |
| sys.exit(1) | |
| channels_file = sys.argv[1] | |
| try: | |
| with open(channels_file, 'r') as f: | |
| channels = f.readlines() | |
| except Exception as e: | |
| print(f"Error reading file: {e}") | |
| sys.exit(1) | |
| # Clean up channel identifiers | |
| channels = [channel.strip() for channel in channels if channel.strip()] | |
| successful = 0 | |
| failed = 0 | |
| # Process each channel | |
| for i, channel in enumerate(channels): | |
| print(f"Processing {i+1}/{len(channels)}: {channel}") | |
| if subscribe_to_channel(channel): | |
| successful += 1 | |
| print(f" Success!") | |
| else: | |
| failed += 1 | |
| print(f" Failed to subscribe.") | |
| # Sleep briefly to avoid overwhelming the system | |
| time.sleep(1) | |
| print(f"Finished processing {len(channels)} channels") | |
| print(f"Successfully subscribed to {successful} channels") | |
| print(f"Failed to subscribe to {failed} channels") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment