reworked scheduler to pass server to instance to remove duplication
This commit is contained in:
		
							parent
							
								
									bf1db64b82
								
							
						
					
					
						commit
						7be718751b
					
				
					 6 changed files with 270 additions and 291 deletions
				
			
		|  | @ -116,7 +116,7 @@ class TautulliServer(NamedTuple): | ||||||
|     id: int = None |     id: int = None | ||||||
|     url: str = None |     url: str = None | ||||||
|     fallback_ip: str = None |     fallback_ip: str = None | ||||||
|     apikey: str = None |     api_key: str = None | ||||||
|     verify_ssl: bool = None |     verify_ssl: bool = None | ||||||
|     get_activity: bool = False |     get_activity: bool = False | ||||||
|     get_activity_run_seconds: int = 30 |     get_activity_run_seconds: int = 30 | ||||||
|  | @ -319,7 +319,6 @@ class TautulliStream(NamedTuple): | ||||||
|     transcode_progress: int = None |     transcode_progress: int = None | ||||||
|     subtitle_language: str = None |     subtitle_language: str = None | ||||||
|     stream_subtitle_container: str = None |     stream_subtitle_container: str = None | ||||||
|     _cache_time: int = None |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def geoip_download(): | def geoip_download(): | ||||||
|  |  | ||||||
							
								
								
									
										126
									
								
								Varken/radarr.py
									
										
									
									
									
								
							
							
						
						
									
										126
									
								
								Varken/radarr.py
									
										
									
									
									
								
							|  | @ -7,11 +7,11 @@ from Varken.helpers import Movie, Queue | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class RadarrAPI(object): | class RadarrAPI(object): | ||||||
|     def __init__(self, servers, influx_server): |     def __init__(self, server, influx_server): | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|         self.influx = InfluxDBClient(influx_server.url, influx_server.port, influx_server.username, |         self.influx = InfluxDBClient(influx_server.url, influx_server.port, influx_server.username, | ||||||
|                                      influx_server.password, 'plex2') |                                      influx_server.password, 'plex2') | ||||||
|         self.servers = servers |         self.server = server | ||||||
|         # Create session to reduce server web thread load, and globally define pageSize for all requests |         # Create session to reduce server web thread load, and globally define pageSize for all requests | ||||||
|         self.session = requests.Session() |         self.session = requests.Session() | ||||||
| 
 | 
 | ||||||
|  | @ -20,87 +20,85 @@ class RadarrAPI(object): | ||||||
|         self.influx.write_points(payload) |         self.influx.write_points(payload) | ||||||
| 
 | 
 | ||||||
|     @logging |     @logging | ||||||
|     def get_missing(self, notimplemented): |     def get_missing(self): | ||||||
|         endpoint = '/api/movie' |         endpoint = '/api/movie' | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|         influx_payload = [] |         influx_payload = [] | ||||||
| 
 | 
 | ||||||
|         for server in self.servers: |         missing = [] | ||||||
|             missing = [] |         headers = {'X-Api-Key': self.server.api_key} | ||||||
|             headers = {'X-Api-Key': server.api_key} |         get = self.session.get(self.server.url + endpoint, headers=headers, verify=self.server.verify_ssl).json() | ||||||
|             get = self.session.get(server.url + endpoint, headers=headers, verify=server.verify_ssl).json() |         movies = [Movie(**movie) for movie in get] | ||||||
|             movies = [Movie(**movie) for movie in get] |  | ||||||
| 
 | 
 | ||||||
|             for movie in movies: |         for movie in movies: | ||||||
|                 if server.get_missing: |             if self.server.get_missing: | ||||||
|                     if not movie.downloaded and movie.isAvailable: |                 if not movie.downloaded and movie.isAvailable: | ||||||
|                         ma = True |                     ma = True | ||||||
|                     else: |                 else: | ||||||
|                         ma = False |                     ma = False | ||||||
|                     movie_name = '{} ({})'.format(movie.title, movie.year) |                 movie_name = '{} ({})'.format(movie.title, movie.year) | ||||||
|                     missing.append((movie_name, ma, movie.tmdbId)) |                 missing.append((movie_name, ma, movie.tmdbId)) | ||||||
| 
 | 
 | ||||||
|             for title, ma, mid in missing: |         for title, ma, mid in missing: | ||||||
|                 influx_payload.append( |             influx_payload.append( | ||||||
|                     { |                 { | ||||||
|                         "measurement": "Radarr", |                     "measurement": "Radarr", | ||||||
|                         "tags": { |                     "tags": { | ||||||
|                             "Missing": True, |                         "Missing": True, | ||||||
|                             "Missing_Available": ma, |                         "Missing_Available": ma, | ||||||
|                             "tmdbId": mid, |                         "tmdbId": mid, | ||||||
|                             "server": server.id |                         "server": self.server.id | ||||||
|                         }, |                     }, | ||||||
|                         "time": self.now, |                     "time": self.now, | ||||||
|                         "fields": { |                     "fields": { | ||||||
|                             "name": title |                         "name": title | ||||||
|                         } |  | ||||||
|                     } |                     } | ||||||
|                 ) |                 } | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|         self.influx_push(influx_payload) |         self.influx_push(influx_payload) | ||||||
| 
 | 
 | ||||||
|     @logging |     @logging | ||||||
|     def get_queue(self, notimplemented): |     def get_queue(self): | ||||||
|         endpoint = '/api/queue' |         endpoint = '/api/queue' | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|         influx_payload = [] |         influx_payload = [] | ||||||
| 
 | 
 | ||||||
|         for server in self.servers: |         queue = [] | ||||||
|             queue = [] |         headers = {'X-Api-Key': self.server.api_key} | ||||||
|             headers = {'X-Api-Key': server.api_key} |         get = self.session.get(self.server.url + endpoint, headers=headers, verify=self.server.verify_ssl).json() | ||||||
|             get = self.session.get(server.url + endpoint, headers=headers, verify=server.verify_ssl).json() |         for movie in get: | ||||||
|             for movie in get: |             movie['movie'] = Movie(**movie['movie']) | ||||||
|                 movie['movie'] = Movie(**movie['movie']) |         download_queue = [Queue(**movie) for movie in get] | ||||||
|             download_queue = [Queue(**movie) for movie in get] |  | ||||||
| 
 | 
 | ||||||
|             for queue_item in download_queue: |         for queue_item in download_queue: | ||||||
|                 name = '{} ({})'.format(queue_item.movie.title, queue_item.movie.year) |             name = '{} ({})'.format(queue_item.movie.title, queue_item.movie.year) | ||||||
| 
 | 
 | ||||||
|                 if queue_item.protocol.upper() == 'USENET': |             if queue_item.protocol.upper() == 'USENET': | ||||||
|                     protocol_id = 1 |                 protocol_id = 1 | ||||||
|                 else: |             else: | ||||||
|                     protocol_id = 0 |                 protocol_id = 0 | ||||||
| 
 | 
 | ||||||
|                 queue.append((name, queue_item.quality['quality']['name'], queue_item.protocol.upper(), |             queue.append((name, queue_item.quality['quality']['name'], queue_item.protocol.upper(), | ||||||
|                               protocol_id, queue_item.id)) |                           protocol_id, queue_item.id)) | ||||||
| 
 | 
 | ||||||
|             for movie, quality, protocol, protocol_id, qid in queue: |         for movie, quality, protocol, protocol_id, qid in queue: | ||||||
|                 influx_payload.append( |             influx_payload.append( | ||||||
|                     { |                 { | ||||||
|                         "measurement": "Radarr", |                     "measurement": "Radarr", | ||||||
|                         "tags": { |                     "tags": { | ||||||
|                             "type": "Queue", |                         "type": "Queue", | ||||||
|                             "tmdbId": qid, |                         "tmdbId": qid, | ||||||
|                             "server": server.id |                         "server": self.server.id | ||||||
|                         }, |                     }, | ||||||
|                         "time": self.now, |                     "time": self.now, | ||||||
|                         "fields": { |                     "fields": { | ||||||
|                             "name": movie, |                         "name": movie, | ||||||
|                             "quality": quality, |                         "quality": quality, | ||||||
|                             "protocol": protocol, |                         "protocol": protocol, | ||||||
|                             "protocol_id": protocol_id |                         "protocol_id": protocol_id | ||||||
|                         } |  | ||||||
|                     } |                     } | ||||||
|                 ) |                 } | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|         self.influx_push(influx_payload) |         self.influx_push(influx_payload) | ||||||
|  |  | ||||||
							
								
								
									
										192
									
								
								Varken/sonarr.py
									
										
									
									
									
								
							
							
						
						
									
										192
									
								
								Varken/sonarr.py
									
										
									
									
									
								
							|  | @ -7,147 +7,141 @@ from Varken.helpers import TVShow, Queue | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class SonarrAPI(object): | class SonarrAPI(object): | ||||||
|     def __init__(self, servers, influx_server): |     def __init__(self, server, influx_server): | ||||||
|         # Set Time of initialization |         # Set Time of initialization | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|         self.today = str(date.today()) |         self.today = str(date.today()) | ||||||
|         self.influx = InfluxDBClient(influx_server.url, influx_server.port, influx_server.username, |         self.influx = InfluxDBClient(influx_server.url, influx_server.port, influx_server.username, | ||||||
|                                      influx_server.password, 'plex') |                                      influx_server.password, 'plex') | ||||||
|         self.servers = servers |         self.server = server | ||||||
|         # Create session to reduce server web thread load, and globally define pageSize for all requests |         # Create session to reduce server web thread load, and globally define pageSize for all requests | ||||||
|         self.session = requests.Session() |         self.session = requests.Session() | ||||||
|         self.session.params = {'pageSize': 1000} |         self.session.params = {'pageSize': 1000} | ||||||
| 
 | 
 | ||||||
|     @logging |     @logging | ||||||
|     def get_missing(self, days_past): |     def get_missing(self): | ||||||
|         endpoint = '/api/calendar' |         endpoint = '/api/calendar' | ||||||
|         last_days = str(date.today() + timedelta(days=-days_past)) |         last_days = str(date.today() + timedelta(days=-self.server.missing_days)) | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|         params = {'start': last_days, 'end': self.today} |         params = {'start': last_days, 'end': self.today} | ||||||
|         influx_payload = [] |         influx_payload = [] | ||||||
|  |         missing = [] | ||||||
|  |         headers = {'X-Api-Key': self.server.api_key} | ||||||
| 
 | 
 | ||||||
|         for server in self.servers: |         get = self.session.get(self.server.url + endpoint, params=params, headers=headers, | ||||||
|             missing = [] |                                verify=self.server.verify_ssl).json() | ||||||
|             headers = {'X-Api-Key': server.api_key} |         # Iteratively create a list of TVShow Objects from response json | ||||||
|  |         tv_shows = [TVShow(**show) for show in get] | ||||||
| 
 | 
 | ||||||
|             get = self.session.get(server.url + endpoint, params=params, headers=headers, |         # Add show to missing list if file does not exist | ||||||
|                                    verify=server.verify_ssl).json() |         for show in tv_shows: | ||||||
|             # Iteratively create a list of TVShow Objects from response json |             if not show.hasFile: | ||||||
|             tv_shows = [TVShow(**show) for show in get] |                 sxe = 'S{:0>2}E{:0>2}'.format(show.seasonNumber, show.episodeNumber) | ||||||
|  |                 missing.append((show.series['title'], sxe, show.airDate, show.title, show.id)) | ||||||
| 
 | 
 | ||||||
|             # Add show to missing list if file does not exist |         for series_title, sxe, air_date, episode_title, sonarr_id in missing: | ||||||
|             for show in tv_shows: |             influx_payload.append( | ||||||
|                 if not show.hasFile: |                 { | ||||||
|                     sxe = 'S{:0>2}E{:0>2}'.format(show.seasonNumber, show.episodeNumber) |                     "measurement": "Sonarr", | ||||||
|                     missing.append((show.series['title'], sxe, show.airDate, show.title, show.id)) |                     "tags": { | ||||||
| 
 |                         "type": "Missing", | ||||||
|             for series_title, sxe, air_date, episode_title, sonarr_id in missing: |                         "sonarrId": sonarr_id, | ||||||
|                 influx_payload.append( |                         "server": self.server.id | ||||||
|                     { |                     }, | ||||||
|                         "measurement": "Sonarr", |                     "time": self.now, | ||||||
|                         "tags": { |                     "fields": { | ||||||
|                             "type": "Missing", |                         "name": series_title, | ||||||
|                             "sonarrId": sonarr_id, |                         "epname": episode_title, | ||||||
|                             "server": server.id |                         "sxe": sxe, | ||||||
|                         }, |                         "airs": air_date | ||||||
|                         "time": self.now, |  | ||||||
|                         "fields": { |  | ||||||
|                             "name": series_title, |  | ||||||
|                             "epname": episode_title, |  | ||||||
|                             "sxe": sxe, |  | ||||||
|                             "airs": air_date |  | ||||||
|                         } |  | ||||||
|                     } |                     } | ||||||
|                 ) |                 } | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|         self.influx_push(influx_payload) |         self.influx_push(influx_payload) | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|     @logging |     @logging | ||||||
|     def get_future(self, future_days): |     def get_future(self): | ||||||
|         endpoint = '/api/calendar/' |         endpoint = '/api/calendar/' | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|         future = str(date.today() + timedelta(days=future_days)) |         future = str(date.today() + timedelta(days=self.server.future_days)) | ||||||
|         influx_payload = [] |         influx_payload = [] | ||||||
|  |         air_days = [] | ||||||
|  |         headers = {'X-Api-Key': self.server.api_key} | ||||||
|  |         params = {'start': self.today, 'end': future} | ||||||
| 
 | 
 | ||||||
|         for server in self.servers: |         get = self.session.get(self.server.url + endpoint, params=params, headers=headers, | ||||||
|             air_days = [] |                                verify=self.server.verify_ssl).json() | ||||||
|  |         tv_shows = [TVShow(**show) for show in get] | ||||||
| 
 | 
 | ||||||
|             headers = {'X-Api-Key': server.api_key} |         for show in tv_shows: | ||||||
|             params = {'start': self.today, 'end': future} |             sxe = 'S{:0>2}E{:0>2}'.format(show.seasonNumber, show.episodeNumber) | ||||||
|  |             air_days.append((show.series['title'], show.hasFile, sxe, show.title, show.airDate, show.id)) | ||||||
| 
 | 
 | ||||||
|             get = self.session.get(server.url + endpoint, params=params, headers=headers, |         for series_title, dl_status, sxe, episode_title, air_date, sonarr_id in air_days: | ||||||
|                                    verify=server.verify_ssl).json() |             influx_payload.append( | ||||||
|             tv_shows = [TVShow(**show) for show in get] |                 { | ||||||
| 
 |                     "measurement": "Sonarr", | ||||||
|             for show in tv_shows: |                     "tags": { | ||||||
|                 sxe = 'S{:0>2}E{:0>2}'.format(show.seasonNumber, show.episodeNumber) |                         "type": "Future", | ||||||
|                 air_days.append((show.series['title'], show.hasFile, sxe, show.title, show.airDate, show.id)) |                         "sonarrId": sonarr_id, | ||||||
| 
 |                         "server": self.server.id | ||||||
|             for series_title, dl_status, sxe, episode_title, air_date, sonarr_id in air_days: |                     }, | ||||||
|                 influx_payload.append( |                     "time": self.now, | ||||||
|                     { |                     "fields": { | ||||||
|                         "measurement": "Sonarr", |                         "name": series_title, | ||||||
|                         "tags": { |                         "epname": episode_title, | ||||||
|                             "type": "Future", |                         "sxe": sxe, | ||||||
|                             "sonarrId": sonarr_id, |                         "airs": air_date, | ||||||
|                             "server": server.id |                         "downloaded": dl_status | ||||||
|                         }, |  | ||||||
|                         "time": self.now, |  | ||||||
|                         "fields": { |  | ||||||
|                             "name": series_title, |  | ||||||
|                             "epname": episode_title, |  | ||||||
|                             "sxe": sxe, |  | ||||||
|                             "airs": air_date, |  | ||||||
|                             "downloaded": dl_status |  | ||||||
|                         } |  | ||||||
|                     } |                     } | ||||||
|                 ) |                 } | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|         self.influx_push(influx_payload) |         self.influx_push(influx_payload) | ||||||
| 
 | 
 | ||||||
|     @logging |     @logging | ||||||
|     def get_queue(self, notimplemented): |     def get_queue(self): | ||||||
|         influx_payload = [] |         influx_payload = [] | ||||||
|         endpoint = '/api/queue' |         endpoint = '/api/queue' | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|  |         queue = [] | ||||||
|  |         headers = {'X-Api-Key': self.server.api_key} | ||||||
| 
 | 
 | ||||||
|         for server in self.servers: |         get = self.session.get(self.server.url + endpoint, headers=headers, verify=self.server.verify_ssl).json() | ||||||
|             queue = [] |         download_queue = [Queue(**show) for show in get] | ||||||
|             headers = {'X-Api-Key': server.api_key} |  | ||||||
| 
 | 
 | ||||||
|             get = self.session.get(server.url + endpoint, headers=headers, verify=server.verify_ssl).json() |         for show in download_queue: | ||||||
|             download_queue = [Queue(**show) for show in get] |             sxe = 'S{:0>2}E{:0>2}'.format(show.episode['seasonNumber'], show.episode['episodeNumber']) | ||||||
|  |             if show.protocol.upper() == 'USENET': | ||||||
|  |                 protocol_id = 1 | ||||||
|  |             else: | ||||||
|  |                 protocol_id = 0 | ||||||
| 
 | 
 | ||||||
|             for show in download_queue: |             queue.append((show.series['title'], show.episode['title'], show.protocol.upper(), | ||||||
|                 sxe = 'S{:0>2}E{:0>2}'.format(show.episode['seasonNumber'], show.episode['episodeNumber']) |                           protocol_id, sxe, show.id)) | ||||||
|                 if show.protocol.upper() == 'USENET': |  | ||||||
|                     protocol_id = 1 |  | ||||||
|                 else: |  | ||||||
|                     protocol_id = 0 |  | ||||||
| 
 | 
 | ||||||
|                 queue.append((show.series['title'], show.episode['title'], show.protocol.upper(), |         for series_title, episode_title, protocol, protocol_id, sxe, sonarr_id in queue: | ||||||
|                               protocol_id, sxe, show.id)) |             influx_payload.append( | ||||||
|  |                 { | ||||||
|  |                     "measurement": "Sonarr", | ||||||
|  |                     "tags": { | ||||||
|  |                         "type": "Queue", | ||||||
|  |                         "sonarrId": sonarr_id, | ||||||
|  |                         "server": self.server.id | ||||||
| 
 | 
 | ||||||
|             for series_title, episode_title, protocol, protocol_id, sxe, sonarr_id in queue: |                     }, | ||||||
|                 influx_payload.append( |                     "time": self.now, | ||||||
|                     { |                     "fields": { | ||||||
|                         "measurement": "Sonarr", |                         "name": series_title, | ||||||
|                         "tags": { |                         "epname": episode_title, | ||||||
|                             "type": "Queue", |                         "sxe": sxe, | ||||||
|                             "sonarrId": sonarr_id, |                         "protocol": protocol, | ||||||
|                             "server": server.id |                         "protocol_id": protocol_id | ||||||
| 
 |  | ||||||
|                         }, |  | ||||||
|                         "time": self.now, |  | ||||||
|                         "fields": { |  | ||||||
|                             "name": series_title, |  | ||||||
|                             "epname": episode_title, |  | ||||||
|                             "sxe": sxe, |  | ||||||
|                             "protocol": protocol, |  | ||||||
|                             "protocol_id": protocol_id |  | ||||||
|                         } |  | ||||||
|                     } |                     } | ||||||
|                 ) |                 } | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|         self.influx_push(influx_payload) |         self.influx_push(influx_payload) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -7,12 +7,12 @@ from Varken.logger import logging | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class TautulliAPI(object): | class TautulliAPI(object): | ||||||
|     def __init__(self, servers, influx_server): |     def __init__(self, server, influx_server): | ||||||
|         # Set Time of initialization |         # Set Time of initialization | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|         self.influx = InfluxDBClient(influx_server.url, influx_server.port, influx_server.username, |         self.influx = InfluxDBClient(influx_server.url, influx_server.port, influx_server.username, | ||||||
|                                      influx_server.password, 'plex2') |                                      influx_server.password, 'plex2') | ||||||
|         self.servers = servers |         self.server = server | ||||||
|         self.session = requests.Session() |         self.session = requests.Session() | ||||||
|         self.endpoint = '/api/v2' |         self.endpoint = '/api/v2' | ||||||
| 
 | 
 | ||||||
|  | @ -21,128 +21,124 @@ class TautulliAPI(object): | ||||||
|         self.influx.write_points(payload) |         self.influx.write_points(payload) | ||||||
| 
 | 
 | ||||||
|     @logging |     @logging | ||||||
|     def get_activity(self, notimplemented): |     def get_activity(self): | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|         params = {'cmd': 'get_activity'} |         params = {'cmd': 'get_activity'} | ||||||
|         influx_payload = [] |         influx_payload = [] | ||||||
|  |         params['apikey'] = self.server.api_key | ||||||
|  |         g = self.session.get(self.server.url + self.endpoint, params=params, verify=self.server.verify_ssl) | ||||||
|  |         get = g.json()['response']['data'] | ||||||
| 
 | 
 | ||||||
|         for server in self.servers: |         influx_payload.append( | ||||||
|             params['apikey'] = server.apikey |             { | ||||||
|             g = self.session.get(server.url + self.endpoint, params=params, verify=server.verify_ssl) |                 "measurement": "Tautulli", | ||||||
|             get = g.json()['response']['data'] |                 "tags": { | ||||||
|  |                     "type": "current_stream_stats", | ||||||
|  |                     "server": self.server.id | ||||||
|  |                 }, | ||||||
|  |                 "time": self.now, | ||||||
|  |                 "fields": { | ||||||
|  |                     "stream_count": int(get['stream_count']), | ||||||
|  |                     "total_bandwidth": int(get['total_bandwidth']), | ||||||
|  |                     "wan_bandwidth": int(get['wan_bandwidth']), | ||||||
|  |                     "lan_bandwidth": int(get['lan_bandwidth']), | ||||||
|  |                     "transcode_streams": int(get['stream_count_transcode']), | ||||||
|  |                     "direct_play_streams": int(get['stream_count_direct_play']), | ||||||
|  |                     "direct_streams": int(get['stream_count_direct_stream']) | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         ) | ||||||
|  | 
 | ||||||
|  |         self.influx_push(influx_payload) | ||||||
|  | 
 | ||||||
|  |     @logging | ||||||
|  |     def get_sessions(self): | ||||||
|  |         self.now = datetime.now(timezone.utc).astimezone().isoformat() | ||||||
|  |         params = {'cmd': 'get_activity'} | ||||||
|  |         influx_payload = [] | ||||||
|  |         params['apikey'] = self.server.api_key | ||||||
|  |         g = self.session.get(self.server.url + self.endpoint, params=params, verify=self.server.verify_ssl) | ||||||
|  |         get = g.json()['response']['data']['sessions'] | ||||||
|  |         sessions = [TautulliStream(**session) for session in get] | ||||||
|  | 
 | ||||||
|  |         for session in sessions: | ||||||
|  |             try: | ||||||
|  |                 geodata = geo_lookup(session.ip_address_public) | ||||||
|  |             except (ValueError, AddressNotFoundError): | ||||||
|  |                 if self.server.fallback_ip: | ||||||
|  |                     geodata = geo_lookup(self.server.fallback_ip) | ||||||
|  |                 else: | ||||||
|  |                     my_ip = requests.get('http://ip.42.pl/raw').text | ||||||
|  |                     geodata = geo_lookup(my_ip) | ||||||
|  | 
 | ||||||
|  |             if not all([geodata.location.latitude, geodata.location.longitude]): | ||||||
|  |                 latitude = 37.234332396 | ||||||
|  |                 longitude = -115.80666344 | ||||||
|  |             else: | ||||||
|  |                 latitude = geodata.location.latitude | ||||||
|  |                 longitude = geodata.location.longitude | ||||||
|  | 
 | ||||||
|  |             decision = session.transcode_decision | ||||||
|  |             if decision == 'copy': | ||||||
|  |                 decision = 'direct stream' | ||||||
|  | 
 | ||||||
|  |             video_decision = session.stream_video_decision | ||||||
|  |             if video_decision == 'copy': | ||||||
|  |                 video_decision = 'direct stream' | ||||||
|  |             elif video_decision == '': | ||||||
|  |                 video_decision = 'Music' | ||||||
|  | 
 | ||||||
|  |             quality = session.stream_video_resolution | ||||||
|  |             if not quality: | ||||||
|  |                 quality = session.container.upper() | ||||||
|  |             elif quality in ('SD', 'sd', '4k'): | ||||||
|  |                 quality = session.stream_video_resolution.upper() | ||||||
|  |             else: | ||||||
|  |                 quality = session.stream_video_resolution + 'p' | ||||||
|  | 
 | ||||||
|  |             player_state = session.state.lower() | ||||||
|  |             if player_state == 'playing': | ||||||
|  |                 player_state = 0 | ||||||
|  |             elif player_state == 'paused': | ||||||
|  |                 player_state = 1 | ||||||
|  |             elif player_state == 'buffering': | ||||||
|  |                 player_state = 3 | ||||||
| 
 | 
 | ||||||
|             influx_payload.append( |             influx_payload.append( | ||||||
|                 { |                 { | ||||||
|                     "measurement": "Tautulli", |                     "measurement": "Tautulli", | ||||||
|                     "tags": { |                     "tags": { | ||||||
|                         "type": "current_stream_stats", |                         "type": "Session", | ||||||
|                         "server": server.id |                         "session_id": session.session_id, | ||||||
|  |                         "name": session.friendly_name, | ||||||
|  |                         "title": session.full_title, | ||||||
|  |                         "platform": session.platform, | ||||||
|  |                         "product_version": session.product_version, | ||||||
|  |                         "quality": quality, | ||||||
|  |                         "video_decision": video_decision.title(), | ||||||
|  |                         "transcode_decision": decision.title(), | ||||||
|  |                         "media_type": session.media_type.title(), | ||||||
|  |                         "audio_codec": session.audio_codec.upper(), | ||||||
|  |                         "audio_profile": session.audio_profile.upper(), | ||||||
|  |                         "stream_audio_codec": session.stream_audio_codec.upper(), | ||||||
|  |                         "quality_profile": session.quality_profile, | ||||||
|  |                         "progress_percent": session.progress_percent, | ||||||
|  |                         "region_code": geodata.subdivisions.most_specific.iso_code, | ||||||
|  |                         "location": geodata.city.name, | ||||||
|  |                         "full_location": '{} - {}'.format(geodata.subdivisions.most_specific.name, | ||||||
|  |                                                           geodata.city.name), | ||||||
|  |                         "latitude": latitude, | ||||||
|  |                         "longitude": longitude, | ||||||
|  |                         "player_state": player_state, | ||||||
|  |                         "device_type": session.platform, | ||||||
|  |                         "server": self.server.id | ||||||
|                     }, |                     }, | ||||||
|                     "time": self.now, |                     "time": self.now, | ||||||
|                     "fields": { |                     "fields": { | ||||||
|                         "stream_count": int(get['stream_count']), |                         "session_id": session.session_id, | ||||||
|                         "total_bandwidth": int(get['total_bandwidth']), |                         "session_key": session.session_key | ||||||
|                         "wan_bandwidth": int(get['wan_bandwidth']), |  | ||||||
|                         "lan_bandwidth": int(get['lan_bandwidth']), |  | ||||||
|                         "transcode_streams": int(get['stream_count_transcode']), |  | ||||||
|                         "direct_play_streams": int(get['stream_count_direct_play']), |  | ||||||
|                         "direct_streams": int(get['stream_count_direct_stream']) |  | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             ) |             ) | ||||||
| 
 | 
 | ||||||
|         self.influx_push(influx_payload) |         self.influx_push(influx_payload) | ||||||
| 
 |  | ||||||
|     @logging |  | ||||||
|     def get_sessions(self, notimplemented): |  | ||||||
|         self.now = datetime.now(timezone.utc).astimezone().isoformat() |  | ||||||
|         params = {'cmd': 'get_activity'} |  | ||||||
|         influx_payload = [] |  | ||||||
| 
 |  | ||||||
|         for server in self.servers: |  | ||||||
|             params['apikey'] = server.apikey |  | ||||||
|             g = self.session.get(server.url + self.endpoint, params=params, verify=server.verify_ssl) |  | ||||||
|             get = g.json()['response']['data']['sessions'] |  | ||||||
|             sessions = [TautulliStream(**session) for session in get] |  | ||||||
| 
 |  | ||||||
|             for session in sessions: |  | ||||||
|                 try: |  | ||||||
|                     geodata = geo_lookup(session.ip_address_public) |  | ||||||
|                 except (ValueError, AddressNotFoundError): |  | ||||||
|                     if server.fallback_ip: |  | ||||||
|                         geodata = geo_lookup(server.fallback_ip) |  | ||||||
|                     else: |  | ||||||
|                         my_ip = requests.get('http://ip.42.pl/raw').text |  | ||||||
|                         geodata = geo_lookup(my_ip) |  | ||||||
| 
 |  | ||||||
|                 if not all([geodata.location.latitude, geodata.location.longitude]): |  | ||||||
|                     latitude = 37.234332396 |  | ||||||
|                     longitude = -115.80666344 |  | ||||||
|                 else: |  | ||||||
|                     latitude = geodata.location.latitude |  | ||||||
|                     longitude = geodata.location.longitude |  | ||||||
| 
 |  | ||||||
|                 decision = session.transcode_decision |  | ||||||
|                 if decision == 'copy': |  | ||||||
|                     decision = 'direct stream' |  | ||||||
| 
 |  | ||||||
|                 video_decision = session.stream_video_decision |  | ||||||
|                 if video_decision == 'copy': |  | ||||||
|                     video_decision = 'direct stream' |  | ||||||
|                 elif video_decision == '': |  | ||||||
|                     video_decision = 'Music' |  | ||||||
| 
 |  | ||||||
|                 quality = session.stream_video_resolution |  | ||||||
|                 if not quality: |  | ||||||
|                     quality = session.container.upper() |  | ||||||
|                 elif quality in ('SD', 'sd', '4k'): |  | ||||||
|                     quality = session.stream_video_resolution.upper() |  | ||||||
|                 else: |  | ||||||
|                     quality = session.stream_video_resolution + 'p' |  | ||||||
| 
 |  | ||||||
|                 player_state = session.state.lower() |  | ||||||
|                 if player_state == 'playing': |  | ||||||
|                     player_state = 0 |  | ||||||
|                 elif player_state == 'paused': |  | ||||||
|                     player_state = 1 |  | ||||||
|                 elif player_state == 'buffering': |  | ||||||
|                     player_state = 3 |  | ||||||
| 
 |  | ||||||
|                 influx_payload.append( |  | ||||||
|                     { |  | ||||||
|                         "measurement": "Tautulli", |  | ||||||
|                         "tags": { |  | ||||||
|                             "type": "Session", |  | ||||||
|                             "session_id": session.session_id, |  | ||||||
|                             "name": session.friendly_name, |  | ||||||
|                             "title": session.full_title, |  | ||||||
|                             "platform": session.platform, |  | ||||||
|                             "product_version": session.product_version, |  | ||||||
|                             "quality": quality, |  | ||||||
|                             "video_decision": video_decision.title(), |  | ||||||
|                             "transcode_decision": decision.title(), |  | ||||||
|                             "media_type": session.media_type.title(), |  | ||||||
|                             "audio_codec": session.audio_codec.upper(), |  | ||||||
|                             "audio_profile": session.audio_profile.upper(), |  | ||||||
|                             "stream_audio_codec": session.stream_audio_codec.upper(), |  | ||||||
|                             "quality_profile": session.quality_profile, |  | ||||||
|                             "progress_percent": session.progress_percent, |  | ||||||
|                             "region_code": geodata.subdivisions.most_specific.iso_code, |  | ||||||
|                             "location": geodata.city.name, |  | ||||||
|                             "full_location": '{} - {}'.format(geodata.subdivisions.most_specific.name, |  | ||||||
|                                                               geodata.city.name), |  | ||||||
|                             "latitude": latitude, |  | ||||||
|                             "longitude": longitude, |  | ||||||
|                             "player_state": player_state, |  | ||||||
|                             "device_type": session.platform, |  | ||||||
|                             "server": server.id |  | ||||||
|                         }, |  | ||||||
|                         "time": self.now, |  | ||||||
|                         "fields": { |  | ||||||
|                             "session_id": session.session_id, |  | ||||||
|                             "session_key": session.session_key |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 ) |  | ||||||
| 
 |  | ||||||
|         self.influx_push(influx_payload) |  | ||||||
|  |  | ||||||
|  | @ -61,7 +61,6 @@ verify_ssl = true | ||||||
| queue = true | queue = true | ||||||
| queue_run_seconds = 300 | queue_run_seconds = 300 | ||||||
| get_missing = true | get_missing = true | ||||||
| get_missing_available = true |  | ||||||
| get_missing_run_seconds = 300 | get_missing_run_seconds = 300 | ||||||
| 
 | 
 | ||||||
| [radarr-2] | [radarr-2] | ||||||
|  |  | ||||||
							
								
								
									
										23
									
								
								varken.py
									
										
									
									
									
								
							
							
						
						
									
										23
									
								
								varken.py
									
										
									
									
									
								
							|  | @ -8,8 +8,8 @@ from Varken.tautulli import TautulliAPI | ||||||
| from Varken.radarr import RadarrAPI | from Varken.radarr import RadarrAPI | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def threaded(job, days=None): | def threaded(job): | ||||||
|     thread = threading.Thread(target=job, args=([days])) |     thread = threading.Thread(target=job) | ||||||
|     thread.start() |     thread.start() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -17,38 +17,31 @@ if __name__ == "__main__": | ||||||
|     CONFIG = INIParser() |     CONFIG = INIParser() | ||||||
| 
 | 
 | ||||||
|     if CONFIG.sonarr_enabled: |     if CONFIG.sonarr_enabled: | ||||||
|         SONARR = SonarrAPI(CONFIG.sonarr_servers, CONFIG.influx_server) |  | ||||||
| 
 |  | ||||||
|         for server in CONFIG.sonarr_servers: |         for server in CONFIG.sonarr_servers: | ||||||
|  |             SONARR = SonarrAPI(server, CONFIG.influx_server) | ||||||
|             if server.queue: |             if server.queue: | ||||||
|                 schedule.every(server.queue_run_seconds).seconds.do(threaded, SONARR.get_queue) |                 schedule.every(server.queue_run_seconds).seconds.do(threaded, SONARR.get_queue) | ||||||
|             if server.missing_days > 0: |             if server.missing_days > 0: | ||||||
|                 schedule.every(server.missing_days_run_seconds).seconds.do(threaded, SONARR.get_missing, |                 schedule.every(server.missing_days_run_seconds).seconds.do(threaded, SONARR.get_missing) | ||||||
|                                                                            server.missing_days) |  | ||||||
|             if server.future_days > 0: |             if server.future_days > 0: | ||||||
|                 schedule.every(server.future_days_run_seconds).seconds.do(threaded, SONARR.get_future, |                 schedule.every(server.future_days_run_seconds).seconds.do(threaded, SONARR.get_future) | ||||||
|                                                                           server.future_days) |  | ||||||
| 
 | 
 | ||||||
|     if CONFIG.tautulli_enabled: |     if CONFIG.tautulli_enabled: | ||||||
|         TAUTULLI = TautulliAPI(CONFIG.tautulli_servers, CONFIG.influx_server) |  | ||||||
| 
 |  | ||||||
|         for server in CONFIG.tautulli_servers: |         for server in CONFIG.tautulli_servers: | ||||||
|  |             TAUTULLI = TautulliAPI(server, CONFIG.influx_server) | ||||||
|             if server.get_activity: |             if server.get_activity: | ||||||
|                 schedule.every(server.get_activity_run_seconds).seconds.do(threaded, TAUTULLI.get_activity) |                 schedule.every(server.get_activity_run_seconds).seconds.do(threaded, TAUTULLI.get_activity) | ||||||
|             if server.get_sessions: |             if server.get_sessions: | ||||||
|                 schedule.every(server.get_sessions_run_seconds).seconds.do(threaded, TAUTULLI.get_sessions) |                 schedule.every(server.get_sessions_run_seconds).seconds.do(threaded, TAUTULLI.get_sessions) | ||||||
| 
 | 
 | ||||||
|     if CONFIG.radarr_enabled: |     if CONFIG.radarr_enabled: | ||||||
|         RADARR = RadarrAPI(CONFIG.radarr_servers, CONFIG.influx_server) |  | ||||||
| 
 |  | ||||||
|         for server in CONFIG.radarr_servers: |         for server in CONFIG.radarr_servers: | ||||||
|             if any([server.get_missing, server.get_missing_available]): |             RADARR = RadarrAPI(server, CONFIG.influx_server) | ||||||
|  |             if server.get_missing: | ||||||
|                 schedule.every(server.get_missing_run_seconds).seconds.do(threaded, RADARR.get_missing) |                 schedule.every(server.get_missing_run_seconds).seconds.do(threaded, RADARR.get_missing) | ||||||
|             if server.queue: |             if server.queue: | ||||||
|                 schedule.every(server.queue_run_seconds).seconds.do(threaded, RADARR.get_queue) |                 schedule.every(server.queue_run_seconds).seconds.do(threaded, RADARR.get_queue) | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     while True: |     while True: | ||||||
|         schedule.run_pending() |         schedule.run_pending() | ||||||
|         sleep(1) |         sleep(1) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue