Skip to content

Instantly share code, notes, and snippets.

@wasi-master
Last active November 1, 2021 05:27
Show Gist options
  • Save wasi-master/5a9242b5383e5a00f24f50f4c74a8ca4 to your computer and use it in GitHub Desktop.
Save wasi-master/5a9242b5383e5a00f24f50f4c74a8ca4 to your computer and use it in GitHub Desktop.
Uses neuralblock to find the sponsors of a video

Neuralblock Website: https://ai.neuralblock.app

To use it just save it to a file then use

python filename.py videoid

replace filename with your file name and videoid with the video id

# Copyright 2021 Wasi Master
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import requests
from lxml import html
import re
def end(msg):
print(msg)
exit()
def load_video(video_id):
TIME_REGEX = re.compile(r"\((?P<start>[\d.:]+), ?(?P<end>[\d.:]+)\)")
video_id = sys.argv[1]
response = requests.post("https://ai.neuralblock.app/predict", data={"vid":video_id})
tree = html.fromstring(response.text)
try:
seconds, = tree.xpath('//*[@id="results"]/ul[1]/li[1]/text()') # 'Seconds: (41.253, 71.893) (1053.947, 1098.333)'
minutes, = tree.xpath('//*[@id="results"]/ul[1]/li[2]/text()') # 'Minutes: (0:41,1:12) (17:34,18:18)'
except ValueError:
end("No sponsorships found, maybe the video id is invalid or the video does not have a sponsor")
seconds, minutes = ([(i.group(1), i.group(2)) for i in TIME_REGEX.finditer(x)] for x in (seconds, minutes))
return seconds, minutes
if __name__ == "__main__":
import sys
try:
video = sys.argv[1]
except IndexError:
end("Please specify a video")
sys.stdout.write("\r Loading\r")
sys.stdout.flush()
seconds, minutes = load_video(video)
for index, (seconds, minutes) in enumerate(zip(seconds, minutes)):
print(f"Segment #{index}\n\tStart: {minutes[0]}\n\tEnd: {minutes[1]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment