curate-me-a-playlist / src /data /get_yt_links.py
berkaygkv54's picture
first push
19759e2
from youtubesearchpython import VideosSearch
import json
import time
from src.config.configs import ProjectPaths
from tqdm import tqdm
def read_json_data():
with open(ProjectPaths.DATA_DIR.joinpath("json", "saved_tracks.json"), "r") as rd:
data = json.load(rd)
return data
def get_track_link(artist_name, track_name):
search_result = VideosSearch(f'{artist_name} - {track_name}', limit=1)
result = search_result.result()["result"][0]
data = {
"artist_name": artist_name,
"track_name": track_name,
"duration": result.get("duration"),
"published_time": result.get("publishedTime"),
"title": result.get("title"),
"view_count": result.get("viewCount").get("text"),
"link": result.get("link")
}
return data
def save_youtube_data(data):
with open(ProjectPaths.DATA_DIR.joinpath("json", "youtube_data.json"), "w") as wr:
json.dump(data, wr, indent=4)
def collect_youtube_links():
data = read_json_data()
youtube_data = []
for track_data in tqdm(data):
yt_data = get_track_link(track_data["artist"], track_data["track"])
youtube_data.append(yt_data)
time.sleep(0.2)
save_youtube_data(youtube_data)
if __name__ == "__main__":
data = read_json_data()
youtube_data = []
for track_data in tqdm(data):
yt_data = get_track_link(track_data["artist"], track_data["track"])
youtube_data.append(yt_data)
time.sleep(0.2)
pass
save_youtube_data(youtube_data)