-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload_Legacy.py
More file actions
79 lines (66 loc) · 2.69 KB
/
Download_Legacy.py
File metadata and controls
79 lines (66 loc) · 2.69 KB
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 os
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm # İlerleme çubuğu için (opsiyonel)
# Ayarlar
base_url = "https://ocw.mit.edu"
source_page = "https://ocw.mit.edu/courses/6-100l-introduction-to-cs-and-programming-using-python-fall-2022/resources/lecture-videos/"
# İstediğin klasör yolu
output_dir = "courses/6-100l-introduction-to-cs-and-programming-using-python-fall-2022/"
def download_file(url, filename):
local_path = os.path.join(output_dir, filename)
# Dosya zaten varsa indirme (resume mantığı)
if os.path.exists(local_path):
print(f" [Atlandı] {filename} zaten mevcut.")
return
print(f" [İndiriliyor] {filename}...")
response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))
with open(local_path, 'wb') as file, tqdm(
desc=filename,
total=total_size,
unit='iB',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in response.iter_content(chunk_size=1024):
size = file.write(data)
bar.update(size)
def main():
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print("Doğrudan video bağlantıları taranıyor...")
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(source_page, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 'resource-thumbnail' class'ına sahip ve href'i .mp4 ile biten linkleri bul
video_links = []
for a in soup.find_all('a', class_='resource-thumbnail', href=True):
href = a['href']
if href.endswith('.mp4'):
full_url = base_url + href
# Dosya adını URL'den çıkar
filename = href.split('/')[-1]
video_links.append((full_url, filename))
if not video_links:
print("Hata: Video bağlantısı bulunamadı! HTML yapısı değişmiş olabilir.")
return
print(f"{len(video_links)} adet direkt video dosyası bulundu.\n")
for url, name in video_links:
try:
download_file(url, name)
except Exception as e:
print(f" [Hata] {name} indirilirken sorun çıktı: {e}")
if __name__ == "__main__":
# tqdm yüklü değilse hata vermemesi için basit bir kontrol
try:
from tqdm import tqdm
except ImportError:
print("Not: 'pip install tqdm' yazarak görsel ilerleme çubuğunu ekleyebilirsiniz.")
# Basit bir bar taklidi
class tqdm:
def __init__(self, *args, **kwargs): pass
def update(self, *args, **kwargs): pass
def __enter__(self): return self
def __exit__(self, *args): pass
main()