|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import json |
| 4 | + |
| 5 | + |
| 6 | +class Video: |
| 7 | + |
| 8 | + |
| 9 | + def __init__(self, video_url): |
| 10 | + self.video_url = video_url |
| 11 | + |
| 12 | + def getDetails(self): |
| 13 | + |
| 14 | + url = self.video_url |
| 15 | + try: |
| 16 | + res = requests.get(url) |
| 17 | + soup = BeautifulSoup(res.text, "html.parser") |
| 18 | + video_data = {"video_data": []} |
| 19 | + |
| 20 | + scripts = soup.find_all("script") |
| 21 | + req_script = scripts[44].text.strip() |
| 22 | + script = req_script[20:-1] |
| 23 | + data = json.loads(script) |
| 24 | + |
| 25 | + base = data["contents"]["twoColumnWatchNextResults"]["results"]["results"][ |
| 26 | + "contents" |
| 27 | + ] |
| 28 | + |
| 29 | + first = base[0]["videoPrimaryInfoRenderer"] |
| 30 | + title = ( |
| 31 | + first["title"]["runs"][0]["text"] |
| 32 | + .strip() |
| 33 | + .encode("ascii", "ignore") |
| 34 | + .decode() |
| 35 | + ) |
| 36 | + views = first["viewCount"]["videoViewCountRenderer"]["viewCount"][ |
| 37 | + "simpleText" |
| 38 | + ] |
| 39 | + date = first["dateText"]["simpleText"] |
| 40 | + |
| 41 | + channel_data = base[1]["videoSecondaryInfoRenderer"]["owner"][ |
| 42 | + "videoOwnerRenderer" |
| 43 | + ] |
| 44 | + avatar = channel_data["thumbnail"]["thumbnails"][2]["url"] |
| 45 | + name = channel_data["title"]["runs"][0]["text"] |
| 46 | + channel_url = channel_data["title"]["runs"][0]["navigationEndpoint"][ |
| 47 | + "commandMetadata" |
| 48 | + ]["webCommandMetadata"]["url"] |
| 49 | + subs = channel_data["subscriberCountText"]["accessibility"][ |
| 50 | + "accessibilityData" |
| 51 | + ]["label"] |
| 52 | + |
| 53 | + desc = ( |
| 54 | + base[1]["videoSecondaryInfoRenderer"]["attributedDescription"][ |
| 55 | + "content" |
| 56 | + ] |
| 57 | + .strip() |
| 58 | + .encode("ascii", "ignore") |
| 59 | + .decode() |
| 60 | + ) |
| 61 | + comment_count = base[2]["itemSectionRenderer"]["contents"][0][ |
| 62 | + "commentsEntryPointHeaderRenderer" |
| 63 | + ]["commentCount"]["simpleText"] |
| 64 | + |
| 65 | + video_data["video_data"].append( |
| 66 | + { |
| 67 | + "title": title, |
| 68 | + "description": desc[:200] + "...", |
| 69 | + "views_count": views, |
| 70 | + "upload_date": date, |
| 71 | + "comment_count": comment_count, |
| 72 | + "channel_name": name, |
| 73 | + "channel_avatar": avatar, |
| 74 | + "subscriber_count": subs, |
| 75 | + "channel_url": "https://youtube.com" + channel_url, |
| 76 | + } |
| 77 | + ) |
| 78 | + return video_data |
| 79 | + except: |
| 80 | + return None |
0 commit comments