--- configs: - config_name: default data_files: - split: season_1 path: data/season_1-* - split: season_2 path: data/season_2-* - split: season_3 path: data/season_3-* - split: season_4 path: data/season_4-* - split: season_5 path: data/season_5-* - split: season_6 path: data/season_6-* - split: season_7 path: data/season_7-* dataset_info: features: - name: episode_title dtype: string - name: season dtype: string - name: episode_number dtype: string - name: script_text dtype: string splits: - name: season_1 num_bytes: 14592 num_examples: 3 - name: season_2 num_bytes: 15372 num_examples: 4 - name: season_3 num_bytes: 38217 num_examples: 6 - name: season_4 num_bytes: 42340 num_examples: 6 - name: season_5 num_bytes: 21330 num_examples: 3 - name: season_6 num_bytes: 34089 num_examples: 5 - name: season_7 num_bytes: 69600 num_examples: 6 download_size: 173212 dataset_size: 235540 --- # "Black Mirror Scripts Scrapping with Jina" ```py import requests import os import time import re import logging from datasets import Dataset, DatasetDict from urllib.parse import urljoin # Set up logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler('scrape_black_mirror.log'), logging.StreamHandler() ] ) logger = logging.getLogger(__name__) # Define the episode structure for all seasons episodes = { "season_1": [ {"title": "The National Anthem", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s01e01", "episode_number": "s01e01"}, {"title": "15 Million Merits", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s01e02", "episode_number": "s01e02"}, {"title": "The Entire History of You", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s01e03", "episode_number": "s01e03"}, ], "season_2": [ {"title": "Be Right Back", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s02e01", "episode_number": "s02e01"}, {"title": "White Bear", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s02e02", "episode_number": "s02e02"}, {"title": "The Waldo Moment", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s02e03", "episode_number": "s02e03"}, {"title": "Christmas Special", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s02e90", "episode_number": "s02e90"}, ], "season_3": [ {"title": "Nosedive", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e01", "episode_number": "s03e01"}, {"title": "Playtest", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e02", "episode_number": "s03e02"}, {"title": "Shut Up and Dance", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e03", "episode_number": "s03e03"}, {"title": "San Junipero", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e04", "episode_number": "s03e04"}, {"title": "Men Against Fire", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e05", "episode_number": "s03e05"}, {"title": "Hated in the Nation", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e06", "episode_number": "s03e06"}, ], "season_4": [ {"title": "USS Callister", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e01", "episode_number": "s04e01"}, {"title": "ArkAngel", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e02", "episode_number": "s04e02"}, {"title": "Crocodile", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e03", "episode_number": "s04e03"}, {"title": "Hang the DJ", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e04", "episode_number": "s04e04"}, {"title": "Metalhead", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e05", "episode_number": "s04e05"}, {"title": "Black Museum", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e06", "episode_number": "s04e06"}, ], "season_5": [ {"title": "Striking Vipers", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s05e01", "episode_number": "s05e01"}, {"title": "Smithereens", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s05e02", "episode_number": "s05e02"}, {"title": "Rachel, Jack and Ashley Too", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s05e03", "episode_number": "s05e03"}, ], "season_6": [ {"title": "Joan Is Awful", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e01", "episode_number": "s06e01"}, {"title": "Loch Henry", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e02", "episode_number": "s06e02"}, {"title": "Beyond the Sea", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e03", "episode_number": "s06e03"}, {"title": "Mazey Day", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e04", "episode_number": "s06e04"}, {"title": "Demon 79", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e05", "episode_number": "s06e05"}, ], "season_7": [ {"title": "Common People", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e01", "episode_number": "s07e01"}, {"title": "BĂȘte Noire", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e02", "episode_number": "s07e02"}, {"title": "Hotel Reverie", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e03", "episode_number": "s07e03"}, {"title": "Plaything", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e04", "episode_number": "s07e04"}, {"title": "Eulogy", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e05", "episode_number": "s07e05"}, {"title": "USS Callister: Into Infinity", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e06", "episode_number": "s07e06"}, ], } # Jina AI Reader API prefix JINA_READER_PREFIX = "https://r.jina.ai/" # Function to clean script text def clean_script_text(text): logger.info("Cleaning script text") # Remove excessive newlines and whitespace text = re.sub(r'\n\s*\n+', '\n\n', text.strip()) # Remove Jina AI metadata or unwanted headers text = re.sub(r'^#.*?\n', '', text, flags=re.MULTILINE) # Remove any navigation or footer content (basic filter) text = re.sub(r'(\[Previous\].*?\[Next\])|(Springfield! Springfield!.*?$)', '', text, flags=re.DOTALL) return text # Function to scrape a single episode def scrape_episode(season, episode): title = episode["title"] url = episode["url"] episode_number = episode["episode_number"] jina_url = JINA_READER_PREFIX + url logger.info(f"Scraping {season}/{title} ({episode_number}) from {jina_url}") try: # Send request to Jina AI Reader response = requests.get(jina_url, timeout=10) response.raise_for_status() # Get and clean script text script_text = clean_script_text(response.text) logger.info(f"Successfully scraped {title} ({episode_number})") return { "episode_title": title, "season": season, "episode_number": episode_number, "script_text": script_text } except requests.exceptions.RequestException as e: logger.error(f"Failed to scrape {title} ({episode_number}): {e}") return None finally: # Rate limiting time.sleep(2) # Collect data for Hugging Face dataset dataset_dict = {} logger.info("Starting scraping process") for season, episode_list in episodes.items(): logger.info(f"Processing {season}") season_data = [] for episode in episode_list: result = scrape_episode(season, episode) if result: season_data.append(result) if season_data: # Create a Dataset for the season dataset_dict[season] = Dataset.from_list(season_data) logger.info(f"Created dataset split for {season} with {len(season_data)} episodes") else: logger.warning(f"No data collected for {season}") # Create Hugging Face DatasetDict if dataset_dict: hf_dataset = DatasetDict(dataset_dict) logger.info("Created Hugging Face DatasetDict") # Save dataset to disk output_dir = "/content/black_mirror_scripts_dataset" hf_dataset.save_to_disk(output_dir) logger.info(f"Saved dataset to {output_dir}") # Optional: Push to Hugging Face Hub (uncomment if desired) from huggingface_hub import login login(token="") hf_dataset.push_to_hub("Svngoku/black_mirror_scripts", private=True) logger.info("Pushed dataset to Hugging Face Hub") else: logger.error("No data collected for any season") logger.info("Scraping and dataset creation complete!") ```