1 line
2.5 KiB
JSON
1 line
2.5 KiB
JSON
{"repo": "arj7192/yc-ml", "files": {"yc.py": {"6c80087e63cc44b1919f2cea801418080fd1a1ab": {"t": 1785674587.2470481, "n": 2222, "h": "721c6fae7e5afbff", "c": "#!/usr/bin/python\n\n# Usage:\n# python scraper.py --videoid='<video_id>'\n\nfrom apiclient.errors import HttpError\n# import argparser\nfrom apiclient.discovery import build\n\nYOUTUBE_API_SERVICE_NAME = \"youtube\"\nYOUTUBE_API_VERSION = \"v3\"\nDEVELOPER_KEY = 'AIzaSyDyq3nr2KjbFME9EeNpHy97AX87vk75H_Y'\n\n\ndef get_comment_threads(youtube, video_id, comments):\n threads = []\n results = youtube.commentThreads().list(\n part=\"snippet\",\n videoId=video_id,\n textFormat=\"plainText\",\n ).execute()\n\n # Get the first set of comments\n for item in results[\"items\"]:\n threads.append(item)\n comment = item[\"snippet\"][\"topLevelComment\"]\n text = comment[\"snippet\"][\"textDisplay\"]\n comments.append(text)\n\n # Keep getting comments from the following pages\n while (\"nextPageToken\" in results):\n results = youtube.commentThreads().list(\n part=\"snippet\",\n videoId=video_id,\n pageToken=results[\"nextPageToken\"],\n textFormat=\"plainText\",\n ).execute()\n for item in results[\"items\"]:\n threads.append(item)\n comment = item[\"snippet\"][\"topLevelComment\"]\n text = comment[\"snippet\"][\"textDisplay\"]\n comments.append(text)\n\n print(\"Total threads: %d\" % len(threads))\n return threads\n\n\ndef get_comments(youtube, parent_id, comments):\n results = youtube.comments().list(\n part=\"snippet\",\n parentId=parent_id,\n textFormat=\"plainText\"\n ).execute()\n\n for item in results[\"items\"]:\n text = item[\"snippet\"][\"textDisplay\"]\n comments.append(text)\n\n return results[\"items\"]\n\n\nif __name__ == \"__main__\":\n argparser.add_argument(\"--videoid\", help=\"Required; ID for video for which the comment will be inserted.\")\n args = argparser.parse_args()\n youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)\n\n comments = []\n\n try:\n video_comment_threads = get_comment_threads(youtube, args.videoid, comments)\n for thread in video_comment_threads:\n get_comments(youtube, thread[\"id\"], comments)\n except(HttpError, e):\n print(\"An HTTP error %d occurred:\\n%s\" % (e.resp.status, e.content))\n"}}}} |