Hello how am I able to search for video attributes to check if a video is an age-restricted and if it is then it launches an if statement
@commands.command()
async def yt(self, ctx, *, search):
query_string = urllib.parse.urlencode({'search_query': search})
htm_content = urllib.request.urlopen('http://www.youtube.com/results?' + query_string)
search_results = re.findall(r'/watch\?v=(.{11})',htm_content.read().decode())
await ctx.send('http://www.youtube.com/watch?v=' + search_results[0])
A little backstory: I submitted my bot to top.gg which is a bot hosting site and it got declined because the bot was able to send "NSFW" content in non NSFW channels, I really want to get with working so all help is much appreciated
According to YouTube's Data API docs, you have at your disposal the following property attached to age-restricted videos:
You'll have to invoke the
Videos.listAPI endpoint, passing to it the parameteridasid=VIDEO_ID, whereVIDEO_IDis the ID of the video of your interest. Also need not forget to have the parameterpartcontaining the valuecontentDetails.The JSON response text will contain the value you need at
items[0].contentDetails.contentRating.ytRating; that value should be checked if equals withytAgeRestricted. Note also that the propertyytRatingmay well be absent; in this case the video is not age-restricted.In the context of Python, if you're using the Google APIs Client Library for Python, then your call to the
Videos.listendpoint would look like:Note that the code above is simplified quite much since there's no error handling.
Also note that the code above is using the parameter
fieldsfor to obtain from the endpoint only theytRatingproperty. It's always good to ask from the API only the info that is really needed.