File size: 2,313 Bytes
fd03f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests
import json
from dotenv import load_dotenv
import os

load_dotenv()

API_KEY = 'NEWSAPI_API_KEY'

# Functions -------------------------------------------------------------------------------------------------------------> 
def extract_article_info_from_list(article_list):   
    try:
        # Initialize a string to store the extracted information
        extracted_info = ""

        # Extract and store the desired information for each article
        for article in article_list:
            description = article.get("description", "N/A")
            title = article.get("title", "N/A")
            author = article.get("author", "N/A")
            content = article.get("content", "N/A")

            # Append the information to the string
            extracted_info += f"Title: {title}\nAuthor: {author}\nDescription: {description}\nContent: {content}\n\n"

        return extracted_info

    except Exception as e:
        # Handle any exceptions
        return f"An error occurred: {str(e)}"
      
def grab_articles(url, **kwargs):
    response = requests.get(url, 5)
    
    if response.status_code == 200:
        extracted_article = extract_article_info_from_list(response.json()['articles'])
        print(extracted_article)
        return extracted_article
    else:
        raise Exception(f"Error: {response.status_code}, {response.text}")

# URL Configuration Here -------------------------------------------------------------------------------------------------------------> 

BASE_URL_EVERYTHING = 'https://newsapi.org/v2/everything?'
BASE_URL_TOP_HEADLINES = 'https://newsapi.org/v2/top-headlines?'
TOPIC = 'bitcoin'
FROM = '2023-01-08' # Starting date
TO = '2023-11-08' # Ending date
SORTBY = 'popularity' # Relevancy, Popularity, PublishedAt
DOMAINS = 'techcrunch.com, thenextweb.com' # Domains you want to use. 
COUNTRY = 'gb&' # Options: US, AU, JP, PH etc. 
SOURCES = 'bbc-news' 
CATEGORY = 'business' # Options: business, entertainment, general health, science, sports, technology

# Everything URL list
urls_everything = f"{BASE_URL_EVERYTHING}q={TOPIC}&domains={DOMAINS}&sortBy={SORTBY}&apiKey={API_KEY}" 

# Top Headlines URL list
urls_top_headlines = f"{BASE_URL_TOP_HEADLINES}topic={TOPIC}&country={COUNTRY}&category={CATEGORY}&sources={SOURCES}&apiKey={API_KEY}"