Skip to content

Commit

Permalink
Add functionality to accept either Wikipedia article URL or the title…
Browse files Browse the repository at this point in the history
… of article
  • Loading branch information
vishalsundar-2 committed Nov 21, 2024
1 parent 29c4679 commit d3cafc0
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, Query
import uvicorn
from uvicorn import run
from typing import Union
import uvicorn
import requests
from pydantic import BaseModel
from bs4 import BeautifulSoup
Expand All @@ -12,8 +12,39 @@
class ArticleResponse(BaseModel):
article: str

def get_wikipedia_url(title: str) -> str:
"""Get the Wikipedia article URL for a given title using the Wikipedia API."""
api_url = 'https://en.wikipedia.org/w/api.php'
params = {
'action': 'query',
'format': 'json',
'titles': title,
'prop': 'info',
'inprop': 'url',
}
response = requests.get(api_url, params=params)
data = response.json()
pages = data.get('query', {}).get('pages', {})
page = next(iter(pages.values()), None)

if not page or 'missing' in page:
raise HTTPException(status_code=404, detail="Wikipedia article not found.")

fullurl = page.get('fullurl')
if not fullurl:
raise HTTPException(status_code=404, detail="Wikipedia article URL not found.")

return fullurl

@app.get("/get_article", response_model=ArticleResponse)
def get_article(url: str):
def get_article(url: str = Query(None), title: str = Query(None)):
if not url and not title:
raise HTTPException(status_code=400, detail="Either 'url' or 'title' must be provided.")

if title:
# Use the Wikipedia API to get the URL from the title
url = get_wikipedia_url(title)

try:
page = requests.get(url)
page.raise_for_status() # Check if the request was successful
Expand All @@ -36,6 +67,7 @@ class Url(BaseModel):
class Comparator(BaseModel):
source:str
target:str

@app.post("/api/v1/article/original/")
def get_orginal_article_by_url(url:Url):
return 'hello'
Expand Down

0 comments on commit d3cafc0

Please sign in to comment.