Neurelo Build Docs
Neurelo Build Docs
  • Introduction
    • Core Concepts
    • Key Features
  • Getting Started
    • Sign-in/Sign-up
    • Dashboard
      • Collapsible Sidebar
      • Light/Dark Mode
      • Account Settings
      • Audit Events
      • User Management
        • Permissions (Member v/s Admin)
      • Org Settings
    • Starting your Neurelo Project
      • Quick Start Guide
      • Step 1 - Add a Data Source
      • Step 2 - Build Definitions
      • Step 3 - Create an Environment
      • Step 4 - Create an API Key
      • Step 5 - Start Runners
      • Try your Neurelo APIs
  • "How to" Videos
    • Product Overview
    • Neurelo APIs & SDKs
    • Project Setup
    • Definitions
    • Environments
    • Data Sources
    • Organization Management
    • Creating and Using Custom Queries
    • Using the Schema Builder to build Relationships
    • Mock Data Generation
  • Definitions
    • Neurelo Schema Editor
      • Schema Builder
      • JSON/YAML Editor
      • Schema Visualization: Entity-Relationship Diagram (ERD)
    • Custom APIs for Complex Queries
      • Write and Commit Custom Queries
      • AI-Assisted Query Generation
      • Deploying Custom API Endpoints
      • Using Variables in your Custom Query
    • Branches and Commits
    • API Docs
  • Environments
    • Creating a new Environment
    • API Playground
    • Observability
    • Migrations
    • API Keys
  • Data Sources
    • PostgreSQL
    • MySQL
    • MongoDB
  • Guides
    • Provisioning Cloud Databases for using with Neurelo
      • PostgreSQL
        • AWS RDS (PostgreSQL)
      • MySQL
        • AWS RDS (MySQL)
      • MongoDB Atlas
    • Mock Data Generation
    • Wipe Data Source
    • Remote Git Repository for Definitions
      • Connecting a Remote Git Repo
      • Creating Commits from Neurelo
      • Syncing Branches
    • Data Viewer
    • Environment/Data Source Tags
    • How to work with Embedded documents and References in MongoDB
    • How to download and use the Postman Collection for your Project
    • Building Python applications with Postgres and FastAPI
    • CI Integration using Neurelo CLI
    • Schema Migrations
    • Schema AI Assist
    • Auto-Introspection
    • Access Policies
    • User Auth
      • Google
      • GitHub
      • GitLab
    • MongoDB Atlas - Migrate GraphQL to Neurelo
    • MongoDB Atlas - Migrate REST Data APIs to Neurelo
  • MongoDB Atlas - Migrate REST Data APIs to Neurelo
  • MongoDB Atlas - Migrate GraphQL APIs to Neurelo
  • Neurelo Schema Language (NSL)
    • Example 1 - DVD Rentals
    • Example 2 - Simple "Posts" App
    • Example 3 - Bookstore
  • Neurelo API Reference (REST)
    • Examples of Neurelo Auto-Generated REST API endpoints
      • Example 1 - Simple “Posts” application
      • Example 2 - "DVD Rentals" application
      • Example 3 - "Bookstore” application
      • cURL API Examples
  • Neurelo API Reference (GraphQL)
  • SDKs
    • TypeScript / JavaScript SDK
    • Go SDK
    • Python SDK
      • Python SDK Tutorial -- News Application
        • News Application using Neurelo’s Python SDKs
  • CLI (Preview Version)
  • Self-Hosted Neurelo Gateways
  • Tutorials
    • Building a Real Time Chat Application with Neurelo and MongoDB using Python
    • Building A Financial Terminal with Neurelo and MongoDB in Rust
    • Building a Restaurant Management System with Neurelo and MongoDB using GraphQL in just a few minutes
    • Bringing Neurelo’s Data APIs to Life Instantly with MySQL
  • Project Examples
  • References
    • Supported Databases
    • Supported OS and Browsers
  • Support
Powered by GitBook
On this page
  • feeds.json
  • fetch.py
  1. SDKs
  2. Python SDK
  3. Python SDK Tutorial -- News Application

News Application using Neurelo’s Python SDKs

feeds.json

{
  "trending": [
    "https://feeds.bbci.co.uk/news/rss.xml",
    "https://www.cbc.ca/webfeed/rss/rss-topstories"
  ]
}

fetch.py

import feedparser
import scrapy
from scrapy.crawler import CrawlerProcess
import json
from newspaper import fulltext
import requests
from dotenv import load_dotenv
import os
import argparse

from neurelo.configuration import Configuration
from neurelo.api_client import ApiClient
from neurelo.api.news_api import NewsApi

scraped = {}

def start_scrapy(urls):
    process = CrawlerProcess(settings={"FEED_FORMAT": "json", "FEED_URI": "items.json"})
    process.crawl(Scrape, kwargs={"urls": urls})
    process.start()

class Scrape(scrapy.Spider):
    def __init__(self, **kwargs):
        self.name = ""
        self.urls = kwargs["kwargs"]["urls"]

    def start_requests(self):
        for url in self.urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        try:
            scraped[response.request.meta["redirect_urls"][0]] = response.text
        except:
            scraped[response.url] = response.text

class Parse:
    def __init__(self):
        pass

    def fetch_feeds(self):
        feeds = json.load(open("./feeds.json"))
        topics = list(feeds.keys())
        return (feeds, topics)

    def parse_rss_feeds(self, feeds, topics, limit=5):
        content = {}
        need_to_scrape = []
        index = 0
        for topic in topics:
            for feed in feeds[topic]:
                fetched = feedparser.parse(feed)
                for news in fetched.entries[:limit]:
                    content[index] = {}
                    content[index]["title"] = news["title"]
                    content[index]["link"] = news["link"]
                    content[index]["published"] = news["published"]
                    content[index]["category"] = topic
                    need_to_scrape.append(news["link"])
                    index += 1
        return (content, need_to_scrape)

    def get_news(self, rss_content):
        for index in rss_content.keys():
            feed_url = rss_content[index]["link"]
            scraped_news = fulltext(scraped[feed_url], language="en")
            rss_content[index]["summary"] = scraped_news
        return rss_content

    def parse(self):
        rss_feeds, topics = self.fetch_feeds()
        content, need_to_scrape = self.parse_rss_feeds(rss_feeds, topics)
        start_scrapy(need_to_scrape)
        return self.get_news(content)

class News:
    def __init__(self) -> None:
        host, key = self.env()
        self.api_client = self.conf(host, key)

    def env(self):
        load_dotenv()
        return (os.getenv("NEURELO_API_HOST") or "", os.getenv("NEURELO_API_KEY") or "")

    def conf(self, host, key):
        configuration = Configuration(host, api_key={"ApiKey": key})
        return ApiClient(configuration=configuration)

    def fetch(self):
        news_api = NewsApi(self.api_client)
        news_item = news_api.find_news()
        return news_item

    def store(self):
        parse = Parse()
        news = parse.parse()
        articles = list(news.values())

        news_api = NewsApi(self.api_client)
        response = news_api.create_many_news(articles)
        return response

    def delete(self):
        news_api = NewsApi(self.api_client)
        deleted = news_api.delete_news()
        return deleted

if __name__ == "__main__":
    news = News()
    print(news.fetch())
PreviousPython SDK Tutorial -- News ApplicationNextCLI (Preview Version)

Last updated 1 year ago