File size: 2,418 Bytes
de84263
 
 
1936f1e
 
de84263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1936f1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import urllib.request
import os
import glob
import requests
from bs4 import BeautifulSoup


def download_video(date):
    # Delete any existing .mp4 files
    for mp4_file in glob.glob("*.mp4"):
        os.remove(mp4_file)

    year = date[:4]
    url = f"https://www.cdep.ro/u02/comisii/{year}/cp46_{date}.mp4"
    try:
        urllib.request.urlretrieve(url, f"video_{date}.mp4")
        print("Video downloaded successfully.")
        return f"video_{date}.mp4"
    except urllib.error.HTTPError as e:
        if e.code == 404:
            print("No video exists for the given date.")
        else:
            print(f"An error occurred while downloading the video: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")


def get_response(url):
    try:
        response = requests.get(url)
        response.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if e.response.status_code == 404:
            print("No video exists for the given date range.")
            return None
        else:
            print(f"An error occurred while getting the webpage: {e}")
            return None
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
        return None

    soup = BeautifulSoup(response.text, "html.parser")
    return soup


def download_video1(from_date, to_date):
    # Get the webpage
    url = f"https://www.riksdagen.se/sv/sok/?avd=webbtv&from={from_date}&tom={to_date}&doktyp=kam-vo"

    soup = get_response(url)
    # Find the download link
    try:
        video_page = [
            a["href"]
            for a in soup.find_all("a", href=True)
            if a.get("aria-label") and a["aria-label"].startswith("Beslut")
        ][0]
        # go to video_page and get all links
        soup = get_response(video_page)
        video_link = [
            a["href"]
            for a in soup.find_all("a", href=True)
            if a["href"].startswith("https://mhdownload.riksdagen.se")
        ][0]
        print(video_link)
    except IndexError:
        print("No video exists for the given date range.")
        return None

    # Download the video
    video_path = f"video_{from_date}_{to_date}.mp4"
    try:
        urllib.request.urlretrieve(video_link, video_path)
        return video_path
    except Exception as e:
        print(f"An error occurred while downloading the video: {e}")
        return None