2020-07-05 18:33:58 +00:00
|
|
|
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-07-06 09:13:59 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
|
|
|
from kibicara.config import config
|
2020-07-05 18:33:58 +00:00
|
|
|
from kibicara.platforms.twitter.bot import spawner
|
|
|
|
from kibicara.platforms.twitter.model import Twitter
|
2020-07-06 09:13:59 +00:00
|
|
|
from kibicara.webapi.hoods import get_hood
|
|
|
|
from logging import getLogger
|
|
|
|
from sqlite3 import IntegrityError
|
2020-07-06 12:35:20 +00:00
|
|
|
from ormantic.exceptions import NoMatch
|
2020-07-06 09:13:59 +00:00
|
|
|
from peony.oauth_dance import get_oauth_token, get_access_token
|
|
|
|
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def get_twitter(twitter_id: int, hood=Depends(get_hood)):
|
|
|
|
try:
|
|
|
|
return await Twitter.objects.get(id=twitter_id, hood=hood)
|
|
|
|
except NoMatch:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
2020-07-05 18:33:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
2020-07-06 09:13:59 +00:00
|
|
|
twitter_callback_router = APIRouter()
|
|
|
|
|
|
|
|
|
2020-07-06 12:50:11 +00:00
|
|
|
@router.get('/')
|
|
|
|
async def twitter_read_all(hood=Depends(get_hood)):
|
|
|
|
return await Twitter.objects.filter(hood=hood).all()
|
|
|
|
|
|
|
|
|
|
|
|
@router.get('/{twitter_id}')
|
|
|
|
async def twitter_read(twitter=Depends(get_twitter)):
|
|
|
|
return twitter
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete('/{twitter_id}', status_code=status.HTTP_204_NO_CONTENT)
|
|
|
|
async def twitter_delete(twitter=Depends(get_twitter)):
|
|
|
|
spawner.stop(twitter)
|
|
|
|
await twitter.delete()
|
|
|
|
|
|
|
|
|
2020-07-06 09:13:59 +00:00
|
|
|
@router.post('/', status_code=status.HTTP_201_CREATED)
|
|
|
|
async def twitter_create(response: Response, hood=Depends(get_hood)):
|
|
|
|
try:
|
2020-07-06 14:11:05 +00:00
|
|
|
request_token = await get_oauth_token(
|
2020-07-08 11:22:53 +00:00
|
|
|
config['twitter']['consumer_key'],
|
|
|
|
config['twitter']['consumer_secret'],
|
2020-07-06 09:13:59 +00:00
|
|
|
callback_uri='http://127.0.0.1:8000/api/twitter/callback',
|
|
|
|
)
|
2020-07-06 14:11:05 +00:00
|
|
|
if request_token['oauth_callback_confirmed'] != 'true':
|
2020-07-06 09:13:59 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
|
2020-07-06 14:11:05 +00:00
|
|
|
twitter = await Twitter.objects.create(
|
|
|
|
hood=hood,
|
|
|
|
access_token=request_token['oauth_token'],
|
|
|
|
access_token_secret=request_token['oauth_token_secret'],
|
2020-07-06 09:13:59 +00:00
|
|
|
)
|
|
|
|
response.headers['Location'] = '%d' % twitter.id
|
|
|
|
return twitter
|
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
|
|
|
|
|
|
|
|
|
|
|
@twitter_callback_router.get('/callback')
|
|
|
|
async def twitter_read_callback(oauth_token: str, oauth_verifier: str):
|
|
|
|
try:
|
|
|
|
twitter = await Twitter.objects.filter(access_token=oauth_token).get()
|
|
|
|
access_token = await get_access_token(
|
2020-07-08 11:22:53 +00:00
|
|
|
config['twitter']['consumer_key'],
|
|
|
|
config['twitter']['consumer_secret'],
|
2020-07-06 09:13:59 +00:00
|
|
|
twitter.access_token,
|
|
|
|
twitter.access_token_secret,
|
|
|
|
oauth_verifier,
|
|
|
|
)
|
|
|
|
await twitter.update(
|
|
|
|
access_token=access_token['oauth_token'],
|
|
|
|
access_token_secret=access_token['oauth_token_secret'],
|
2020-07-06 12:35:20 +00:00
|
|
|
successful_verified=True,
|
2020-07-06 09:13:59 +00:00
|
|
|
)
|
|
|
|
spawner.start(twitter)
|
|
|
|
return []
|
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
2020-07-06 12:35:20 +00:00
|
|
|
except NoMatch:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|