File size: 10,610 Bytes
6999c6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d61e9c0
6999c6f
 
d61e9c0
6999c6f
 
d61e9c0
6999c6f
 
d61e9c0
6999c6f
 
d61e9c0
6999c6f
 
d61e9c0
6999c6f
 
d61e9c0
6999c6f
d61e9c0
 
6999c6f
3e3edad
6999c6f
3e3edad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---
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!")

```