python-lottie  0.7.0+dev66cafb9
A framework to work with lottie files and telegram animated stickers (tgs)
tgs.py
Go to the documentation of this file.
1 import io
2 import json
3 import gzip
4 from ..objects import Animation
5 
6 
7 def parse_tgs_json(file, encoding="utf-8"):
8  """!
9  Reads both tgs and lottie files, returns the json structure
10  """
11  return open_maybe_gzipped(file, json.load)
12 
13 
14 def open_maybe_gzipped(file, on_open, encoding="utf-8"):
15  if isinstance(file, str):
16  with open(file, "r", encoding=encoding) as fileobj:
17  return open_maybe_gzipped(fileobj, on_open)
18 
19  if isinstance(file, io.TextIOBase) and hasattr(file, "buffer"):
20  binfile = file.buffer
21  else:
22  binfile = file
23 
24  try:
25  binfile.seek(binfile.tell()) # Throws when not seekable
26  mn = binfile.read(2)
27  binfile.seek(0)
28  except (io.UnsupportedOperation, OSError):
29  mn = b''
30 
31  if mn == b'\x1f\x8b': # gzip magic number
32  final_file = gzip.open(binfile, "rb")
33  elif isinstance(file, io.TextIOBase):
34  final_file = file
35  else:
36  final_file = io.TextIOWrapper(file, encoding=encoding)
37 
38  return on_open(final_file)
39 
40 
41 def parse_tgs(filename, encoding="utf-8"):
42  """!
43  Reads both tgs and lottie files
44  """
45  lottie = parse_tgs_json(filename, encoding)
46  return Animation.load(lottie)
def open_maybe_gzipped(file, on_open, encoding="utf-8")
Definition: tgs.py:14
def parse_tgs(filename, encoding="utf-8")
Reads both tgs and lottie files.
Definition: tgs.py:41
def parse_tgs_json(file, encoding="utf-8")
Reads both tgs and lottie files, returns the json structure.
Definition: tgs.py:7