Skip to content
Permalink
Browse files
first project update
  • Loading branch information
nocerae committed Apr 7, 2021
1 parent 0768e01 commit 38e8394053fb07612ac177f5b508dfc10e6cf15a
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 282 deletions.

Some generated files are not rendered by default. Learn more.

@@ -1,55 +1,54 @@
import requests # to get/download the data from the api
import json # parse the api data
# Reference: Python Requests Tutorial: Request Web Pages, Download Images, POST Data, Read JSON, and More
# https://www.youtube.com/watch?v=tb8gHvYlCFs&t=839s

"""
Class APIManager
import requests # to get/download the data from the news Api
import json # to decode the api response

Gestisce la comunicazione HTTP con le API che forniscono i dati per le newsletter
"""
Class APIManager
Handles HTTP communication with the API that provide data for the news service
"""

class APIManager:

"""
Method: getNews
Metodo: getNews
Invia una richiesta GET tramite la libreria requests alle API trovate online
- Costruisce il dizionario con i parametri della richiesta (header + querystring)
- Invia la richiesta con metodo GET all'url delle API (trovato online su rapidapi.com)
- decodifica la risposta json in un Dizionario e restituisce i campi d'interesse (titolo, corpo, url) della news
Sends a GET request via the requests library to the API
- Builds the dictionary with the request parameters (header + querystring)
- Sends the request with GET method to the API url (rapidapi.com)
- Decodes the json response in a dictionary and returns the fields of interest (title, body, url) of the news
"""


def getNews(self, category):

url = "https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/search/NewsSearchAPI" # url of the api

# I parametri che andiamo a fornire alle API per la nostra richiesta.
# Questi sono da trovare nella documentazione delle API
# The parameters of the API request
# Reference: rapidapi.com

querystring = {
"q": f"{category}", # query to follow, topic to get the about
"pageNumber": "1", # no of pages cna be change
"q": f"{category}", # news request based on category
"pageNumber": "1", # n. of pages can be changed
"pageSize": "1", # size of the page should limit to paragraphs
"autoCorrect": "true", # spelling checker
"fromPublishedDate": "null", # date init
"toPublishedDate": "null" # date end
"fromPublishedDate": "null", # initial date
"toPublishedDate": "null" # end date
}

headers = {
'x-rapidapi-key': "20c7edee10msh9b13ee9a5b2a529p1d3da9jsnbed33a1d0b94", # sending header
'x-rapidapi-host': "contextualwebsearch-websearch-v1.p.rapidapi.com" # hosting sender
}

# creating requests
# creating request
response = requests.request("GET", url, headers=headers, params=querystring)

responseText = response.text # leggiamo la risposta in JSON che ci ha dato il server (si trova nel campo text)
data = json.loads(responseText) # fornattiamo il JSON in un dizionario Python
responseText = response.text # reads the JSON response provided by API (found in the text field)
data = json.loads(responseText) # converts JSON response in a Python dictionary

# Estrai i dati dal dizionario data e ritorna i singoli campi
# Extracts data from the "data" dictionary and returns them
title = data['value'][0]['title']
body = data['value'][0]['body']
url = data['value'][0]['url']

0 comments on commit 38e8394

Please sign in to comment.