- Data Pipeline
The pipeline automates the full process of collecting, processing, and uploading movie data into a vector search engine (Elasticsearch) and cloud storage (Google BigQuery). This pipeline ensures scalable and efficient ingestion of new movie data for recommendation.
- Workflow
-
TT Code Scraping
Uses Playwright to extract unique IMDbttcodes from genre-based search results. Simulates user scrolling and clicking "Show More" to dynamically load more movie IDs. -
Metadata & Synopsis Extraction
Asynchronously visits movie and plot pages usingaiohttp+BeautifulSoup, extracting: - Title, year, duration, rating, genres
-
Short synopsis, long synopsis, and user summaries
-
Data Cleaning
Removes rows with no narrative text and cleans extra characters, usernames, and unnecessary suffixes. -
Data Splitting
- Metadata File: structured movie info
-
Synopsis File: cleaned narrative texts, labeled by type (
short,long,summary) -
Cloud Upload
Metadata is uploaded to Google Drive via API, then ingested into Google BigQuery. -
Chunking & Embedding
Synopsis text is chunked (~250 words) and embedded with SBERT into dense vectors usingSentenceTransformer. -
Output Generation
Each chunk + vector is saved as a.jsonfile and archived into a ZIP for ingestion. -
ETL Trigger
The ZIP is moved toetl/data/jsons/, whererun_etl.shautomatically unzips and indexes all files into Elasticsearch.
- Main Pipeline
run_pipeline()
Executes the main pipeline for processing IMDb movie data. The pipeline consists of the following steps:
1. Scrape IMDb tt codes.
2. Scrape movie metadata and synopsis.
3. Split combined CSV into metadata and synopsis files.
4. Upload metadata to Google Drive.
5. Upload metadata from Google Drive to BigQuery.
6. Clean synopsis data.
7. Chunk and embed synopsis data.
8. Move the resulting ZIP file to the etl/data/jsons/ directory.
Source code in app/pipeline/main_pipeline.py
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 | |
- Submodules
extract_tt_codes(html)
async
Extracts IMDb tt codes from the provided HTML content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html |
str
|
The HTML content of the IMDb page. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
set |
Set[str]
|
A set of unique tt codes extracted from the page. |
Source code in app/pipeline/scrape_tt_codes.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
run_scraper()
Runs the IMDb tt code scraper.
This function initializes the asynchronous scraping process and ensures
that the scrape_imdb_movie_codes coroutine is executed.
Source code in app/pipeline/scrape_tt_codes.py
114 115 116 117 118 119 120 121 | |
scrape_imdb_movie_codes()
async
Scrapes IMDb movie tt codes by navigating through the IMDb page and extracting tt codes. The extracted tt codes are saved to a TSV file.
Steps
- Opens the IMDb URL using Playwright.
- Extracts tt codes from the current page.
- Saves new tt codes to a TSV file.
- Clicks the "Show More" button to load additional movies and repeats the process.
Raises:
| Type | Description |
|---|---|
Exception
|
If any error occurs during the scraping process. |
Source code in app/pipeline/scrape_tt_codes.py
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 | |
IMDbCrawler
An asynchronous web crawler for scraping IMDb movie metadata and synopses.
Attributes:
| Name | Type | Description |
|---|---|---|
output_dir |
Path
|
Directory to store output files. |
min_delay |
float
|
Minimum delay between requests (for rate limiting). |
max_delay |
float
|
Maximum delay between requests. |
max_retries |
int
|
Number of retry attempts for failed requests. |
concurrent_requests |
int
|
Maximum number of concurrent requests. |
headers |
dict
|
HTTP headers to mimic browser behavior. |
session |
ClientSession
|
Reusable HTTP session for requests. |
Source code in app/pipeline/extract_movie_data.py
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
crawl_movies(num_movies=None)
async
Orchestrates crawling multiple IMDb movies concurrently and writes results to CSV in batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_movies |
int
|
Maximum number of movies to crawl. Defaults to all. |
None
|
Source code in app/pipeline/extract_movie_data.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
extract_movie_data(tconst)
async
Extracts full metadata and synopsis info from IMDb for a single title.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tconst |
str
|
IMDb title ID. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, Union[str, List[str], None]]
|
Extracted movie data. |
Source code in app/pipeline/extract_movie_data.py
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 | |
get_movies_from_tsv(tsv_file=TSV_FILENAME)
async
Reads movie IDs from a TSV file and filters out already processed IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tsv_file |
str
|
Path to the input TSV file. |
TSV_FILENAME
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: List of IMDb movie IDs to process. |
Source code in app/pipeline/extract_movie_data.py
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 | |
init_session()
async
Initializes the aiohttp session with SSL context.
Source code in app/pipeline/extract_movie_data.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
record_processed_id(tconst)
async
Records a processed IMDb ID into a TSV log with timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tconst |
str
|
IMDb title ID. |
required |
Source code in app/pipeline/extract_movie_data.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
run_crawler(output_dir='imdb_data', tsv_file=None, num_movies=0)
async
Entrypoint to run the IMDbCrawler with optional input and batch limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir |
str
|
Output directory path. |
'imdb_data'
|
tsv_file |
str
|
Path to custom TSV file of movie IDs. |
None
|
num_movies |
int
|
Number of movies to crawl (0 = all). |
0
|
Source code in app/pipeline/extract_movie_data.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |
clean_romance_synopsis(input_csv=None, output_csv=None)
Cleans a CSV file containing romance movie synopses by removing rows with missing data and processing the summaries column. The cleaned data is saved to an Excel file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_csv |
str
|
Path to the input CSV file containing the full movie data. Default is "imdb_data/romance_newbatch.csv". |
None
|
output_csv |
str
|
Path to the output CSV file where the cleaned data will be saved. Default is "imdb_data/romance_full_cleaned.csv". |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in app/pipeline/clean_synopsis.py
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 | |
split_movie_xlsx(input_csv=None, output_dir='imdb_data')
Splits an XLSX file containing movie data into two separate files: one for metadata and another for synopsis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_csv |
str
|
Path to the input CSV file containing movie data. |
None
|
output_dir |
str
|
Directory where the output files will be saved. Defaults to "imdb_data". |
'imdb_data'
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A tuple containing the paths to the metadata and synopsis files. (metadata_path, synopsis_path) |
Source code in app/pipeline/split_movie_data.py
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 | |
upload_metadata_to_drive(file_path=None, file_name=None)
Uploads a metadata file to Google Drive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path |
str
|
The local path to the file to be uploaded. Default is "imdb_data/romance_metadata.xlsx". |
None
|
file_name |
str
|
The name of the file as it will appear in Google Drive. Default is "romance_metadata.xlsx". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The ID of the uploaded file in Google Drive. |
Source code in app/pipeline/drive_upload.py
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 | |
upload_single_file_to_bigquery(target_file_name=None, folder_id='1sJcfq2h5NCOO2DNz2RxeIgSq-HYhl9Kg', dataset_id='romance_dataset', table_name='full_data_table')
Uploads a single Excel file from Google Drive to a BigQuery table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_file_name |
str
|
The name of the target Excel file to search for in Google Drive. |
None
|
folder_id |
str
|
The ID of the Google Drive folder containing the file. |
'1sJcfq2h5NCOO2DNz2RxeIgSq-HYhl9Kg'
|
dataset_id |
str
|
The ID of the BigQuery dataset where the table resides. |
'romance_dataset'
|
table_name |
str
|
The name of the BigQuery table to upload data to. |
'full_data_table'
|
Returns:
| Type | Description |
|---|---|
Optional[None]
|
None |
Source code in app/pipeline/bigquery_upload.py
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 | |
run_chunk_and_embed_pipeline(input_excel=None, output_dir=None)
Processes an Excel file containing movie data, chunks text fields, embeds the chunks using a SentenceTransformer model, and saves the results as JSON files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_excel |
str
|
Path to the input Excel file containing movie data. Default is "imdb_data/romance_synopsis_cleaned.xlsx". |
None
|
output_dir |
str
|
Directory where the chunked JSON files will be saved. Default is "chunked_jsons/romance_chunks_json". |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in app/pipeline/chunk_and_embed.py
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 | |