Last active
December 13, 2015 17:48
-
-
Save mikew/4950734 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env ruby | |
| require 'rexml/document' | |
| require 'open-uri' | |
| include REXML | |
| USAGE = <<EOS | |
| Usage: #{$0} [movie,show] | |
| Marks all items in a Plex library as watched. | |
| EOS | |
| class PlexScrobbler | |
| attr_accessor :section, :section_id | |
| def initialize(section) | |
| @section = section | |
| @section_id = get_section_id | |
| end | |
| def plex_endpoint(part) | |
| 'http://localhost:32400' + part | |
| end | |
| def plex_request(endpoint) | |
| Document.new open(plex_endpoint(endpoint)).read | |
| end | |
| def plexpath(endpoint, xpath) | |
| XPath.match plex_request(endpoint), xpath | |
| end | |
| def get_section_id | |
| plexpath('/library/sections', '//Directory').each do |d| | |
| agent = d.attribute('agent').to_s | |
| key = d.attribute('key').to_s | |
| type = d.attribute('type').to_s | |
| return key if section == type && !agent.end_with?('.none') | |
| end | |
| nil | |
| end | |
| def mark_all_watched | |
| if !section_id | |
| puts "Could not find #{section} in library." | |
| exit 1 | |
| end | |
| element = 'movie' == section ? 'Video' : 'Directory' | |
| plexpath("/library/sections/#{section_id}/all", "//#{element}").each do |d| | |
| id = d.attribute('ratingKey').to_s | |
| endpoint = "/:/scrobble?key=#{id}&identifier=com.plexapp.plugins.library" | |
| puts endpoint | |
| open(plex_endpoint endpoint).read | |
| end | |
| end | |
| end | |
| if 0 == ARGV.length | |
| puts USAGE | |
| exit 0 | |
| else | |
| PlexScrobbler.new(ARGV.first).mark_all_watched | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment