|
@@ -0,0 +1,51 @@
|
|
|
+import requests
|
|
|
+import config
|
|
|
+
|
|
|
+
|
|
|
+class yt():
|
|
|
+ def get_html_to_json(self, path):
|
|
|
+ api_url = path
|
|
|
+ r = requests.get(api_url)
|
|
|
+ if r.status_code == requests.codes.ok:
|
|
|
+ data = r.json()
|
|
|
+ else:
|
|
|
+ data = None
|
|
|
+ return data
|
|
|
+
|
|
|
+
|
|
|
+googleApi = config.googleApi
|
|
|
+path = "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics" \
|
|
|
+ "&id=f3goszxHt4k&key="+googleApi
|
|
|
+item = yt()
|
|
|
+data = item.get_html_to_json(path)
|
|
|
+
|
|
|
+
|
|
|
+list1 = data.get("items", "not found list1")
|
|
|
+for i in list1:
|
|
|
+ snippet = i.get("snippet", "not found snippet")
|
|
|
+ title = snippet.get('title', "not found title")
|
|
|
+ content = snippet.get('description', "not found description")
|
|
|
+
|
|
|
+
|
|
|
+list2 = data.get("items", "not found list2")
|
|
|
+for i in list2:
|
|
|
+ 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")
|
|
|
+
|
|
|
+column = '"'
|
|
|
+breaks = ","
|
|
|
+length = ""
|
|
|
+share = ""
|
|
|
+next = "\n"
|
|
|
+
|
|
|
+values = f'"{title}", "{content}", "{likes}", "{views}", "{comments}", ' \
|
|
|
+ f'"{length}","{share}"{next}'
|
|
|
+
|
|
|
+# csv save
|
|
|
+with open('movie_yt.csv', 'w', encoding="utf-8") as file:
|
|
|
+ file.write('"title","content","likes","views","comments",'
|
|
|
+ '"length","share"'+next)
|
|
|
+ file.write(values+next)
|
|
|
+file.close()
|