Whenever you hear the word 'Bot' you probably imagine a robot like they show in the movies. They are bots too, rather smart ones. But we are referring to a different kind of bot here.
每当您听到“ Bot”一词时,您都可能会想象一个像电影中那样的机器人。 他们也是机器人,相当聪明。 但是我们在这里指的是另一种机器人。
We can define a bot as a script that can be used to automate certain tasks which are either tedious or difficult for humans. There are good bots and bad bots, and there is a very fine line between them.
我们可以将机器人定义为脚本,该脚本可用于自动执行某些繁琐或人类难以完成的任务。 有好人和坏人,它们之间有很好的界线。
Before we start, let me tell you one thing, This tutorial only discusses the part which involves uploading videos to YouTube. We will be using a sample video here.
在开始之前,我先告诉您一件事,本教程仅讨论将视频上传到YouTube的部分。 我们将在此处使用示例视频。
If you create a bot which uploads video regularly (like Webdriver Torso, which we will talk more about at the end of the article) you need a source for the video.
如果您创建了一个定期上传视频的机器人(例如Webdriver Torso,我们将在本文结尾详细介绍),则您需要视频的源。
Also, a warning: make sure you check the copyrights of all videos you upload. The version of the bot I first created uploaded others' videos to YouTube – and you won't believe what happened. I got series of copyright claims and strikes and my channel was ultimately deleted.
另外,警告 :请确保您检查了所有上传的视频的版权。 我最初创建的机器人版本将其他人的视频上传到了YouTube –您不会相信发生了什么。 我收到了一系列版权声明和警告,最终删除了我的频道。
So in this tutorial I'll show you how it's done. But if you want to implement it, first find a good source of videos that you can upload without copyright issues.
因此,在本教程中,我将向您展示它是如何完成的。 但是,如果要实施它,首先要找到一个很好的视频源,您可以上传该视频而不会出现版权问题。
Google Apps Script is a scripting platform developed by google for running light-weight applications. Its syntax is based on JavaScript. If you already know Javascript you will find it really easy.
Google Apps脚本是由Google开发的脚本平台,用于运行轻量级应用程序。 它的语法基于JavaScript。 如果您已经知道Javascript,就会发现它非常简单。
The reason we are using Google Apps Script is because it makes it really easy to build this bot. It has in-built support for most Google products like Gmail, Sheets, Forms, and Youtube. And it's free.
我们之所以使用Google Apps脚本,是因为它使构建此机器人变得非常容易。 它具有对大多数Google产品(如Gmail,表格,表格和Youtube)的内置支持。 而且是免费的。
Google Apps Script has an online editor and filesystem. It doesn't need any deployment, you just have to save your code and it's running.
Google Apps脚本具有在线编辑器和文件系统。 它不需要任何部署,您只需要保存代码即可运行。
If you open the Apps Script editor, it will look something like this:
如果您打开Apps脚本编辑器,它将看起来像这样:
Just like any other text editor, it has a sidebar with list of files, a menu, and space for writing code. The code files have a .gs extension (probably for Google script). And most important, all the code must be inside some function – anonymous code won't be executed.
与其他任何文本编辑器一样,它具有一个带有文件列表,菜单和用于编写代码的空间的侧边栏。 代码文件的扩展名为.gs(可能是Google脚本)。 最重要的是,所有代码都必须在某个函数内–匿名代码将不会执行。
There are two special functions, doPost()
and doGet()
. As the name suggests, they are used for handling POST and GET requests, respectively. We won't be using them here, but if you want to create this bot like an API that can be called using HTTP requests, it would be useful.
有两个特殊函数, doPost()
和doGet()
。 顾名思义,它们分别用于处理POST和GET请求。 我们将不在这里使用它们,但是如果您想像可以使用HTTP请求调用的API一样创建此机器人,那么它将很有用。
Unlike other tutorials, I won't tell you to clone a GitHub repository. First, because I want you to learn and code along with me. Second, the code isn't that big, and you need to understand how it works.
与其他教程不同,我不会告诉您克隆GitHub存储库。 首先,因为我希望您与我一起学习和编码。 其次,代码不是那么大,您需要了解它的工作原理。
This is the function that uploads a given video to a YouTube channel:
此功能可将给定的视频上传到YouTube频道:
function upload(url, title, description, topics) {
try {
var video = UrlFetchApp.fetch(url);
YouTube.Videos.insert({
snippet: {
title: title,
description: description,
tags: topics
},
status: {
privacyStatus: "public",
},
}, "snippet,status", video);
return ContentService.createTextOutput("done")
} catch (err) {
return ContentService.createTextOutput(err.message)
}
}
Before you run this function, you have to enable YouTube Data API V3. You must be familiar with what an API is, so let me try to explain that with an example.
在运行此功能之前,您必须启用YouTube Data API V3。 您必须熟悉API是什么,所以让我尝试通过一个示例进行解释。
Google Maps is a great service. It has lots of amazing tools. Suppose you want to use those features in your app, let's say for building a smart digital invitation card with an embedded map.
Google Maps是一项出色的服务。 它有很多惊人的工具。 假设您要在应用程序中使用这些功能,比如说要构建带有嵌入式地图的智能数字邀请卡。
Normally, to do this you need the source code of Maps. But that code is not open-source. And it would be foolish to give someone source code just because they want to use some feature. That's where an API comes handy.
通常,要执行此操作,您需要Maps的源代码。 但是该代码不是开源的。 仅由于某人想要使用某些功能而提供某人源代码是愚蠢的。 那就是API派上用场的地方。
APIs or Application Programming Interfaces, are a way for developers to allow others to use your app's features without disclosing the source code.
API或应用程序编程接口是开发人员允许其他人使用应用程序功能而不公开源代码的一种方式。
In this case, YouTube's API allows developers to use YouTube's features in their apps, or to control some YouTube channel after authentication. If you have an idea of what an API is, let's continue.
在这种情况下,YouTube的API允许开发人员在其应用程序中使用YouTube的功能,或在身份验证后控制某些YouTube频道。 如果您对API是什么有所了解,让我们继续。
To enable the YouTube Data API, navigate to Resources > Advanced Google Services. You will see something like the below. It may ask you to accept Google Cloud's terms before proceeding if you don't have a project already set up. If asked, accept the terms.
要启用YouTube数据API,请导航至资源>高级Google服务 。 您将看到类似下面的内容。 如果您尚未设置项目,则可能会要求您先接受Google Cloud的条款。 如果询问,请接受条款。
Scroll to the bottom. You will see YouTube Data API V3. Usually, you need to create a Google Cloud Project to use it. But App Script creates a project for itself, so you don't need to create something separate. Enable it and close the popup. Now you are good to go.
滚动到底部。 您将看到YouTube Data API V3。 通常,您需要创建一个Google Cloud Project才能使用它。 但是App Script会自己创建一个项目,因此您无需创建其他项目。 启用它并关闭弹出窗口。 现在你很好。
You have to paste the code given below (same as above) into the text editor and save it from the File > Save menu.
您必须将下面给出的代码(与上面相同)粘贴到文本编辑器中,然后从“ 文件”>“保存”菜单中将其保存 。
function upload(url, title, description, topics) {
try {
var video = UrlFetchApp.fetch(url);
YouTube.Videos.insert({
snippet: {
title: title,
description: description,
tags: topics
},
status: {
privacyStatus: "public",
},
}, "snippet,status", video);
return ContentService.createTextOutput("done")
} catch (err) {
return ContentService.createTextOutput(err.message)
}
}
After saving it, you have to navigate to the Run > Run function > Upload menu. It will look something like this:
保存后,您必须导航到“ 运行”>“运行功能”>“上载”菜单。 它看起来像这样:
On clicking the upload button, you will receive a popup like this:
单击上传按钮后,您将收到如下所示的弹出窗口:
Now this is where the usefulness of App Script becomes apparent. Here, you can give it permission to upload videos to your account. It will show you warnings that the app is not verified – but don't worry, that's a security step Google takes to prevent malicious apps.
现在,这是App Script的用途变得显而易见的地方。 在这里,您可以授予将视频上传到您的帐户的权限。 它会向您显示警告,表明该应用程序尚未通过验证-但请放心,这是Google防止恶意应用程序采取的安全措施。
But here, you are the developer, so you can ignore the warning and give permission from the Advanced button on the bottom left. It will ask you for two permissions, as shown:
但是在这里,您是开发人员,因此可以忽略警告,并通过左下方的“高级”按钮授予权限。 它将要求您提供两个权限,如下所示:
Permission to manage your YouTube account is a serious permission. Though it's safe in this particular instance, never give this permission to any app you don't trust. It will give the app complete access to your YouTube account.
拥有管理YouTube帐户的权限是一项认真的权限。 尽管在特定情况下是安全的,但切勿将此权限授予任何您不信任的应用程序。 它将为该应用提供对您的YouTube帐户的完全访问权限。
And this isn't just the case for YouTube. You should be extra careful before you allow sensitive scopes on any platform, like Twitter, Facebook, GitHub and other social sign-in methods.
YouTube不仅如此。 在任何平台(例如Twitter,Facebook,GitHub和其他社交登录方法)上允许敏感范围之前,应格外小心。
After you give that permission, the function will be executed. But nothing will happen as we didn't specify what should be uploaded.
授予该权限后,将执行该功能。 但是不会发生任何事情,因为我们没有指定应上传的内容。
Here, you need to give values to the URL, title, description, and tags variables. Let us take a sample video for our use (remember those copyright issues?).
在这里,您需要为URL,标题,描述和标签变量提供值。 让我们拿一个样本视频供我们使用(还记得那些版权问题吗?)。
This is the modified upload()
function:
这是修改后的upload()
函数:
function upload() {
try {
var video = UrlFetchApp.fetch("https://www.w3schools.com/html/mov_bbb.mp4");
YouTube.Videos.insert({
snippet: {
title: "Big Buck Funny",
description: "This Is The Description",
tags: ["funny"]
},
status: {
privacyStatus: "public",
},
}, "snippet,status", video);
return ContentService.createTextOutput("done")
} catch (err) {
return ContentService.createTextOutput(err.message)
}
}
You can Save it and Run the function again. You can see that we changed the URL to a video URL, the title and description were set, and the tags are also set as an array. You can also set privacyStatus to private for testing.
您可以保存并再次运行该功能。 您会看到我们将URL更改为视频URL,设置了标题和描述,并且标记也设置为数组。 您还可以将privacyStatus设置为private进行测试。
Let's see the results:
让我们看看结果:
As I told you at the start of the tutorial, copyright infringement is a big deal. The above video was removed by YouTube within minutes of uploading.
正如我在教程开始时告诉您的那样,侵犯版权是一件大事。 上面的视频在上传后几分钟内被YouTube删除。
So, find a good video source first. If you want to generate videos programatically, there are libraries like ffmpeg
, but let's not discuss that here. The aim of this article was to create a bot and we did.
因此,首先要找到一个好的视频源。 如果您想以编程方式生成视频,则可以使用ffmpeg
类的库,但在此不再讨论。 本文的目的是创建一个机器人,而我们做到了。
Suppose you find a source for videos and want your bot to regularly upload videos. Google Apps Script has an inbuilt Cron service which will execute your function at regular intervals, like daily, or once a week. You can create a trigger from the dashboard:
假设您找到了视频的来源,并希望您的机器人定期上传视频。 Google Apps脚本具有内置的Cron服务,可定期执行您的功能,例如每天或每周执行一次。 您可以从信息中心创建触发器:
You will get an option to add a trigger on the bottom right. The options will look something like this:
您将获得一个在右下角添加触发器的选项。 选项将如下所示:
You can set the function to be called, the frequency, and other options. Thus we have successfully created a YouTube Uploader Bot.
您可以设置要调用的功能,频率和其他选项。 因此,我们已经成功创建了YouTube Uploader Bot。
This was just one use of Google Apps script – you can use it for other tasks like:
这只是Google Apps脚本的一种用途-您可以将其用于其他任务,例如:
You may try other projects too, There is a lot you can do with it.
您也可以尝试其他项目,您可以做很多事情。
You may not have heard about it, but this bot (or maybe a superhuman) has uploaded more than 70,000 videos on its youtube channel over a relatively short period of time. And although they are procedurally generated videos, it's still very cool.
您可能没有听说过,但是该机器人(或也许是超人)已在较短的时间内在其youtube频道上上传了70,000多个视频。 尽管它们是按程序生成的视频,但它仍然非常酷。
These are the most viewed videos on this channel:
这些是该频道上观看次数最多的视频:
You can see that the Eiffel Tower at night video (which is illegal) has the most views. There are conspiracy theories that this channel is owned by YouTube for testing purposes.
您可以看到夜间埃菲尔铁塔视频(非法)观看次数最多。 有阴谋论认为该频道归YouTube所有,用于测试。
Either way, don't get inspired – if you upload this many videos, YouTube will most probably suspend you.
无论哪种方式,都不会受到启发-如果您上传了这么多视频,YouTube很可能会暂停您。
If you found this interesting, you may visit this article to know more.
如果您发现这很有趣,则可以访问本文以了解更多信息。
The response to my previous article was great – thank you! Your feedback motivates me to write new stuff. Also, do visit my internet home theabbie.github.io.
对我上一篇文章的反馈很好-谢谢! 您的反馈促使我写新东西。 另外,请访问我的互联网主页theabbie.github.io 。
翻译自: https://www.freecodecamp.org/news/build-a-youtube-uploader-bot-using-google-apps-script/