aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2019-01-07 20:13:20 +0000
committerjwansek <eddie.atten.ea29@gmail.com>2019-01-07 20:13:20 +0000
commitcaa23c670d168cc72f8cd332fc5792780c84e796 (patch)
tree42d97fc8803d86a0d66cf8ec692bb8022d753140
parent4a7c93c493af152884cb34ba34f6785b965e196b (diff)
parent3dc4ac293ad0959a14013eb0f5422db845bbfd42 (diff)
downloadSmallYTChannelBot-caa23c670d168cc72f8cd332fc5792780c84e796.tar.gz
SmallYTChannelBot-caa23c670d168cc72f8cd332fc5792780c84e796.zip
updated database
-rw-r--r--SmallYTChannelBotSubmissions.py48
-rw-r--r--onceaday.py2
-rw-r--r--test_ytapi.py69
-rw-r--r--ytapi.py90
4 files changed, 208 insertions, 1 deletions
diff --git a/SmallYTChannelBotSubmissions.py b/SmallYTChannelBotSubmissions.py
index 712f5b0..aa3943d 100644
--- a/SmallYTChannelBotSubmissions.py
+++ b/SmallYTChannelBotSubmissions.py
@@ -248,6 +248,54 @@ def main():
will be rewarded 1λ if you do so. For more information, read the [FAQ](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)""" % (score - 3, str(submission.author))
db.change_lambda(str(submission.author), -3)
+ ytid = ytapi.get_videoId_from_url(submission.url)
+ if "/" not in ytid:
+ ytdata = ytapi.get_video_data(ytid)
+
+ text += """
+\n\n\n##Video data:
+
+Field|Data
+:-|:-
+Title|%s
+Thumbnail|[Link](%s)
+Views|%s
+Length|%s
+Likes/Dislikes|%s/%s
+Comments|%s
+Description|%s
+
+##Channel Data:
+
+Field|Data
+:-|:-
+Name|%s
+Thumbnail|[Link](%s)
+Subscribers|%s
+Videos|%s
+Views|%s
+ """ % (
+ ytdata["title"],
+ ytdata["thumbnail"],
+ ytdata["views"],
+ ytdata["length"],
+ ytdata["likes"],
+ ytdata["dislikes"],
+ ytdata["comments"],
+ ytdata["description"],
+ ytdata["channel"],
+ ytdata["channelThumb"],
+ ytdata["subscribers"],
+ ytdata["videos"],
+ ytdata["channelViews"]
+ )
+
+ curflair = submission.link_flair_text
+ if str(curflair) != "None":
+ submission.mod.flair(" %s | %s | :youtube: %s" % (curflair, ytdata["length"], ytdata["channel"]))
+ else:
+ submission.mod.flair("%s | :youtube: %s" % (ytdata["length"], ytdata["channel"]))
+
update_users_flair(submission)
reply = submission.reply(text + tail)
reply.mod.distinguish(sticky = True)
diff --git a/onceaday.py b/onceaday.py
index a84b0d9..5bebc54 100644
--- a/onceaday.py
+++ b/onceaday.py
@@ -5,5 +5,5 @@ SECONDS_IN_DAY = 25 * 60 * 60
while True:
SmallYTChannelBotSubmissions.every_day()
- print("Called @ %s" % SmallYTChannelBotSubmissions.get_time)
+ print("Called @ %s" % SmallYTChannelBotSubmissions.get_time())
sleep(SECONDS_IN_DAY)
diff --git a/test_ytapi.py b/test_ytapi.py
new file mode 100644
index 0000000..44f439f
--- /dev/null
+++ b/test_ytapi.py
@@ -0,0 +1,69 @@
+import praw
+import database
+import login
+import ytapi
+
+reddit = login.REDDIT
+
+subreddit = reddit.subreddit("jwnskanzkwktest")
+
+tail = "\n\n\n ^/u/SmallYTChannelBot ^*made* ^*by* ^/u/jwnskanzkwk. ^*PM* ^*for* ^*bug* ^*reports.* ^*For* ^*more* ^*information,* ^*read* ^*the* ^[FAQ.](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)"
+
+submission_stream = subreddit.stream.submissions(pause_after=-1)
+while True:
+
+ for submission in submission_stream:
+ if submission is not None:
+
+ text = "Thank you for submitting..."
+ ytid = ytapi.get_videoId_from_url(submission.url)
+ if "/" not in ytid:
+ ytdata = ytapi.get_video_data(ytid)
+
+ text += """
+\n\n\n##Video data:
+
+Field|Data
+:-|:-
+Title|%s
+Thumbnail|[Link](%s)
+Views|%s
+Length|%s
+Likes/Dislikes|%s/%s
+Comments|%s
+Description|%s
+
+##Channel Data:
+
+Field|Data
+:-|:-
+Name|%s
+Thumbnail|[Link](%s)
+Subscribers|%s
+Videos|%s
+Views|%s
+
+ """ % (
+ ytdata["title"],
+ ytdata["thumbnail"],
+ ytdata["views"],
+ ytdata["length"],
+ ytdata["likes"],
+ ytdata["dislikes"],
+ ytdata["comments"],
+ ytdata["description"],
+ ytdata["channel"],
+ ytdata["channelThumb"],
+ ytdata["subscribers"],
+ ytdata["videos"],
+ ytdata["channelViews"]
+ )
+
+ curflair = submission.link_flair_text
+ if str(curflair) != "None":
+ submission.mod.flair(" %s | %s | :youtube: %s" % (curflair, ytdata["length"], ytdata["channel"]))
+ else:
+ submission.mod.flair("%s | :youtube: %s" % (ytdata["length"], ytdata["channel"]))
+
+ reply = submission.reply(text + tail)
+ reply.mod.distinguish(sticky = True)
diff --git a/ytapi.py b/ytapi.py
new file mode 100644
index 0000000..b472b7e
--- /dev/null
+++ b/ytapi.py
@@ -0,0 +1,90 @@
+from googleapiclient.discovery import build
+from googleapiclient.errors import HttpError
+import js2py
+
+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?",
+ "dislikes": "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 = 'AIzaSyBQsuU5GgCTZdFi7cBmPQHWZwIa545zLUE'
+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):
+ if time == "ERROR Video deleted?":
+ return time
+
+ time = time.replace("M", ":")[2:].replace("S", "")
+
+ s = time.split(":")
+ if len(s) > 1:
+ if len(s[1]) < 2:
+ time = s[0] + ":" + s[1] + "0"
+
+ return time
+
+#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"],
+ "dislikes": stats["dislikeCount"],
+ "comments": stats["commentCount"]
+ }
+
+
+if __name__ == '__main__':
+ try:
+ print(get_channel_data("https://www.youtube.com/watch?v=XPpAkggrdaU&feature=youtu.be"))
+ except HttpError as e:
+ print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)) \ No newline at end of file