| 324 | |
| 325 | |
| 326 | class Stream(object): |
| 327 | def __init__(self, session_id=None, user_id=None, username=None, tautulli=None, session=None): |
| 328 | self.state = None |
| 329 | self.ip_address = None |
| 330 | self.session_id = session_id |
| 331 | self.user_id = user_id |
| 332 | self.username = username |
| 333 | self.session_exists = False |
| 334 | self.tautulli = tautulli |
| 335 | |
| 336 | if session is not None: |
| 337 | self._set_stream_attributes(session) |
| 338 | |
| 339 | def _set_stream_attributes(self, session): |
| 340 | for k, v in session.items(): |
| 341 | setattr(self, k, v) |
| 342 | |
| 343 | def get_all_stream_info(self): |
| 344 | """Get all stream info from Tautulli.""" |
| 345 | session = self.tautulli.get_activity(session_id=self.session_id) |
| 346 | if session: |
| 347 | self._set_stream_attributes(session) |
| 348 | self.session_exists = True |
| 349 | else: |
| 350 | self.session_exists = False |
| 351 | |
| 352 | def terminate(self, message=''): |
| 353 | """Calls Tautulli to terminate the session. |
| 354 | |
| 355 | Parameters |
| 356 | ---------- |
| 357 | message : str |
| 358 | The message to use if the stream is terminated. |
| 359 | """ |
| 360 | self.tautulli.terminate_session(session_id=self.session_id, message=message) |
| 361 | |
| 362 | def terminate_long_pause(self, message, limit, interval): |
| 363 | """Kills the session if it is paused for longer than <limit> seconds. |
| 364 | |
| 365 | Parameters |
| 366 | ---------- |
| 367 | message : str |
| 368 | The message to use if the stream is terminated. |
| 369 | limit : int |
| 370 | The number of seconds the session is allowed to remain paused before it |
| 371 | is terminated. |
| 372 | interval : int |
| 373 | The amount of time to wait between checks of the session state. |
| 374 | """ |
| 375 | start = datetime.now() |
| 376 | checked_time = 0 |
| 377 | # Continue checking 2 intervals past the allowed limit in order to |
| 378 | # account for system variances. |
| 379 | check_limit = limit + (interval * 2) |
| 380 | |
| 381 | while checked_time < check_limit: |
| 382 | self.get_all_stream_info() |
| 383 |
no outgoing calls
no test coverage detected