Last active
December 18, 2015 02:01
-
-
Save Yengas/c72c94aed201e199f945 to your computer and use it in GitHub Desktop.
Change youtube videos privacy setting between given times with whitelisting/blacklisting of videos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "whitelist": [], | |
| "blacklist": [], | |
| "tcom": [[225, "public"], [230, "private"]], | |
| "account": { | |
| "client_id": "xxx", | |
| "client_secret": "xxx", | |
| "refresh_token": "xxx" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'yt' | |
| def get_closest(arr, val, direction) | |
| return if direction != :left && direction != :right | |
| left = direction == :left | |
| (0 .. arr.size - 1).each{ |i| | |
| return arr[i] if left && val == arr[i][0] | |
| return arr[ (i + (left ? -1 : 0)) % arr.size ] if arr[i][0] > val | |
| } | |
| return left ? arr.last : arr.first | |
| end | |
| class PrivacyHandler | |
| def initialize(properties) | |
| Yt.configure{ |cfg| | |
| cfg.client_id = properties["account"]["client_id"]; | |
| cfg.client_secret = properties["account"]["client_secret"]; | |
| cfg.log_level = :debug; | |
| } | |
| properties["tcom"].sort | |
| @account = Yt::Account.new( refresh_token: properties["account"]["refresh_token"] ) | |
| @whitelist = properties["whitelist"] || [] | |
| @blacklist = properties["blacklist"] || [] | |
| @timed_commands = properties["tcom"] || [] | |
| @process = 0 | |
| end | |
| def private(video, is_private) | |
| return video.update( privacy_status: is_private ? 'private' : 'public' ) | |
| end | |
| def start() | |
| left = get_closest(@timed_commands, self.time(), :left) | |
| (puts "No command found."; return) if left.nil? | |
| process(left[1]); | |
| run_next | |
| end | |
| def run_next() | |
| right = get_closest(@timed_commands, self.time(), :right) | |
| difference = (right[0] - self.time()) % 1440 | |
| puts "Next task will be started after %d minutes." % [difference] | |
| sleep(difference * 60); | |
| process(right[1]); | |
| run_next | |
| end | |
| def process(privacy) | |
| @process += 1 | |
| start = @process + 0 | |
| private = privacy == 'private' | |
| puts "Setting all matching videos to: %s" % [private ? 'private' : 'public'] | |
| Thread.new{ | |
| self.list_videos{|video| | |
| (puts "Another process method has started working..."; return) if @process != start | |
| puts "Setting the privacy for '%s' to: %s." % [video.title, private ? 'private' : 'public'] | |
| self.private(video, private); | |
| } | |
| } | |
| end | |
| def list_videos() | |
| @account.videos.each{|video| | |
| next if (@whitelist.size > 0 && @whitelist.include?(video.id) == false) || (@blacklist.size > 0 && @blacklist.include?(video.id)); | |
| yield video | |
| }; | |
| end | |
| def privacy_toggle(video) | |
| return self.private(video, !video.private?); | |
| end | |
| def time | |
| Time.now.hour * 60 + Time.now.min | |
| end | |
| end | |
| properties = JSON.parse(File.read('application.properties')) | |
| ph = PrivacyHandler.new(properties); | |
| ph.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment