yt.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # sele
  2. import requests
  3. import config
  4. class yt():
  5. def __init__(self, api_key):
  6. self.base_url = "https://www.googleapis.com/youtube/v3/videos?part=" \
  7. "snippet,statistics&id="
  8. self.api_key = api_key
  9. def get_html_to_json(self, path):
  10. """組合 URL 後 GET 網頁並轉換成 JSON"""
  11. api_url = f"{self.base_url}{path}&key={self.api_key}"
  12. r = requests.get(api_url)
  13. if r.status_code == requests.codes.ok:
  14. data = r.json()
  15. else:
  16. data = None
  17. return data
  18. googleApi = config.googleApi
  19. urllist = ["f3goszxHt4k", "fi5Sdo1ngXM", "3xMEQLAlY2Q", "g7bGfn_ym2k",
  20. "5kwo6GJqZak", "ZS4hiZWWjxA", "rJZ7SJuMGe4", "qcUB4uTZZNM",
  21. "EojvnYcFP18", "U4ucS3wp3M4", "8qaM_HWNSLc", "xh5MMFksEWo",
  22. "qAe4EpqAJXk", "hlS7KF2Kc3Q", "6gH9ZF5nfQ8", "gGVA5gcx5PE",
  23. "QHeyNICIePM", "Eq_Nj2EVJRA", "Zsai9VxZ7oM", "7pcSaHg_V4c",
  24. "Ts7HDyZ9DoQ", "lsD_6YD1wqg", "qNvkdZ8yPgA", "ki4tmmiCYVs",
  25. "fx4fTBh9PFo", "WC_rYXVP2g8", "3aHCylX2Du0", "aH8T9bQhZjI",
  26. "0e7sGmBWCFM", "uD5dxKLBL0I", "KgHqcNulmHY", "OogrWr0yVAA",
  27. "g9Pwxd9FEpM", "36EvsfVlobE", "Ja6WRIvMaAM", "hETKVh3DIP0",
  28. "kLmrSdEHLkQ", "o3t1nTM2JXI", "1dy0phcIb1s", "zE3PxSg0GPo",
  29. "Qvf3uPhN4SY", "MwXwfDuZAN0", "G_M1tWGplkM", "bd6memqoLg8",
  30. "XOd2NCcJLz0", "i3p-5UYtpbE", "oADIpGnUjfY", "MDPXxc_4rsE",
  31. "aolWx-AiyMs", "fHNYihpq2b8", "HKcpzMnzZ5k", "5rCY7LYmxRs",
  32. "2dE-zGdR3pQ", "srKHanTS-A8", "EXw9opKnWF4", "8g1imn_pTv0",
  33. "5xbXmTrXObw", "_H9hNu2VzTE", "HQkAmut-OQw", "SmpaVb6f8pE",
  34. "L_U7UoSo3U8", "KHB80ywdsPE", "UwBYV6Y2SR4", "kCeJ9aDpE2A",
  35. "Owf1T6-w_1o", "1fwTV_WaccY", "2hnNuztqlbQ", "D0tO0E0J2uY",
  36. "xS9GiawCYI8", "2tF9i9fQIKs", "rC21xM6_xbE", "dTZNhRaKxug",
  37. "NW34lOX9pT0", "yK7KTnAeqqg", "lgiK6OPLU1A", "DJzPEGo720A",
  38. "wRf83pGUquE", "yJfHdJQ2aJE", "Q7q_ajxcGFA", "7GQ4LmahHJI",
  39. "zXi1JG5KmYI", "ebTeRrpZwRw", "5BsD16LtDcw", "gKaDBXZ6aDU"]
  40. item = yt(googleApi)
  41. # csv save
  42. column = '"'
  43. breaks = ","
  44. length = ""
  45. share = ""
  46. next = "\r"
  47. with open('movie_yt.csv', 'w', encoding="utf-8") as file:
  48. file.write('"title","content","likes","views","comments",'
  49. '"length","share"' + next)
  50. for path in urllist:
  51. data = item.get_html_to_json(path)
  52. snippet = data.get("items", "not found items")
  53. for i in snippet:
  54. snippet = i.get("snippet", "not found snippet")
  55. title = snippet.get('title', "not found title")
  56. content = snippet.get('description', "not found description")
  57. statistics = data.get("items", "not found items")
  58. for i in statistics:
  59. statistics = i.get("statistics", "not found statistics")
  60. views = statistics.get('viewCount', "not found viewCount")
  61. likes = statistics.get('likeCount', "not found likeCount")
  62. comments = statistics.get('commentCount', "not found commentCount")
  63. values = f'"{title}";"{content}";"{likes}";"{views}";"{comments}";' \
  64. f'"{length}";"{share}"{next}'
  65. file.writelines(values)
  66. file.close()