############ # # GUILDS # ############ # TODO: vérifier la validité des serv/chan ids à chaque fois import decimal import json from cogs.utils import checks, commons, prefs, scores from collections import defaultdict from discord.enums import ChannelType from kyoukai import HTTPRequestContext, Kyoukai api = Kyoukai('dh_api') def json_default(obj): if isinstance(obj, decimal.Decimal): return float(obj) raise TypeError async def prepare_resp(resp_payload, code=200): return json.dumps(resp_payload, default=json_default), code, { "Content-Type": "application/json" } async def list_members(server_id, channel_id):
# HOUSEKEEPING import json import asyncio # RANDOM from random import choice from os import walk, path # KYOUKAI from kyoukai import Kyoukai, util from werkzeug import Response # ############################ app = Kyoukai(__name__) # Storing our files. global files # Make our files an empty list. files = [] # Run through all the files in the img folder. for (dirpath, dirnames, filenames) in walk('img/'): # Get all the filenames files.extend(filenames) # Beep break # ############################
app.config["owapi_cache_time"] = cache_time async def start(self, ctx): self.add_component('kyoukai', KyoukaiComponent, ip="127.0.0.1", port=4444, app="app:app", template_renderer=None) if app.config["owapi_use_redis"]: from asphalt.redis.component import RedisComponent self.add_component('redis', RedisComponent) else: logger.warning('redis is disabled by config, rate limiting and caching not available') await super().start(ctx) logger.info("Started OWAPI server.") app = Kyoukai("owapi") @app.route("/") async def root(ctx: HTTPRequestContext): raise RequestRedirect("https://github.com/SunDwarf/OWAPI/blob/master/api.md") @app.root.errorhandler(500) async def e500(ctx: HTTPRequestContext, exc: HTTPException): obb = { "error": 500, "msg": "please report this!", "exc": repr(exc.__cause__) } logger.error("Unhandled exception - Blizzard format probably changed!")
# Run with: python3 -O simple_server.py import asyncio import logging import ujson import uvloop from kyoukai import HTTPRequestContext, Kyoukai loop = uvloop.new_event_loop() asyncio.set_event_loop(loop) kyk = Kyoukai("example_app") logger = logging.getLogger("Kyoukai") logger.setLevel(logging.ERROR) @kyk.route("/") async def index(ctx: HTTPRequestContext): return ( ujson.dumps({"test": True}), 200, {"Content-Type": "application/json"}, ) kyk.run()
from kyoukai import Kyoukai from .util import jsonify from .models import Base from .api_v1 import api_v1 app = Kyoukai("bashhub") app.register_blueprint(api_v1) @app.route("/") async def index(ctx): return jsonify({"_": repr(ctx.dbsession)}) @app.route("/_create_all") async def _create_all(ctx): Base.metadata.create_all(ctx.sql) return "OK" @app.before_request async def before_request(ctx): print(repr(ctx.request.path)) print(repr(ctx.request.headers)) print(repr(ctx.request.body)) print("") return ctx
#!/bin/env python3 import asyncio import aiohttp import result_parser as rp from kyoukai import Kyoukai from kyoukai.util import as_json LOOP = asyncio.get_event_loop() SESSION = aiohttp.ClientSession(loop=LOOP) SEARCH_URI = 'https://www.google.com/searchbyimage?&image_url={}' HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'} app = Kyoukai('image_search', loop=LOOP) async def get_resp_obj(url): """ Gets the aiohttp response object for a given URL """ async with SESSION.get(url, headers=HEADERS) as r: return r async def get_resp_html(url): """ Gets the HTML response of a given URL """ async with SESSION.get(url, headers=HEADERS) as r: if r.status == 200: return await r.text() else: return None def is_image(r: aiohttp.ClientResponse):