import os
import logging
from flask import Flask, request, jsonify, render_template
import requests

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__)

NEWS_API_KEY = os.environ['NEWS_API_KEY']
NEWS_API_URL = "https://newsapi.org/v2/top-headlines"

@app.route("/news", methods=["GET"])
def get_news():
    categories = request.args.getlist('category')
    country = request.args.get('country', default='us', type=str)
    all_headlines = []
    try:
        for category in categories:
            headlines = fetch_headlines(category.capitalize(), country)
            all_headlines.extend(headlines)
        return jsonify(all_headlines), 200
    except requests.exceptions.RequestException as e:

About this template

The News API app allows users to select news categories and receive a list of 10 headlines from those categories. Made by Barandev[github.com/barandev]

Introduction to the News API App Template

Welcome to the News API App template! This template is designed to help you create a web application that fetches and displays news headlines based on user-selected categories. It uses Flask, a lightweight web application framework in Python, to serve web pages and handle requests. The front end is built with HTML, CSS, and JavaScript, allowing for an interactive user experience. This step-by-step guide will walk you through the process of setting up and using this template on the Lazy platform.

Getting Started

To begin using this template, click on "Start with this Template" in the Lazy Builder interface. This will pre-populate the code in the Lazy Builder, so you won't need to copy or paste any code manually.

Initial Setup: Adding Environment Secrets

Before you can use the News API App, you'll need to set up an environment secret for the NEWS_API_KEY. This key is required to authenticate requests to the News API service. Here's how to obtain and set up your NEWS_API_KEY:

  1. Visit https://newsapi.org/ and sign up for an API key.
  2. Once you have your API key, go to the Environment Secrets tab within the Lazy Builder.
  3. Create a new secret with the key as NEWS_API_KEY and paste your API key as the value.

This API key will be used by the application to fetch news headlines from the News API service.

Test: Deploying the App

With the NEWS_API_KEY set up, you're ready to deploy the app. Press the "Test" button in the Lazy Builder. This will start the deployment process and launch the Lazy CLI. The app will be deployed on the Lazy platform, and you won't need to worry about installing libraries or setting up the environment.

Using the App

Once the app is deployed, Lazy will provide you with a dedicated server link. Use this link to access the web interface of your News API App. Here's how to interact with the app:

  1. Open the provided server link in your web browser.
  2. You will see a form with checkboxes for different news categories such as General, Business, Entertainment, etc.
  3. Select the categories you're interested in and click the "Get News" button.
  4. The app will display a list of the top 10 headlines for each selected category.

The front end will make requests to the Flask backend, which in turn fetches news from the News API and returns it to the front end for display.

Integrating the App

If you wish to integrate this News API App into another service or front end, you can use the server link provided by Lazy as the base URL for your API requests. Here's a sample request you might make to the app's API:


GET /news?category=general&category=business

And here's a sample response you might receive:


[
  {
    "title": "Breaking News: Market sees unprecedented growth",
    "source": "Financial Times",
    "publishedAt": "2023-04-01T12:00:00Z",
    "category": "Business"
  },
  // More articles...
]

Use this API to fetch news headlines and integrate them into your own applications or services.

By following these steps, you should now have a fully functional News API App running on the Lazy platform. Enjoy delivering the latest news to your users!

Technology
Last published
July 26, 2024

More templates like this

SecureUserAuthenticator

Develop a secure User Authentication system for users to register, log in, and manage their profiles, laying the foundation for user-specific data management and permissions in the CMS.

Laravel
Python
Flask
Javascript

Simple Multiplayer Telegram game

This app is a simple frontend for a game where users can upvote and downvote the most popular word in their country, learn about the flags of other countries, and view what other people voted for on a leaderboard.

Telegram
Python
Javascript

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.

Python
Flask
Home
/
News API