aboutsummaryrefslogtreecommitdiffstats
path: root/ytapi.py
blob: 582efcd432a7f9714e821a8514fb21359a36cd49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import json
import js2py
import os

if os.path.split(os.getcwd())[-1] == "onceaday":
    configpath = "../config.json"
else:
    configpath = "config.json"

with open(configpath, "r") as f:
    CONFIG = json.load(f)

ERROR_DICT =  {
        "title": "ERROR Video deleted?",
        "description": "ERROR Video deleted?",
        "channel": "ERROR Video deleted?",
        "subscribers": "ERROR Video deleted?",
        "videos": "ERROR Video deleted?",
        "channelViews": "ERROR Video deleted?",
        "channelThumb": "ERROR Video deleted?",
        "thumbnail": "ERROR Video deleted?",
        "length": "ERROR Video deleted?",
        "views": "ERROR Video deleted?",
        "likes": "ERROR Video deleted?",
        "comments": "ERROR Video deleted?"
    }

# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
#   https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = CONFIG["youtubeapi"]["developer_key"]
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'

#run JavaScript because I don't understand regular expressions so we can copy this bad boy from Stack Overflow
get_videoId_from_url = js2py.eval_js(r"""function $(url){
                            var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*?[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
                            return url.replace(re, '$1');
                        }""")

def _yt_time_to_norm(time):
    origtime = time
    if time == "ERROR Video deleted?":
        return time

    time = time[2:].replace("H", ":").replace("M", ":").replace("S", "")

    out = ""
    for i in time.split(":"):
        if len(i) == 1:
            out += "0" + i + ":"
        elif len(i) == 0:
            out += "00:"
        else:
            out += i + ":"

    return out[:-1]



#this would be better as a class but I can't be bothered so dictionary it is
def get_video_data(videoId):
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)


    #youTubeData = youtube.videos().list(part = "snippet", id = videoId).execute()["items"][0]["snippet"]

    #return {"title": youTubeData["title"], "description": youTubeData["description"], "tags": youTubeData["tags"]}

    try:
        youTubeData = youtube.videos().list(part = "snippet,contentDetails,statistics", id = videoId).execute()["items"][0]
    except IndexError:
        return ERROR_DICT

    snippet = youTubeData["snippet"]
    length = youTubeData["contentDetails"]["duration"]
    stats = youTubeData["statistics"]
    channelId = snippet["channelId"]

    channelData = youtube.channels().list(part = 'snippet,statistics', id = channelId).execute()["items"][0]

    return {
        "title": snippet["title"],
        "description": snippet["description"].replace("\n", "⤶"),
        "channel": channelData["snippet"]["title"],
        "subscribers": channelData["statistics"]["subscriberCount"],
        "videos": channelData["statistics"]["videoCount"],
        "channelViews": channelData["statistics"]["viewCount"],
        "channelThumb": channelData["snippet"]["thumbnails"]["high"]["url"],
        "thumbnail": snippet["thumbnails"]["high"]["url"],
        "length": _yt_time_to_norm(length),
        "views": stats["viewCount"],
        "likes": stats["likeCount"],
        "comments": stats["commentCount"]
    }


if __name__ == '__main__':
    try:
        print(get_video_data(get_videoId_from_url("https://www.youtube.com/watch?v=ZYqG31V4qtA")))
    except HttpError as e:
        print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content))