-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplex-compare.py
executable file
·143 lines (107 loc) · 4.96 KB
/
plex-compare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
# -*- coding: utf-8 -*-
# wget -O stranger.xml http://<plexserver>:32400/library/sections/1/all?type=1
# curl -o stranger.xml "http://plex:32400/library/sections/1/all?type=1"
import xml.etree.ElementTree as ET
import urllib2
import csv
import io
class PlexCompare():
def __init__(self, stranger, host):
self.host = host
# File URL
self.stranger = stranger
def compare(self, sectionId):
strangerFile = open(self.stranger, "r")
strangerParsed = ET.parse(strangerFile)
strangerFile.close()
acquaintanceFile = urllib2.urlopen(self.host + "/library/sections/" + sectionId + "/all?type=1")
acquaintanceParsed = ET.parse(acquaintanceFile)
acquaintanceFile.close()
strangerRoot = strangerParsed.getroot()
acquaintanceRoot = acquaintanceParsed.getroot()
# file, title, width, size, audioChannels, videoFrameRate
newVideos = []
betterQualityVideos = []
for video in strangerRoot.getchildren():
media = video.find("Media")
part = media.find("Part")
strangerTitle = video.attrib.get("title")
strangerVideoWidth = media.attrib.get("width")
strangerFile = part.attrib.get("file")
strangerSize = part.attrib.get("size")
strangerAudioChannels = media.attrib.get("audioChannels")
strangerVideoFrameRate = media.attrib.get("videoFrameRate")
strangerVideoResolution = media.attrib.get("videoResolution")
ownThisTitle = False
strangerHasBetterQuality = False
for acquaintanceVideo in acquaintanceRoot.getchildren():
acquaintanceMedia = acquaintanceVideo.find("Media")
acquaintanceTitle = acquaintanceVideo.attrib.get("title")
acquaintanceVideoWidth = acquaintanceMedia.attrib.get("width")
acquaintanceVideoResolution = media.attrib.get("videoResolution")
if strangerTitle == acquaintanceTitle:
ownThisTitle = True
if int(strangerVideoWidth) > int(acquaintanceVideoWidth):
strangerHasBetterQuality = True
break
if ownThisTitle is True:
if strangerHasBetterQuality is True:
betterQualityVideos.append({"title": strangerTitle, "width": strangerVideoWidth, "file": strangerFile, "size": strangerSize, "audioChannels": strangerAudioChannels, "videoFrameRate": strangerVideoFrameRate})
else:
newVideos.append({"title": strangerTitle, "width": strangerVideoWidth, "file": strangerFile, "size": strangerSize, "audioChannels": strangerAudioChannels, "videoFrameRate": strangerVideoFrameRate})
return (newVideos, betterQualityVideos)
def sectionSize(self, sectionId):
sectionFile = urllib2.urlopen(self.host + "/library/sections/" + sectionId + "/all?type=1")
section = ET.parse(sectionFile)
sectionFile.close()
return section.getroot().attrib.get("size")
def saveToDisk(self, videos, filename):
with open(filename, 'wb') as f:
dict_writer = csv.DictWriter(f, videos[0].keys())
dict_writer.writeheader()
for video in videos:
dict_writer.writerow({k:v.encode('utf8') for k,v in video.items()})
def sectionList(self):
sectionsFile = urllib2.urlopen(self.host + "/library/sections")
sectionsParsed = ET.parse(sectionsFile)
sectionsRoot = sectionsParsed.getroot()
sections = []
for section in sectionsRoot.getchildren():
attributes = section.attrib
title = attributes.get("title")
section_type = attributes.get("type")
section_id = attributes.get("key")
sections.append({"title": title, "type": section_type, "id": section_id})
return sections
def printSummary(self):
sections = self.sectionList()
print("There are " + str(len(sections)) + " sections available:")
for section in sections:
print("")
print("\tId: " + section.get("id"))
print("\tTitle: " + section.get("title"))
print("\tType: " + section.get("type"))
print("\tSize: " + self.sectionSize(section.get("id")))
sectionChoice = raw_input("Choose a section _id_ you want to compate with: ")
(newVideos, betterQualityVideos) = self.compare(sectionChoice)
totalSize = 0
for newVideo in newVideos:
totalSize = totalSize + float(newVideo.get("size"))
for betterQualityVideo in betterQualityVideos:
totalSize = totalSize + float(betterQualityVideo.get("size"))
print("")
print("== Summary ==")
print("New Videos: " + str(len(newVideos)))
print("Videos with better quality: " + str(len(betterQualityVideos)))
print("Total size of files: " + str(round(totalSize/1000/1000/1000, 2)) + "GB")
print("")
saveQuestion = raw_input("Do you want to save these videos to a file? (y/n)")
if saveQuestion == "y":
self.saveToDisk(newVideos, "new_videos.csv")
self.saveToDisk(betterQualityVideos, "better_quality_videos.csv")
if __name__ == "__main__":
host = raw_input("Enter the Plex Media Server IP address e.g. plex:32400: ")
if host:
plex = PlexCompare("stranger.xml", "http://" + host)
plex.printSummary()