-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
141 lines (119 loc) · 3.84 KB
/
main.py
File metadata and controls
141 lines (119 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import io
import json
import os
import zipfile
import requests
from converter.render import render_file
headers = {
'authority': 'graphql.lottiefiles.com',
'accept': '*/*',
'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'authorization': 'Bearer ory_st_Nlt5qRyux32UAbHZqm3mYT1f9m8UNKSf',
'cache-control': 'no-cache',
'client-name': '@lottiefiles/workflow-web',
'client-version': '5.3.0',
'content-type': 'application/json',
'origin': 'https://app.lottiefiles.com',
'pragma': 'no-cache',
'referer': 'https://app.lottiefiles.com/',
'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
}
def get_schema():
query = """
{
__schema {
types {
name
fields {
name
type {
name
kind
}
}
}
}
}
"""
response = requests.post('https://graphql.lottiefiles.com/2022-08', headers=headers, json={'query': query})
with open('__schema.json', 'w', encoding='utf-8') as file:
file.write(response.text)
def get_info(url):
if 'animation' not in url:
print('Incorrect url')
return None
animation_ID = url.split('/')[-1]
query = """
query getFile($id: ID!) {
file(id: $id) {
name
fileObject {
key
url
filename
attributes {
contentLength
contentType
}
metadata {
height
width
frames
frameRate
layers
}
}
}
}
"""
json_data = {
'query': query,
'variables': {
'id': animation_ID
}
}
response = requests.post('https://graphql.lottiefiles.com/2022-08', headers=headers, json=json_data)
data = json.loads(response.text)
if not data.get('data', {}):
if 'errors' in data:
message = data.get('errors', [{}])[0].get('message')
location = data.get('errors', [])[0]
print(f"Failed to get response: {message}\nError: {location}")
print("Data not found")
return None
animation_info = data.get('data', {}).get('file', {})
if not animation_info:
print("Information not found")
return None
file = animation_info.get('fileObject', {})
return dict(
key=file.get('key'),
name=animation_info.get('name'),
url=file.get('url'),
filename=file.get('filename'),
metadata=file.get('metadata'),
extesnion='json'
) if file else {}
def get_json_animation(data):
json_filename = f"animations/{data.get('key')}.json"
url = data.get('url')
response = requests.get(url, headers=headers)
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
with archive.open(json_filename) as json_file:
return json_file.read().decode("utf-8")
def littie2video(url, filename='output.mp4'):
animation_info = get_info(url)
animation_data = io.StringIO(get_json_animation(animation_info))
downloads_dir = os.path.join(os.path.dirname(__file__), 'Downloads')
if not os.path.exists(downloads_dir):
os.makedirs(downloads_dir)
output = os.path.join(downloads_dir, filename)
render_file(animation_data, output, animation_info)
if __name__ == '__main__':
littie2video('https://app.lottiefiles.com/animation/1a51fd78-a8c1-4720-b3ae-7745a553427f', 'animation_1.mp4')