Skip to content

Instantly share code, notes, and snippets.

@elico
Last active June 9, 2017 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elico/cbda8a6918cb71918616d39b560c90d8 to your computer and use it in GitHub Desktop.
Save elico/cbda8a6918cb71918616d39b560c90d8 to your computer and use it in GitHub Desktop.
This is a tool that finds the user or the channel of a specific YouTube videoid and it will allow future ability for whitelist\blacklist of youtube videos based on their creator or channel.
#!/usr/bin/env ruby
require "nokogiri"
require 'open-uri'
require 'cgi'
def FindYTVideoID(videoid)
id = ""
case videoid
when /^[a-zA-Z0-9\-\_]+$/
id = videoid
when /^https?\:\/\/(m|www)\.youtube\.com\/watch\?v\=([a-zA-Z0-9\-\_]+)/
id = $2
when /^https?\:\/\/(m|www)\.youtube\.com\/embed\/([a-zA-Z0-9\-\_]+)(\/|\?)/
id = $2
when /^https?\:\/\/([a-zA-Z0-9\-\_\.]+)\.ytimg\.com\/vi\/([a-zA-Z0-9\-\_]+)(\/|\?)/
id = $2
else
puts "Unknown url or ID #{videoid}"
end
return id
end
$debug = false
cgi = CGI.new()
h = cgi.params
youtubeid = ""
puts h['videoid'] if $debug
videoid = h['videoid'][0] if h['videoid'].class == Array && h['videoid'].size > 0
youtubeid = FindYTVideoID(videoid)
print cgi.header( 'type' => 'text/plain', "Cache-Control" => "no-cache")
if youtubeid == ""
puts "Missing youtube video ID"
exit 0
end
$whitelist = [ "/channel/UCJ8Li77eOhrHN2YheaWqkHQ" , "/channel/UCQDDeGFnMl4Wj7QogO_5Gvg", "/user/Hidabroot1"]
puts "Started checking videoid => [#{youtubeid}]"
html = Nokogiri::HTML(open("https://www.youtube.com/watch?v=#{youtubeid}"))
def check_channel(channel)
return true if $whitelist.include?(channel.to_s)
return false
end
if html.at_css("div#watch7-user-header") == nil
puts "Missing details or wrong id"
exit 0
end
channelid = html.at_css("div#watch7-user-header").xpath("a/@href")
puts "Video => [#{youtubeid}] , channel or user => [#{channelid}]"
if check_channel(channelid)
puts "RESULT => WHITELISTED"
else
puts "RESULT => not whitelisted"
# Can be checked for blacklisted
end
/*
license note
Copyright (c) 2017, Eliezer Croitoru
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*/
package main
import (
"flag"
"fmt"
"github.com/jbowtie/gokogiri"
"github.com/jbowtie/gokogiri/xpath"
"io/ioutil"
"net/http"
"os"
)
var debug *bool
var videoId *string
func main() {
fmt.Fprintln(os.Stderr, "ERRlog: hello go, running [youtubeID user/cahnnel finder] :D")
debug = flag.Bool("debug", false, "Debug mode, use \"1\" to enable.")
videoId = flag.String("videoid", "", "YouTube VideoID")
flag.Parse()
// fetch and read a web page
if len(*videoId) < 5 {
fmt.Fprintln(os.Stderr, "Missing or small video ID =>", *videoId)
os.Exit(1)
}
resp, err := http.Get("https://www.youtube.com/watch?v=" + *videoId)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
page, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
doc, err := gokogiri.ParseHtml(page)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer doc.Free()
xps := xpath.Compile("//div[@id='watch7-user-header']")
xpw := xpath.Compile("a/@href")
ss, _ := doc.Root().Search(xps)
for _, s := range ss {
ww, _ := s.Search(xpw)
for _, w := range ww {
fmt.Println(w)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment