Last active
April 29, 2021 01:01
-
-
Save rririanto/0fdcb7a6024acc53533c6d333afe1d9d to your computer and use it in GitHub Desktop.
Image to s3 ghost python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests # pip install requests | |
import jwt # pip install pyjwt | |
from datetime import datetime as date | |
import json | |
import os | |
CONTENT_API = "<YOUR CONTENT API>" | |
ADMIN_API = "<YOUR ADMIN API>" | |
API_URL = "<URL ENDPOINT>" | |
JSON_PATH = "<YOUR JSON PATH>" | |
# Admin API key goes here | |
key = ADMIN_API | |
# Split the key into ID and SECRET | |
id, secret = key.split(':') | |
def post_request(data): | |
iat = int(date.now().timestamp()) | |
header = {'alg': 'HS256', 'typ': 'JWT', 'kid': id} | |
payload = { | |
'iat': iat, | |
'exp': iat + 5 * 60, | |
'aud': '/v4/admin/' | |
} | |
# Create the token (including decoding secret) | |
token = jwt.encode(payload, bytes.fromhex(secret), | |
algorithm='HS256', headers=header) | |
# Make an authenticated request to create a post | |
url = API_URL + "/ghost/api/v4/admin/posts/" | |
headers = {'Authorization': 'Ghost {}'.format(token)} | |
body = {"posts": [data]} | |
r = requests.post(url, json=body, headers=headers) | |
print(r.status_code) | |
def initiate_data(file): | |
data = json.loads(file)['data'] | |
for dt in data['posts']: | |
feature_image = dt['feature_image'] | |
if feature_image: | |
feature_image = "endpoints3" + dt['feature_image'] | |
site_data = { | |
"feature_image": feature_image, | |
"id": dt['id'] | |
} | |
post_request(site_data) | |
if __name__ == "__main__": | |
get_data = open(JSON_PATH).read() | |
initiate_data(get_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment