Plex plugin to to play various online streams (mostly Latvian).

request.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from __future__ import absolute_import
  2. from base64 import b64encode
  3. from ..packages.six import b
  4. ACCEPT_ENCODING = 'gzip,deflate'
  5. def make_headers(keep_alive=None, accept_encoding=None, user_agent=None,
  6. basic_auth=None, proxy_basic_auth=None, disable_cache=None):
  7. """
  8. Shortcuts for generating request headers.
  9. :param keep_alive:
  10. If ``True``, adds 'connection: keep-alive' header.
  11. :param accept_encoding:
  12. Can be a boolean, list, or string.
  13. ``True`` translates to 'gzip,deflate'.
  14. List will get joined by comma.
  15. String will be used as provided.
  16. :param user_agent:
  17. String representing the user-agent you want, such as
  18. "python-urllib3/0.6"
  19. :param basic_auth:
  20. Colon-separated username:password string for 'authorization: basic ...'
  21. auth header.
  22. :param proxy_basic_auth:
  23. Colon-separated username:password string for 'proxy-authorization: basic ...'
  24. auth header.
  25. :param disable_cache:
  26. If ``True``, adds 'cache-control: no-cache' header.
  27. Example::
  28. >>> make_headers(keep_alive=True, user_agent="Batman/1.0")
  29. {'connection': 'keep-alive', 'user-agent': 'Batman/1.0'}
  30. >>> make_headers(accept_encoding=True)
  31. {'accept-encoding': 'gzip,deflate'}
  32. """
  33. headers = {}
  34. if accept_encoding:
  35. if isinstance(accept_encoding, str):
  36. pass
  37. elif isinstance(accept_encoding, list):
  38. accept_encoding = ','.join(accept_encoding)
  39. else:
  40. accept_encoding = ACCEPT_ENCODING
  41. headers['accept-encoding'] = accept_encoding
  42. if user_agent:
  43. headers['user-agent'] = user_agent
  44. if keep_alive:
  45. headers['connection'] = 'keep-alive'
  46. if basic_auth:
  47. headers['authorization'] = 'Basic ' + \
  48. b64encode(b(basic_auth)).decode('utf-8')
  49. if proxy_basic_auth:
  50. headers['proxy-authorization'] = 'Basic ' + \
  51. b64encode(b(proxy_basic_auth)).decode('utf-8')
  52. if disable_cache:
  53. headers['cache-control'] = 'no-cache'
  54. return headers