Play images and video from Synology PhotoStation server

common.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. """
  3. kodiswift.common
  4. -----------------
  5. This module contains some common helpful functions.
  6. :copyright: (c) 2012 by Jonathan Beluch
  7. :license: GPLv3, see LICENSE for more details.
  8. """
  9. from __future__ import absolute_import
  10. import urllib
  11. try:
  12. import cPickle as pickle
  13. except ImportError:
  14. import pickle
  15. __all__ = ['clean_dict', 'kodi_url', 'unpickle_args', 'pickle_dict',
  16. 'unpickle_dict', 'download_page', 'Modes']
  17. class Modes(object):
  18. ONCE = 'ONCE'
  19. CRAWL = 'CRAWL'
  20. INTERACTIVE = 'INTERACTIVE'
  21. def kodi_url(url, **options):
  22. """Appends key/val pairs to the end of a URL. Useful for passing arbitrary
  23. HTTP headers to Kodi to be used when fetching a media resource, e.g.
  24. cookies.
  25. Args:
  26. url (str):
  27. **options (dict):
  28. Returns:
  29. str:
  30. """
  31. options = urllib.urlencode(options)
  32. if options:
  33. return url + '|' + options
  34. return url
  35. def clean_dict(data):
  36. """Remove keys with a value of None
  37. Args:
  38. data (dict):
  39. Returns:
  40. dict:
  41. """
  42. return dict((k, v) for k, v in data.items() if v is not None)
  43. def pickle_dict(items):
  44. """Convert `items` values into pickled values.
  45. Args:
  46. items (dict): A dictionary
  47. Returns:
  48. dict: Values which aren't instances of basestring are pickled. Also,
  49. a new key '_pickled' contains a comma separated list of keys
  50. corresponding to the pickled values.
  51. """
  52. ret = {}
  53. pickled_keys = []
  54. for k, v in items.items():
  55. if isinstance(v, basestring):
  56. ret[k] = v
  57. else:
  58. pickled_keys.append(k)
  59. ret[k] = pickle.dumps(v)
  60. if pickled_keys:
  61. ret['_pickled'] = ','.join(pickled_keys)
  62. return ret
  63. def unpickle_args(items):
  64. """Takes a dict and un-pickles values whose keys are found in a '_pickled'
  65. key.
  66. >>> unpickle_args({'_pickled': ['foo'], 'foo': ['I3%0A.']})
  67. {'foo': 3}
  68. Args:
  69. items (dict): A pickled dictionary.
  70. Returns:
  71. dict: Dict with values un-pickled.
  72. """
  73. # Technically there can be more than one _pickled value. At this point
  74. # we'll just use the first one
  75. pickled = items.pop('_pickled', None)
  76. if pickled is None:
  77. return items
  78. pickled_keys = pickled[0].split(',')
  79. ret = {}
  80. for k, v in items.items():
  81. if k in pickled_keys:
  82. ret[k] = [pickle.loads(val) for val in v]
  83. else:
  84. ret[k] = v
  85. return ret
  86. def unpickle_dict(items):
  87. """un-pickles a dictionary that was pickled with `pickle_dict`.
  88. Args:
  89. items (dict): A pickled dictionary.
  90. Returns:
  91. dict: An un-pickled dictionary.
  92. """
  93. pickled_keys = items.pop('_pickled', '').split(',')
  94. ret = {}
  95. for k, v in items.items():
  96. if k in pickled_keys:
  97. ret[k] = pickle.loads(v)
  98. else:
  99. ret[k] = v
  100. return ret
  101. def download_page(url, data=None):
  102. """Returns the response for the given url. The optional data argument is
  103. passed directly to urlopen.
  104. Args:
  105. url (str): The URL to read.
  106. data (Optional[any]): If given, a POST request will be made with
  107. :param:`data` as the POST body.
  108. Returns:
  109. str: The results of requesting the URL.
  110. """
  111. conn = urllib.urlopen(url, data)
  112. resp = conn.read()
  113. conn.close()
  114. return resp