mirror of
https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
synced 2025-10-30 10:22:34 +00:00
Create Ghost Profile python scripts
Signed-off-by: Matthew Stidham <stidmatt@gmail.com>
This commit is contained in:
76
py-dashboard/GhostRequest.py
Normal file
76
py-dashboard/GhostRequest.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
# Class holds default settings for json requests to Grafana -
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] != 3:
|
||||
print("This script requires Python 3")
|
||||
exit()
|
||||
|
||||
import requests
|
||||
|
||||
import jwt
|
||||
from datetime import datetime as date
|
||||
|
||||
|
||||
class GhostRequest:
|
||||
def __init__(self,
|
||||
_ghostjson_host,
|
||||
_ghostjson_port,
|
||||
_api_token=None,
|
||||
_headers=dict(),
|
||||
_overwrite='false',
|
||||
debug_=False,
|
||||
die_on_error_=False):
|
||||
self.debug = debug_
|
||||
self.die_on_error = die_on_error_
|
||||
self.ghostjson_url = "http://%s:%s/ghost/api/v3" % (_ghostjson_host, _ghostjson_port)
|
||||
self.data = dict()
|
||||
self.data['overwrite'] = _overwrite
|
||||
self.ghostjson_login = self.ghostjson_url + '/admin/session/'
|
||||
self.api_token = _api_token
|
||||
|
||||
|
||||
def create_post(self,
|
||||
title=None,
|
||||
text=None,
|
||||
tags=None,
|
||||
authors=None,
|
||||
status="published"):
|
||||
ghostjson_url = self.ghostjson_url + '/admin/posts/'
|
||||
datastore = dict()
|
||||
datastore['title'] = title
|
||||
if tags is not None:
|
||||
datastore['tags'] = tags
|
||||
if authors is not None:
|
||||
datastore['authors'] = authors
|
||||
datastore['html'] = text
|
||||
datastore['status'] = status
|
||||
post = dict()
|
||||
posts = list()
|
||||
datastore = dict()
|
||||
datastore['html'] = text
|
||||
datastore['title'] = title
|
||||
datastore['status'] = status
|
||||
posts.append(datastore)
|
||||
post['posts'] = posts
|
||||
|
||||
headers = dict()
|
||||
|
||||
# Split the key into ID and SECRET
|
||||
id, secret = self.api_token.split(':')
|
||||
|
||||
# Prepare header and payload
|
||||
iat = int(date.now().timestamp())
|
||||
|
||||
header = {'alg': 'HS256', 'typ': 'JWT', 'kid': id}
|
||||
payload = {
|
||||
'iat': iat,
|
||||
'exp': iat + 5 * 60,
|
||||
'aud': '/v3/admin/'
|
||||
}
|
||||
token = jwt.encode(payload, bytes.fromhex(secret), algorithm='HS256', headers=header)
|
||||
headers['Authorization'] = 'Ghost {}'.format(token)
|
||||
requests.post(ghostjson_url, json=post, headers=headers)
|
||||
87
py-scripts/ghost_profile.py
Executable file
87
py-scripts/ghost_profile.py
Executable file
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
NAME: ghost_profile.py
|
||||
PURPOSE: modify ghost database from the command line.
|
||||
SETUP: A Ghost installation which the user has admin access to.
|
||||
EXAMPLE: ./ghost_profile.py --article_text_file text.txt --title Test --authors Matthew --ghost_token SECRET_KEY --host 192.168.1.1
|
||||
|
||||
Matthew Stidham
|
||||
Copyright 2021 Candela Technologies Inc
|
||||
License: Free to distribute and modify. LANforge systems must be licensed.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import argparse
|
||||
|
||||
if sys.version_info[0] != 3:
|
||||
print("This script requires Python 3")
|
||||
exit(1)
|
||||
|
||||
if 'py-json' not in sys.path:
|
||||
sys.path.append(os.path.join(os.path.abspath('..'), 'py-json'))
|
||||
sys.path.append(os.path.join(os.path.abspath('..'), 'py-dashboard'))
|
||||
|
||||
from GhostRequest import GhostRequest
|
||||
from LANforge.lfcli_base import LFCliBase
|
||||
|
||||
class UseGhost(LFCliBase):
|
||||
def __init__(self,
|
||||
_ghost_token=None,
|
||||
host="localhost",
|
||||
port=8080,
|
||||
_debug_on=False,
|
||||
_exit_on_fail=False,
|
||||
_ghost_host="localhost",
|
||||
_ghost_port=2368, ):
|
||||
super().__init__(host, port, _debug=_debug_on, _exit_on_fail=_exit_on_fail)
|
||||
self.ghost_host = _ghost_host
|
||||
self.ghost_port = _ghost_port
|
||||
self.ghost_token = _ghost_token
|
||||
self.GP = GhostRequest(self.ghost_host, str(self.ghost_port), _api_token=self.ghost_token)
|
||||
|
||||
def create_post(self, title, text, tags, authors):
|
||||
return self.GP.create_post(title=title, text=text, tags=tags, authors=authors)
|
||||
|
||||
def create_post_from_file(self, title, file, tags, authors):
|
||||
text = open(file).read()
|
||||
return self.GP.create_post(title=title, text=text, tags=tags, authors=authors)
|
||||
|
||||
|
||||
def main():
|
||||
parser = LFCliBase.create_basic_argparse(
|
||||
prog='ghost_profile.py',
|
||||
formatter_class=argparse.RawTextHelpFormatter,
|
||||
epilog='''Manage Ghost Website''',
|
||||
description='''
|
||||
ghost_profile.py
|
||||
----------------
|
||||
Command example:
|
||||
./ghost_profile.py
|
||||
--ghost_token'''
|
||||
)
|
||||
optional = parser.add_argument_group('optional arguments')
|
||||
optional.add_argument('--ghost_token', default=None)
|
||||
optional.add_argument('--create_post', default=None)
|
||||
optional.add_argument('--article_text_file', default=None)
|
||||
|
||||
optional.add_argument('--ghost_port', help='Ghost port if different from 2368', default=2368)
|
||||
optional.add_argument('--ghost_host', help='Ghost host if different from localhost', default='localhost')
|
||||
optional.add_argument('--article_text')
|
||||
optional.add_argument('--article_tags', action='append')
|
||||
optional.add_argument('--authors', action='append')
|
||||
optional.add_argument('--title', default=None)
|
||||
args = parser.parse_args()
|
||||
|
||||
Ghost = UseGhost(_ghost_token=args.ghost_token,
|
||||
_ghost_port=args.ghost_port,
|
||||
_ghost_host=args.ghost_host)
|
||||
|
||||
if args.create_post is not None:
|
||||
Ghost.create_post(args.title, args.article_text, args.article_tags, args.authors)
|
||||
if args.article_text_file is not None:
|
||||
Ghost.create_post_from_file(args.title, args.article_text_file, args.article_tags, args.authors)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user