File size: 1,541 Bytes
19759e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)