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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
download_size: 173212
|
| 52 |
dataset_size: 235540
|
| 53 |
---
|
| 54 |
+
# "Black Mirror Scripts Scrapping with Jina"
|
| 55 |
|
| 56 |
+
```py
|
| 57 |
+
|
| 58 |
+
import requests
|
| 59 |
+
import os
|
| 60 |
+
import time
|
| 61 |
+
import re
|
| 62 |
+
import logging
|
| 63 |
+
from datasets import Dataset, DatasetDict
|
| 64 |
+
from urllib.parse import urljoin
|
| 65 |
+
|
| 66 |
+
# Set up logging
|
| 67 |
+
logging.basicConfig(
|
| 68 |
+
level=logging.INFO,
|
| 69 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 70 |
+
handlers=[
|
| 71 |
+
logging.FileHandler('scrape_black_mirror.log'),
|
| 72 |
+
logging.StreamHandler()
|
| 73 |
+
]
|
| 74 |
+
)
|
| 75 |
+
logger = logging.getLogger(__name__)
|
| 76 |
+
|
| 77 |
+
# Define the episode structure for all seasons
|
| 78 |
+
episodes = {
|
| 79 |
+
"season_1": [
|
| 80 |
+
{"title": "The National Anthem", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s01e01", "episode_number": "s01e01"},
|
| 81 |
+
{"title": "15 Million Merits", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s01e02", "episode_number": "s01e02"},
|
| 82 |
+
{"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"},
|
| 83 |
+
],
|
| 84 |
+
"season_2": [
|
| 85 |
+
{"title": "Be Right Back", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s02e01", "episode_number": "s02e01"},
|
| 86 |
+
{"title": "White Bear", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s02e02", "episode_number": "s02e02"},
|
| 87 |
+
{"title": "The Waldo Moment", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s02e03", "episode_number": "s02e03"},
|
| 88 |
+
{"title": "Christmas Special", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s02e90", "episode_number": "s02e90"},
|
| 89 |
+
],
|
| 90 |
+
"season_3": [
|
| 91 |
+
{"title": "Nosedive", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e01", "episode_number": "s03e01"},
|
| 92 |
+
{"title": "Playtest", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e02", "episode_number": "s03e02"},
|
| 93 |
+
{"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"},
|
| 94 |
+
{"title": "San Junipero", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e04", "episode_number": "s03e04"},
|
| 95 |
+
{"title": "Men Against Fire", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s03e05", "episode_number": "s03e05"},
|
| 96 |
+
{"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"},
|
| 97 |
+
],
|
| 98 |
+
"season_4": [
|
| 99 |
+
{"title": "USS Callister", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e01", "episode_number": "s04e01"},
|
| 100 |
+
{"title": "ArkAngel", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e02", "episode_number": "s04e02"},
|
| 101 |
+
{"title": "Crocodile", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e03", "episode_number": "s04e03"},
|
| 102 |
+
{"title": "Hang the DJ", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e04", "episode_number": "s04e04"},
|
| 103 |
+
{"title": "Metalhead", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e05", "episode_number": "s04e05"},
|
| 104 |
+
{"title": "Black Museum", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s04e06", "episode_number": "s04e06"},
|
| 105 |
+
],
|
| 106 |
+
"season_5": [
|
| 107 |
+
{"title": "Striking Vipers", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s05e01", "episode_number": "s05e01"},
|
| 108 |
+
{"title": "Smithereens", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s05e02", "episode_number": "s05e02"},
|
| 109 |
+
{"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"},
|
| 110 |
+
],
|
| 111 |
+
"season_6": [
|
| 112 |
+
{"title": "Joan Is Awful", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e01", "episode_number": "s06e01"},
|
| 113 |
+
{"title": "Loch Henry", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e02", "episode_number": "s06e02"},
|
| 114 |
+
{"title": "Beyond the Sea", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e03", "episode_number": "s06e03"},
|
| 115 |
+
{"title": "Mazey Day", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e04", "episode_number": "s06e04"},
|
| 116 |
+
{"title": "Demon 79", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s06e05", "episode_number": "s06e05"},
|
| 117 |
+
],
|
| 118 |
+
"season_7": [
|
| 119 |
+
{"title": "Common People", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e01", "episode_number": "s07e01"},
|
| 120 |
+
{"title": "Bête Noire", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e02", "episode_number": "s07e02"},
|
| 121 |
+
{"title": "Hotel Reverie", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e03", "episode_number": "s07e03"},
|
| 122 |
+
{"title": "Plaything", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e04", "episode_number": "s07e04"},
|
| 123 |
+
{"title": "Eulogy", "url": "https://www.springfieldspringfield.co.uk/view_episode_scripts.php?tv-show=black-mirror-2011&episode=s07e05", "episode_number": "s07e05"},
|
| 124 |
+
{"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"},
|
| 125 |
+
],
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
# Jina AI Reader API prefix
|
| 129 |
+
JINA_READER_PREFIX = "https://r.jina.ai/"
|
| 130 |
+
|
| 131 |
+
# Function to clean script text
|
| 132 |
+
def clean_script_text(text):
|
| 133 |
+
logger.info("Cleaning script text")
|
| 134 |
+
# Remove excessive newlines and whitespace
|
| 135 |
+
text = re.sub(r'\n\s*\n+', '\n\n', text.strip())
|
| 136 |
+
# Remove Jina AI metadata or unwanted headers
|
| 137 |
+
text = re.sub(r'^#.*?\n', '', text, flags=re.MULTILINE)
|
| 138 |
+
# Remove any navigation or footer content (basic filter)
|
| 139 |
+
text = re.sub(r'(\[Previous\].*?\[Next\])|(Springfield! Springfield!.*?$)', '', text, flags=re.DOTALL)
|
| 140 |
+
return text
|
| 141 |
+
|
| 142 |
+
# Function to scrape a single episode
|
| 143 |
+
def scrape_episode(season, episode):
|
| 144 |
+
title = episode["title"]
|
| 145 |
+
url = episode["url"]
|
| 146 |
+
episode_number = episode["episode_number"]
|
| 147 |
+
jina_url = JINA_READER_PREFIX + url
|
| 148 |
+
|
| 149 |
+
logger.info(f"Scraping {season}/{title} ({episode_number}) from {jina_url}")
|
| 150 |
+
|
| 151 |
+
try:
|
| 152 |
+
# Send request to Jina AI Reader
|
| 153 |
+
response = requests.get(jina_url, timeout=10)
|
| 154 |
+
response.raise_for_status()
|
| 155 |
+
|
| 156 |
+
# Get and clean script text
|
| 157 |
+
script_text = clean_script_text(response.text)
|
| 158 |
+
|
| 159 |
+
logger.info(f"Successfully scraped {title} ({episode_number})")
|
| 160 |
+
|
| 161 |
+
return {
|
| 162 |
+
"episode_title": title,
|
| 163 |
+
"season": season,
|
| 164 |
+
"episode_number": episode_number,
|
| 165 |
+
"script_text": script_text
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
except requests.exceptions.RequestException as e:
|
| 169 |
+
logger.error(f"Failed to scrape {title} ({episode_number}): {e}")
|
| 170 |
+
return None
|
| 171 |
+
|
| 172 |
+
finally:
|
| 173 |
+
# Rate limiting
|
| 174 |
+
time.sleep(2)
|
| 175 |
+
|
| 176 |
+
# Collect data for Hugging Face dataset
|
| 177 |
+
dataset_dict = {}
|
| 178 |
+
logger.info("Starting scraping process")
|
| 179 |
+
|
| 180 |
+
for season, episode_list in episodes.items():
|
| 181 |
+
logger.info(f"Processing {season}")
|
| 182 |
+
season_data = []
|
| 183 |
+
|
| 184 |
+
for episode in episode_list:
|
| 185 |
+
result = scrape_episode(season, episode)
|
| 186 |
+
if result:
|
| 187 |
+
season_data.append(result)
|
| 188 |
+
|
| 189 |
+
if season_data:
|
| 190 |
+
# Create a Dataset for the season
|
| 191 |
+
dataset_dict[season] = Dataset.from_list(season_data)
|
| 192 |
+
logger.info(f"Created dataset split for {season} with {len(season_data)} episodes")
|
| 193 |
+
else:
|
| 194 |
+
logger.warning(f"No data collected for {season}")
|
| 195 |
+
|
| 196 |
+
# Create Hugging Face DatasetDict
|
| 197 |
+
if dataset_dict:
|
| 198 |
+
hf_dataset = DatasetDict(dataset_dict)
|
| 199 |
+
logger.info("Created Hugging Face DatasetDict")
|
| 200 |
+
|
| 201 |
+
# Save dataset to disk
|
| 202 |
+
output_dir = "/content/black_mirror_scripts_dataset"
|
| 203 |
+
hf_dataset.save_to_disk(output_dir)
|
| 204 |
+
logger.info(f"Saved dataset to {output_dir}")
|
| 205 |
+
|
| 206 |
+
# Optional: Push to Hugging Face Hub (uncomment if desired)
|
| 207 |
+
from huggingface_hub import login
|
| 208 |
+
login(token="")
|
| 209 |
+
hf_dataset.push_to_hub("Svngoku/black_mirror_scripts", private=True)
|
| 210 |
+
logger.info("Pushed dataset to Hugging Face Hub")
|
| 211 |
+
else:
|
| 212 |
+
logger.error("No data collected for any season")
|
| 213 |
+
|
| 214 |
+
logger.info("Scraping and dataset creation complete!")
|
| 215 |
+
|
| 216 |
+
```
|