mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-20 13:29:37 +02:00
37 lines
1.6 KiB
Python
37 lines
1.6 KiB
Python
|
from rest_framework import viewsets
|
||
|
from rest_framework.decorators import action
|
||
|
from rest_framework.permissions import IsAuthenticated
|
||
|
from rest_framework.response import Response
|
||
|
import requests
|
||
|
|
||
|
class GenerateDescription(viewsets.ViewSet):
|
||
|
permission_classes = [IsAuthenticated]
|
||
|
|
||
|
@action(detail=False, methods=['get'],)
|
||
|
def desc(self, request):
|
||
|
name = self.request.query_params.get('name', '')
|
||
|
# un url encode the name
|
||
|
name = name.replace('%20', ' ')
|
||
|
print(name)
|
||
|
url = 'https://en.wikipedia.org/w/api.php?origin=*&action=query&prop=extracts&exintro&explaintext&format=json&titles=%s' % name
|
||
|
response = requests.get(url)
|
||
|
data = response.json()
|
||
|
data = response.json()
|
||
|
page_id = next(iter(data["query"]["pages"]))
|
||
|
extract = data["query"]["pages"][page_id]
|
||
|
if extract.get('extract') is None:
|
||
|
return Response({"error": "No description found"}, status=400)
|
||
|
return Response(extract)
|
||
|
@action(detail=False, methods=['get'],)
|
||
|
def img(self, request):
|
||
|
name = self.request.query_params.get('name', '')
|
||
|
# un url encode the name
|
||
|
name = name.replace('%20', ' ')
|
||
|
url = 'https://en.wikipedia.org/w/api.php?origin=*&action=query&prop=pageimages&format=json&piprop=original&titles=%s' % name
|
||
|
response = requests.get(url)
|
||
|
data = response.json()
|
||
|
page_id = next(iter(data["query"]["pages"]))
|
||
|
extract = data["query"]["pages"][page_id]
|
||
|
if extract.get('original') is None:
|
||
|
return Response({"error": "No image found"}, status=400)
|
||
|
return Response(extract["original"])
|