100 lines
3.4 KiB
Python
Executable File
100 lines
3.4 KiB
Python
Executable File
|
|
import urllib.request
|
|
import json
|
|
import re
|
|
import ssl
|
|
import imdb_suche
|
|
import sys
|
|
|
|
|
|
def get_movie_details(imdb_url):
|
|
"""
|
|
Ruft eine IMDb-Filmseite auf und extrahiert den Regisseur und die Hauptdarsteller.
|
|
|
|
Args:
|
|
imdb_url (str): Die vollständige URL zur IMDb-Filmseite.
|
|
|
|
Returns:
|
|
dict: Ein Wörterbuch mit "director" und "actors" oder None bei einem Fehler.
|
|
"""
|
|
if not re.match(r"https://www.imdb.com/de/title/tt\d+/?", imdb_url):
|
|
print("Fehler: Ungültige IMDb-URL. Die URL sollte so aussehen: https://www.imdb.com/title/tt...")
|
|
return None
|
|
|
|
try:
|
|
# SSL-Kontext erstellen, um Zertifikatsüberprüfungsfehler zu umgehen
|
|
context = ssl._create_unverified_context()
|
|
|
|
# User-Agent setzen, um wie ein Browser auszusehen und Blockaden zu vermeiden
|
|
headers = {
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
|
|
|
|
req = urllib.request.Request(imdb_url, headers=headers)
|
|
|
|
with urllib.request.urlopen(req, context=context) as response:
|
|
html_content = response.read().decode('utf-8')
|
|
|
|
# Suchen Sie nach dem strukturierten Datenblock (JSON-LD), der Filminformationen enthält
|
|
match = re.search(
|
|
r'<script type="application/ld\+json">(.*?)</script>', html_content, re.DOTALL)
|
|
|
|
if not match:
|
|
print("Fehler: Konnte den JSON-LD-Datenblock auf der Seite nicht finden.")
|
|
return None
|
|
|
|
json_data = json.loads(match.group(1))
|
|
|
|
# Extrahieren des Regisseurs
|
|
director = [d.get('name') for d in json_data.get('director', [])] if isinstance(
|
|
json_data.get('director'), list) else [json_data.get('director', {}).get('name')]
|
|
|
|
# Extrahieren der Schauspieler
|
|
actors = [a.get('name') for a in json_data.get('actor', [])]
|
|
|
|
# Extrahieren des Erscheinungsjahres
|
|
release_year = json_data.get('datePublished', '').split('-')[0]
|
|
|
|
|
|
return {
|
|
"director": [d for d in director if d],
|
|
"actors": [a for a in actors if a],
|
|
"release_year": release_year
|
|
}
|
|
|
|
except urllib.error.HTTPError as e:
|
|
print(f"HTTP-Fehler beim Abrufen der URL: {e.code} {e.reason}")
|
|
return None
|
|
except Exception as e:
|
|
print(f"Ein Fehler ist aufgetreten: {e}")
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) > 1:
|
|
film_name = " ".join(sys.argv[1:])
|
|
url = imdb_suche.suche_film_url(film_name)
|
|
if url:
|
|
print(f"URL für '{film_name}' gefunden: {url}")
|
|
|
|
# Beispiel-URL für den Film "Inception"
|
|
# Du kannst jede andere IMDb-Film-URL hier einfügen
|
|
movie_url = url
|
|
|
|
print(f"Rufe Informationen für den Film ab: {movie_url}")
|
|
|
|
details = get_movie_details(movie_url)
|
|
|
|
if details:
|
|
print("\n--- Filminformationen ---")
|
|
print(f"Regisseur(e): {', '.join(details['director'])}")
|
|
# Zeigt die ersten 5 Schauspieler an
|
|
print(f"Hauptdarsteller: {', '.join(details['actors'][:5])}")
|
|
if details.get('release_year'):
|
|
print(f"Erscheinungsjahr: {details['release_year']}")
|
|
print("-------------------------")
|
|
else:
|
|
print(f"Konnte keinen Film mit dem Titel '{film_name}' finden.")
|
|
else:
|
|
print("Benutzung: python imdb_suche.py <film_titel>")
|