# sele
import requests
import config


class yt():
    def __init__(self, api_key):
        self.base_url = "https://www.googleapis.com/youtube/v3/videos?part=" \
                        "snippet,statistics&id="
        self.api_key = api_key

    def get_html_to_json(self, path):
        """組合 URL 後 GET 網頁並轉換成 JSON"""
        api_url = f"{self.base_url}{path}&key={self.api_key}"
        r = requests.get(api_url)
        if r.status_code == requests.codes.ok:
            data = r.json()
        else:
            data = None
        return data


googleApi = config.googleApi
urllist = ["f3goszxHt4k", "fi5Sdo1ngXM", "3xMEQLAlY2Q", "g7bGfn_ym2k",
           "5kwo6GJqZak", "ZS4hiZWWjxA", "rJZ7SJuMGe4", "qcUB4uTZZNM",
           "EojvnYcFP18", "U4ucS3wp3M4", "8qaM_HWNSLc", "xh5MMFksEWo",
           "qAe4EpqAJXk", "hlS7KF2Kc3Q", "6gH9ZF5nfQ8", "gGVA5gcx5PE",
           "QHeyNICIePM", "Eq_Nj2EVJRA", "Zsai9VxZ7oM", "7pcSaHg_V4c",
           "Ts7HDyZ9DoQ", "lsD_6YD1wqg", "qNvkdZ8yPgA", "ki4tmmiCYVs",
           "fx4fTBh9PFo", "WC_rYXVP2g8", "3aHCylX2Du0", "aH8T9bQhZjI",
           "0e7sGmBWCFM", "uD5dxKLBL0I", "KgHqcNulmHY", "OogrWr0yVAA",
           "g9Pwxd9FEpM", "36EvsfVlobE", "Ja6WRIvMaAM", "hETKVh3DIP0",
           "kLmrSdEHLkQ", "o3t1nTM2JXI", "1dy0phcIb1s", "zE3PxSg0GPo",
           "Qvf3uPhN4SY", "MwXwfDuZAN0", "G_M1tWGplkM", "bd6memqoLg8",
           "XOd2NCcJLz0", "i3p-5UYtpbE", "oADIpGnUjfY", "MDPXxc_4rsE",
           "aolWx-AiyMs", "fHNYihpq2b8", "HKcpzMnzZ5k", "5rCY7LYmxRs",
           "2dE-zGdR3pQ", "srKHanTS-A8", "EXw9opKnWF4", "8g1imn_pTv0",
           "5xbXmTrXObw", "_H9hNu2VzTE", "HQkAmut-OQw", "SmpaVb6f8pE",
           "L_U7UoSo3U8", "KHB80ywdsPE", "UwBYV6Y2SR4", "kCeJ9aDpE2A",
           "Owf1T6-w_1o", "1fwTV_WaccY", "2hnNuztqlbQ", "D0tO0E0J2uY",
           "xS9GiawCYI8", "2tF9i9fQIKs", "rC21xM6_xbE", "dTZNhRaKxug",
           "NW34lOX9pT0", "yK7KTnAeqqg", "lgiK6OPLU1A", "DJzPEGo720A",
           "wRf83pGUquE", "yJfHdJQ2aJE", "Q7q_ajxcGFA", "7GQ4LmahHJI",
           "zXi1JG5KmYI", "ebTeRrpZwRw", "5BsD16LtDcw", "gKaDBXZ6aDU"]
item = yt(googleApi)


# csv save
column = '"'
breaks = ","
length = ""
share = ""
next = "\r"
with open('movie_yt.csv', 'w', encoding="utf-8") as file:
    file.write('"title","content","likes","views","comments",'
               '"length","share"' + next)
    for path in urllist:

        data = item.get_html_to_json(path)

        snippet = data.get("items", "not found items")
        for i in snippet:
            snippet = i.get("snippet", "not found snippet")
            title = snippet.get('title', "not found title")
            content = snippet.get('description', "not found description")

        statistics = data.get("items", "not found items")
        for i in statistics:
            statistics = i.get("statistics", "not found statistics")
            views = statistics.get('viewCount', "not found viewCount")
            likes = statistics.get('likeCount', "not found likeCount")
            comments = statistics.get('commentCount', "not found commentCount")

        values = f'"{title}";"{content}";"{likes}";"{views}";"{comments}";' \
                 f'"{length}";"{share}"{next}'

        file.writelines(values)

file.close()
