import cPickle as pickle

with open('classify.pkl', 'rb') as fp:
    content = pickle.load(fp)
    model, vect = content[0], content[1]

from newsapi import NewsApiClient

api_keys = [
    '4b4233b0e7c243ea8bdd9abf5a19bbbd', 'cb4e308246624d8188e9f254d721c345',
    '74b2cc200dcb4591b486369116c1cc41'
]
next_key_index = 0
newsapi = NewsApiClient(api_key=api_keys[next_key_index])
sources = 'the-hindu'  # bbc-news,fox-news,the-times-of-india,cnn,espn'

from preprocess import clean_text
from json import dumps
from crawler import crawl
from store import insert
from time import sleep


def fetch(page):
    result = newsapi.get_everything(sources=sources, language='en', page=page)
    articles = result['articles']
    for article in articles:
        if (not article['title']) or (not article['description']):
            continue
        values = [
            article['source']['name'], article['author'],
Exemple #2
0
from newsapi import NewsApiClient
newsapi = NewsApiClient(api_key='*****************')
top_headlines = newsapi.get_top_headlines(q='bitcoin',
                                          sources='bbc-news,the-verge',
                                          category='business',
                                          language='en',
                                          country='us')
sources = newsapi.get_sources()
def cnn():
    newsapi = NewsApiClient(api_key='fd4c0e4b873343a3a0a7b50168a89e9a')
    cnews = newsapi.get_top_headlines(sources='cnn', page_size=30)
    return jsonify(cnews)
from apikeyconstants import *
from newsapi import NewsApiClient

# Init
newsapi = NewsApiClient(api_key=NewsApiKeyMine)

# print(NewsApiKeyMine)
# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(q='bitcoin',
                                          sources='bbc-news,the-verge',
                                          language='en').text()
print(top_headlines)
# /v2/everything
all_articles = newsapi.get_everything(q='bitcoin',
                                      sources='bbc-news,the-verge',
                                      domains='bbc.co.uk,techcrunch.com',
                                      from_param='2017-12-01',
                                      to='2017-12-12',
                                      language='en',
                                      sort_by='relevancy',
                                      page=2).json()
print(all_articles)

# /v2/sources
sources = newsapi.get_sources()
print(sources)
Exemple #5
0
f = open("../keys/api.txt", "r")
keys = f.read()
f.close()
ACCESS_TOKEN = keys
# USE ACCESS_TOKEN = keys[:-1] if you are getting new line issue

#-------------------------------------------------------------------------------------------
#   PARMS SECTION
#-------------------------------------------------------------------------------------------

# READING IN
NewsFileNameArray = []  # A LIST OF FILENAMES
data = []  # ALL NEWS DATA FROM LOCAL NEWS JSON FILES

# REQUESTING NEW DATA
newsapi = NewsApiClient(api_key=ACCESS_TOKEN)  # INITIALISING newsapi object
news_keyname_array = [
    'bbc-news', 'abc-news', 'cnn', 'fox-news', 'independent', 'mirror',
    'metro', 'daily-mail', 'Theguardian.com', 'Sky.com', 'the-new-york-times',
    'al-jazeera-english', 'reuters', 'the-hill', 'breitbart-news', 'the-verge',
    'the-huffington-post'
]
news_array = []

#-------------------------------------------------------------------------------------------
#   CREATE NEW FOLDER FOR TODAY IF NOT EXIST
#-------------------------------------------------------------------------------------------
# GET TODAYS DATE
today = date.today()
print("Today's date:", today)
Exemple #6
0
class NewsGather:
    def __init__(self,
                 api_key_news=None,
                 api_key_rapid=None,
                 database_path=None,
                 json_path=None,
                 pickle_path=None,
                 verbose=False):
        self.verbose = verbose
        if api_key_news is None and api_key_rapid is None:
            print("No keys provided, fail")
            exit(1)
        if api_key_news is None:
            print("NO API_NEWS_KEY results only from rapid")
        else:
            self.news = NewsApiClient(api_key_news)
        if api_key_rapid is None:
            print("NO API_RAPID_KEY results only from newsapi")
        else:
            self.api_key_rapid = api_key_rapid
        self.database_path = database_path
        if self.database_path is not None:
            self.database = True
            self.database_setup()
        else:
            self.database = False
        self.json_path = json_path
        self.pickle_path = pickle_path

    # Taken from stack overflow
    @staticmethod
    def date_range(start_date, end_date):
        for n in range(int((end_date - start_date).days)):
            yield start_date + timedelta(n)

    @staticmethod
    def date_to_string(date):
        return date.strftime("%Y-%m-%d")

    @staticmethod
    def source_to_name(results):
        for article in results['articles']:
            article['source'] = article['source']['name']
        return results

    def search_date_range(self,
                          query,
                          start_date,
                          end_date,
                          search_everywhere,
                          pages=10,
                          database=False,
                          json_f=True,
                          pickle_f=False):
        for day in self.date_range(start_date, end_date):
            if self.verbose:
                print(f"Searching within date range {start_date}, {end_date}."
                      f"  At day {day}. Ending at day {day + timedelta(1)}")
            self.search_to_output(query,
                                  search_everywhere=search_everywhere,
                                  start_date=day,
                                  end_date=day + timedelta(1),
                                  database=database,
                                  json_f=json_f,
                                  pickle_f=pickle_f,
                                  pages=pages)

    def search_top(self, query):
        if self.verbose:
            print(f"Searching the top news headlines for {query}.")
        results = self.source_to_name(
            self.news.get_top_headlines(q=query, language='en', country='us'))
        return results

    def search_everywhere(self,
                          query,
                          start_time,
                          end_time=datetime.datetime.now(),
                          pages=10):
        results = dict()
        for i in range(1, pages):
            if self.verbose:
                print(
                    f"Searching in page {i} in newsapi-python, for query {query}."
                )
            result = self.news.get_everything(
                q=query,
                from_param=self.date_to_string(start_time),
                to=self.date_to_string(end_time),
                language='en',
                sort_by='relevancy')
            results.update(result)
        return self.source_to_name(results)

    def search_rapid(self,
                     query,
                     api_key_rapid=None,
                     pages=10,
                     start_date=None,
                     end_date=None):
        if api_key_rapid is not None:
            self.api_key_rapid = api_key_rapid
        conn = http.client.HTTPSConnection(
            "contextualwebsearch-websearch-v1.p.rapidapi.com")

        headers = {
            'x-rapidapi-host':
            "contextualwebsearch-websearch-v1.p.rapidapi.com",
            'x-rapidapi-key': self.api_key_rapid
        }

        result = []

        for i in range(1, 10):
            if self.verbose:
                print(f"Searching in page {i} in rapid, for query {query}.")
            if start_date is not None and end_date is not None:
                conn.request(
                    "GET",
                    f"/api/Search/NewsSearchAPI?fromPublishedDate={start_date}&toPublishedDate={end_date}"
                    f"&autoCorrect=false&pageNumber={i}&pageSize={pages}&q={query}&safeSearch=false",
                    headers=headers)
            else:
                conn.request(
                    "GET",
                    f"/api/Search/NewsSearchAPI?autoCorrect=false&pageNumber={i}"
                    f"&pageSize={pages}&q={query}&safeSearch=false",
                    headers=headers)

            res = conn.getresponse()
            results = res.read().decode('utf-8')
            results = json.loads(self.replacer(results))['value']
            results = self.rapid_list_fix(results)
            result.append(results)
        return result

    def search_to_output(self,
                         query,
                         search_everywhere=False,
                         start_date=None,
                         end_date=datetime.datetime.now(),
                         database=False,
                         json_f=False,
                         pickle_f=False,
                         pages=10,
                         database_path=None):
        if not database and not json_f and not pickle_f:
            return
        else:
            if self.api_key_rapid is None and self.news is None:
                print("FAILED TO HAVE NEWS API")
                print("Please input an api key")
                return
            else:
                if self.news is not None:
                    if search_everywhere:
                        results = self.search_everywhere(query,
                                                         start_date,
                                                         end_date,
                                                         pages=pages)
                    else:
                        results = self.search_top(query)
                    self.output(query,
                                results,
                                database,
                                json_f,
                                pickle_f,
                                rapid=False,
                                database_path=database_path)
                if self.api_key_rapid is not None:
                    results = self.search_rapid(query, pages=pages)
                    self.output(query,
                                results,
                                database,
                                json_f,
                                pickle_f,
                                rapid=True,
                                database_path=database_path)

    def output(self,
               query,
               results,
               database,
               json_f,
               pickle_f,
               database_path,
               rapid=False):
        if database:
            if self.database_path is None:
                self.set_database_path(database_path)
            if self.database_path is None:
                print("FAILED TO SETUP DATABASE PATH")
            else:
                if not rapid:
                    self.to_database_news(query, results, self.database_path)
                else:
                    self.to_database_rapid(query, results, self.database_path)
        if json_f:
            self.to_json(results)
        if pickle_f:
            self.to_pickle(results)

    def set_database_path(self, database_path):
        if database_path is not None:
            self.database_path = database_path

    def to_database_news(self, query, results, database_path=None):
        if self.database is False:
            self.database_setup(database_path)
        self.set_database_path(database_path)
        if self.verbose:
            print("Connect to database, placing documents in table.")
        connection = sqlite3.connect(self.database_path)
        cursor = connection.cursor()
        for result in results['articles']:
            cursor.execute(
                '''
            INSERT OR IGNORE INTO documents(source, query, author, title, description, url, url_to_image, published_at, content)
            VALUES(?,?,?,?,?,?,?,?,?)''', [
                    result['source'], query, result['author'], result['title'],
                    result['description'], result['url'], result['urlToImage'],
                    result['publishedAt'], result['content']
                ])
        connection.commit()
        cursor.close()

    def to_database_rapid(self, query, results, database_path=None):
        if self.database is False:
            self.database_setup(database_path)
        self.set_database_path(database_path)
        if self.verbose:
            print("Connect to database, placing documents in table.")
        connection = sqlite3.connect(self.database_path)
        cursor = connection.cursor()
        results = results[0]
        for result in results:
            cursor.execute(
                '''
            INSERT OR IGNORE INTO documents(source, query, author, title, description, url, url_to_image, published_at, content)
            VALUES(?,?,?,?,?,?,?,?,?)''', [
                    result['provider'], query, None, result['title'],
                    result['description'], result['url'], result['image'],
                    result['datePublished'], result['body']
                ])
        connection.commit()
        cursor.close()

    def database_setup(self, database_path=None):
        self.set_database_path(database_path)
        if self.database_path is None:
            print('FAILED AT CREATION OF DATABASE')
            print('NO PATH GIVEN')
            return
        if self.verbose:
            print(f"Setting up database {self.database_path}")
        connection = sqlite3.connect(self.database_path)
        cursor = connection.cursor()
        cursor.execute('''
                CREATE TABLE IF NOT EXISTS documents(
                id integer PRIMARY KEY AUTOINCREMENT,
                source TEXT,
                query TEXT,
                author TEXT,
                title TEXT,
                description TEXT,
                url TEXT,
                url_to_image TEXT,
                published_at TEXT NOT NULL,
                content BLOB
                )''')
        cursor.close()
        self.database = True

    def grab_from_documents_table(self, database_path=None):
        self.set_database_path(database_path)
        if self.database_path is None:
            print("FAILED AT CONNECTION OF DATABASE")
            return
        if self.verbose:
            print(f"Connecting database {self.database_path}")
        connection = sqlite3.connect(self.database_path)
        cursor = connection.cursor()
        cursor.execute('''
        SELECT id, description, content, title
        FROM documents;''')
        content = cursor.fetchall()
        return content

    def to_master_content(self, contents_table):

        content_without_id = list()

        for content in contents_table:
            content = [content[1], content[2], content[3]]
            content_without_id.append(content)

        content_without_id = self.to_single_content(content_without_id)

        content_with_id = list()

        for i in range(len(contents_table)):
            content_with_id.append(
                [contents_table[i][0], content_without_id[i]])

        return content_with_id

    @staticmethod
    def to_single_content(contents_table):
        space = " "
        for content in contents_table:
            content = re.sub(r'\n', '', space.join(content))
        return contents_table

    def to_json(self, results):
        if self.verbose:
            print("Placing data into json file.")
        results = self.source_to_name(results)
        with open(self.json_path, 'w') as file:
            json.dump(results, file)

    def to_pickle(self, results):
        if self.verbose:
            print("Placing data into pandas dataframe pickle file.")
        results = self.source_to_name(results)
        articles = results['articles']
        data = pd.DataFrame.from_dict(articles)
        data.to_pickle(self.pickle_path)

    @staticmethod
    def replacer(data):
        data = re.sub(r'\\n+', ' ', data)
        data = re.sub(r'\s+', ' ', data)
        data = re.sub(r'</*b>', '', data)
        return data

    @staticmethod
    def rapid_list_fix(results):
        for result in results:
            result['provider'] = result['provider']['name']
            result['image'] = result['image']['url']
        return results
Exemple #7
0
from flask import Flask, request, jsonify
from newsapi import NewsApiClient

""" 
    Enquiring NEWS API from this localhost server
    @author Dawen
"""

app = Flask(__name__)

API_KEY ='3498f3618263477a88f516f01aa24d00'
newsapi = NewsApiClient(api_key=API_KEY)

# GET Topheadlines from News API based on category
@app.route("/top-headlines/<category>")
def get_topheadlines_from_newsAPI(category):
    print(category)
    top_headlines = newsapi.get_top_headlines( category='business',
                                          language='en',
                                          country='us')
    return top_headlines

# GET all English News sources from News API
@app.route("/sources")
def get_sources():
    print("in get_sources")
    sources = newsapi.get_sources(language='en')
    return sources

    
# running web app in local machine
Exemple #8
0
from flask import Flask,render_template
from flask import *
import pickle
import pandas as pd
import numpy as np
from textblob import TextBlob
#nltk.download('punkt')
#nltk.download('brown')
from newsapi import NewsApiClient 
newsapi=NewsApiClient(api_key='932e630a539a47308e1cef5d6eb05ed6')


app=Flask(__name__)
@app.route("/")
def home():
    data = newsapi.get_top_headlines(language='en',country="in", page_size=10)
    l1=[]
    l2=[]
    for i in data['articles']:
        l1.append(i['title'])
        l2.append(i['url'])
    return render_template("home.html",l1=l1,l2=l2)      #template
@app.route("/about")
def about():
    return render_template("about.html")
@app.route("/new1")
def new1():
    return render_template("new1.html")    #news predictor
@app.route('/accepted', methods=['GET','POST'])
def accepted():
    if request.method=="POST":
Exemple #9
0
import re  #оставлено чтобы помнить про некорректный  пользовательский ввод
import pprint
from newsapi import NewsApiClient

newsapi = NewsApiClient(
    api_key='11a7d4ea5ca44a99838403e265a62c16')  # с безопасностью всё плохо...


def input_language():
    try:
        input_language = int(
            input(
                'Выберете язык \n 1 (Enter) - Русский (ru) \n 2 - Английский (en) \n'
            ))
        if input_language == 1:
            language = 'ru'
        elif input_language == 2:
            language = 'en'
    except:
        language = 'ru'
    return language


def input_page():
    input_page = int(input('Укажите номер страницы: '))
    return input_page


def input_country():
    #разорбать эту каку
    try:
Exemple #10
0
#! /usr/bin/env python

from newsapi import NewsApiClient
import sqlite3
from envious import load_env
import os

os.environ['ENV_FILE'] = os.path.dirname(os.path.abspath(__file__)) + '/.env'
load_env()
api_key = os.getenv('API_KEY')

newsapi = NewsApiClient(api_key=api_key)


### DATABASE INSERTS ###
def sql_insert(source, author, title, description, url, urlToImage,
               publishedAt, content, news_type, location):

    try:
        sqliteConnection = sqlite3.connect('/opt/news_archive/news/db/db.db')
        cursor = sqliteConnection.cursor()
        print("Successfully Connected to SQLite")

        sqlite_insert_query = """INSERT INTO us_news
                                                 ('source', 'author', 'title', 'description', 'url', 'urlToImage', 'publishedAt', content, news_type, location)
                                                  VALUES
                                                 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""

        count = cursor.execute(
            sqlite_insert_query,
            (source, author, title, description, url, urlToImage, publishedAt,
import secrets  # file that contains API authorizations
from newsapi import NewsApiClient
import sqlite3

conn = sqlite3.connect("finalproj.db")
cur = conn.cursor()

client_key = secrets.TWITTER_API_KEY
client_secret = secrets.TWITTER_API_SECRET_KEY
access_token = secrets.TWITTER_ACCESS_TOKEN
access_token_secret = secrets.TWITTER_ACCESS_TOKEN_SECRET

yelp_api_key = secrets.YELP_API_KEY
yelp_client_key = secrets.YELP_CLIENT_KEY

newsapi = NewsApiClient(api_key=secrets.NEWS_API_KEY)

# pass object along with requests.get() request and parameters
oauth = OAuth1(client_key,
               client_secret=client_secret,
               resource_owner_key=access_token,
               resource_owner_secret=access_token_secret)

# dictionary of yahoo woeids for 10 major cities in america. Will need to use this for twitter api query
# woeid api is no longer supported by yahoo, so manual look up plus adding to dictionary is fastest way to build this in.
woeid_dictionary = {
    'new york': 2459115,
    'los angeles': 2442047,
    'chicago': 2379574,
    'houston': 2424767,
    'phoenix': 2471390,
from newsapi import NewsApiClient
import pyttsx3
from datetime import datetime, timedelta
import pyttsx3

# Initiate the new reading engine
conversionEngine = pyttsx3.init()
voices = conversionEngine.getProperty('voices')
conversionEngine.setProperty('rate', 160)
conversionEngine.setProperty('voice', voices[0].id)
newsapi = NewsApiClient(api_key='Put your key here')  #Register at newsapi.org

top_headlines = newsapi.get_top_headlines(q='COVID-19',
                                          sources='bbc-news',
                                          language='en',
                                          page=2)

for item in top_headlines['articles']:
    conversionEngine.say(item['description'])
    conversionEngine.runAndWait()
    conversionEngine.end()

# all_articles = newsapi.get_everything(q='COVID-19',
#                                       sources='bbc-news,the-verge',
#                                       domains='bbc.co.uk,CNN.com',
#                                       from_param=(datetime.now() - timedelta(1)).strftime('%Y-%m-%d'),
#                                       to=datetime.today().strftime('%Y-%m-%d'),
#                                       language='en',
#                                       sort_by='relevancy',
#                                       page=2)
Exemple #13
0
def feed_database():
    print("running background task")
    newsapi = NewsApiClient(api_key='39749670bdb648c889399bcbb436c09f')
    userProfiles = UserProfile.objects.all()
    print(userProfiles)

    if userProfiles.exists():
        for userProfile in userProfiles:
            latest_newsFeed = NewsFeed.objects.filter(
                user=userProfile).order_by("-published_at")[0]

            if latest_newsFeed.exists():
                # keyword
                try:
                    if userProfile.keywords is not None:
                        filtered_key_response = newsapi.get_top_headlines(
                            sources=str(userProfile.keywords))

                        for response in filtered_key_response['articles']:
                            print(response['source']['name'])
                            try:

                                print(
                                    dt.datetime.strptime(
                                        response['publishedAt'],
                                        "%Y-%m-%dT%H:%M:%SZ") > dt.datetime.
                                    strptime(latest_newsFeed.published_at,
                                             "%Y-%m-%dT%H:%M:%SZ"))

                                if dt.datetime.strptime(response['publishedAt'], "%Y-%m-%dT%H:%M:%SZ") > \
                                        dt.datetime.strptime(latest_newsFeed.published_at, "%Y-%m-%dT%H:%M:%SZ"):
                                    print("inserting : ",
                                          response['source']['name'])
                                    NewsFeed.objects.create(
                                        user=userProfile,
                                        headline=response['title'],
                                        thumbnail=response['urlToImage'],
                                        news_url=response['url'],
                                        source_of_news=response['source']
                                        ['name'],
                                        published_at=response['publishedAt'],
                                        country_of_news=userProfile.keywords,
                                        description=response['description'],
                                        content=response['content'],
                                    )

                                    try:
                                        keywords = userProfile.keywords.split(
                                            ",")
                                        for keyword in keywords:
                                            if keyword.strip() in str(response['title']) or \
                                                    keyword.strip() in str(response['description']) or \
                                                    keyword.strip() in str(response['content']):
                                                print("keyword match found")
                                                # send email to the user
                                                sendEmail(
                                                    userProfile.username,
                                                    keyword, userProfile.email)
                                    except:
                                        pass

                                else:
                                    pass

                            except Exception as ex:
                                print(ex)
                                print("Didn't find latest news")
                except Exception as ex:
                    print('Keyword Issue')
                    print(ex)
                    pass

                # Source
                try:
                    sourceChoices = SourceChoice.objects.filter(
                        user=userProfile)
                    if sourceChoices.exists():
                        for data in sourceChoices:
                            filtered_source_response = newsapi.get_top_headlines(
                                sources=str(data.source_name))

                            for response in filtered_source_response[
                                    'articles']:
                                print(response['source']['name'])
                                try:

                                    print(
                                        dt.datetime.strptime(
                                            response['publishedAt'],
                                            "%Y-%m-%dT%H:%M:%SZ") > dt.datetime
                                        .strptime(latest_newsFeed.published_at,
                                                  "%Y-%m-%dT%H:%M:%SZ"))

                                    if dt.datetime.strptime(response['publishedAt'], "%Y-%m-%dT%H:%M:%SZ") > \
                                            dt.datetime.strptime(latest_newsFeed.published_at, "%Y-%m-%dT%H:%M:%SZ"):
                                        print("inserting : ",
                                              response['source']['name'])
                                        NewsFeed.objects.create(
                                            user=userProfile,
                                            headline=response['title'],
                                            thumbnail=response['urlToImage'],
                                            news_url=response['url'],
                                            source_of_news=response['source']
                                            ['name'],
                                            published_at=response[
                                                'publishedAt'],
                                            country_of_news=data.source_name,
                                            description=response[
                                                'description'],
                                            content=response['content'],
                                        )

                                        try:
                                            keywords = userProfile.keywords.split(
                                                ",")
                                            for keyword in keywords:
                                                if keyword.strip() in str(response['title']) or \
                                                        keyword.strip() in str(response['description']) or \
                                                        keyword.strip() in str(response['content']):
                                                    print(
                                                        "keyword match found")
                                                    # send email to the user
                                                    sendEmail(
                                                        userProfile.username,
                                                        keyword,
                                                        userProfile.email)
                                        except:
                                            pass

                                    else:
                                        pass

                                except Exception as ex:
                                    print(ex)
                                    print("Didn't find latest news")

                    else:
                        pass
                except Exception as ex:
                    print('Source Issue')
                    print(ex)
                    pass

                # Country
                try:
                    countryChoices = CountryChoice.objects.filter(
                        user=userProfile)
                    if countryChoices.exists():
                        for data in countryChoices:
                            filtered_country_response = newsapi.get_top_headlines(
                                country=str(data.country_name))

                            for response in filtered_country_response[
                                    'articles']:
                                print(response['source']['name'])
                                try:

                                    print(
                                        dt.datetime.strptime(
                                            response['publishedAt'],
                                            "%Y-%m-%dT%H:%M:%SZ") > dt.datetime
                                        .strptime(latest_newsFeed.published_at,
                                                  "%Y-%m-%dT%H:%M:%SZ"))

                                    if dt.datetime.strptime(response['publishedAt'], "%Y-%m-%dT%H:%M:%SZ") > \
                                            dt.datetime.strptime(latest_newsFeed.published_at, "%Y-%m-%dT%H:%M:%SZ"):
                                        print("inserting : ",
                                              response['source']['name'])
                                        NewsFeed.objects.create(
                                            user=userProfile,
                                            headline=response['title'],
                                            thumbnail=response['urlToImage'],
                                            news_url=response['url'],
                                            source_of_news=response['source']
                                            ['name'],
                                            published_at=response[
                                                'publishedAt'],
                                            country_of_news=data.country_name,
                                            description=response[
                                                'description'],
                                            content=response['content'],
                                        )

                                        try:
                                            keywords = userProfile.keywords.split(
                                                ",")
                                            for keyword in keywords:
                                                if keyword.strip() in str(response['title']) or \
                                                        keyword.strip() in str(response['description']) or \
                                                        keyword.strip() in str(response['content']):
                                                    print(
                                                        "keyword match found")
                                                    # send email to the user
                                                    sendEmail(
                                                        userProfile.username,
                                                        keyword,
                                                        userProfile.email)
                                        except:
                                            pass

                                    else:
                                        pass

                                except Exception as ex:
                                    print(ex)
                                    print("Didn't find latest news")

                    else:
                        pass
                except Exception as ex:
                    print('Country Issue')
                    print(ex)
                    pass
            else:
                pass
    else:
        pass
Exemple #14
0
import json
import requests
import datetime as dt
from .models import *
from background_task import background

from newsfeed.settings import EMAIL_HOST_USER
from django.core.mail import send_mail

from newsapi import NewsApiClient

newsapi = NewsApiClient(api_key='76c50369dfc54cf393ba20de4543a681')


@background(schedule=60)
def feed_database():
    print("running background task")
    newsapi = NewsApiClient(api_key='39749670bdb648c889399bcbb436c09f')
    userProfiles = UserProfile.objects.all()
    print(userProfiles)

    if userProfiles.exists():
        for userProfile in userProfiles:
            latest_newsFeed = NewsFeed.objects.filter(
                user=userProfile).order_by("-published_at")[0]

            if latest_newsFeed.exists():
                # keyword
                try:
                    if userProfile.keywords is not None:
                        filtered_key_response = newsapi.get_top_headlines(
Exemple #15
0
from newsapi import NewsApiClient
from flask import Flask, render_template, request
from datetime import datetime

app = Flask(__name__)
YOUR_API_HERE = ("Enter your API key here ")
newsapi = NewsApiClient(api_key=YOUR_API_HERE)


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'GET':
        results = newsapi.get_everything(q='linux', page_size=10)
        results_1 = newsapi.get_everything(q='open source', page_size=10)
        results_2 = newsapi.get_everything(q='android', page_size=10)
        results = results['articles'] + results_1['articles'] + results_2[
            'articles']
        results.sort(key=lambda date: datetime.strptime(
            date['publishedAt'], '%Y-%m-%dT%H:%M:%SZ'),
                     reverse=True)
        return render_template("index.html", results=results)
    else:
        query = request.form['query']
        results = newsapi.get_everything(q=query, page_size=10)
        return render_template("index.html", results=results['articles'])


if __name__ == "__main__":
    app.run(debug=True)
Exemple #16
0
    '''CREATE TABLE if NOT EXISTS "users" ("id" INTEGER NOT NULL UNIQUE, "user_id" INTEGER NOT NULL 
                    UNIQUE, PRIMARY KEY("id" AUTOINCREMENT));''')
cursor.execute(
    '''CREATE TABLE if NOT EXISTS "categories" ("id" INTEGER NOT NULL UNIQUE, "user_id" INTEGER NOT NULL,
                    "news_categories" TEXT, PRIMARY KEY("id" AUTOINCREMENT));'''
)
cursor.execute(
    '''CREATE TABLE if NOT EXISTS "keywords" ("id" INTEGER NOT NULL UNIQUE, "user_id" INTEGER NOT NULL,
                    "news_keywords" TEXT, PRIMARY KEY("id" AUTOINCREMENT));''')

conn.commit()
conn.close()

bot = telebot.TeleBot('1709600970:AAG_3ZNii1IjT62hOpRgbjML_9_SGYCr9lI',
                      parse_mode=None)  #Убрать токен
api = NewsApiClient(api_key='6a674cae33ec44c885bafd2fbb060894')  # Убрать ключ

available_categories = ('business', 'entertainment', 'general', 'health',
                        'science', 'sports', 'technology')


@bot.message_handler(commands=['start'])
def send_welcome(message):
    bot.reply_to(
        message,
        f'Привет, {message.from_user.first_name}, я новостной бот, давай зарегестрируемся и посмотрим'
        f' новости!',
        reply_markup=keyboard1)


@bot.message_handler(commands=['help'])
Exemple #17
0
from flask import Flask, render_template, request
from newsapi import NewsApiClient

app = Flask(__name__)

yourAPIKEY = 'e35e468d3c6b4178a34ed70d92f68356'  # write your API key here

newsapi = NewsApiClient(api_key=yourAPIKEY)

#print(top_headlines['articles'])

#head_lines=[]

#for news in top_headlines['articles']:
#   head_lines.append(news['title'])


@app.route('/')
def home():
    #news =head_lines
    return render_template('index.html', news='')


@app.route('/results/', methods=['POST'])
def get_results():
    keyword = request.form['keyword']  #getting input from user

    news = newsapi.get_top_headlines(
        q=keyword,
        #sources='bbc-news,the-verge',#optional and you can change
        #category='business', #optional and you can change also
Exemple #18
0
import os
import uuid
from datetime import datetime
from dataclasses import dataclass, field
from typing import Dict
from newsapi import NewsApiClient
from pymongo.errors import ConnectionFailure
from newsapi.newsapi_exception import NewsAPIException

from model.model import Model
from model.errors import InvalidNewsApiError
from common.mongo_database import MongoDatabase

api = NewsApiClient(api_key=os.environ.get("GOOGLE_NEWS_API_KEY"))


@dataclass(eq=False)
class News(Model):
    stock_code: str
    articles: list = field(default_factory=list)
    last_updated: datetime = field(default_factory=datetime.utcnow)
    _id: str = field(default=uuid.uuid4().hex)

    def load_articles(self, query):
        try:
            response = api.get_everything(q=query + " stock news",
                                          language='en',
                                          sort_by='relevancy',
                                          page_size=10)

            # if response['status'] is not "ok":
Exemple #19
0
from flask import *
from newsapi import NewsApiClient

newsapi = NewsApiClient(api_key='049840520ee14a2f850a7cfabdcc52dd')

app = Flask(__name__)


@app.route("/")
def hee():
    return render_template("newsa.html")


@app.route("/weather1", methods=["POST", "GET"])
def hee1():
    keyword = request.form["keyword"]
    top_headlines = newsapi.get_top_headlines(
        q=keyword,
        #sources='bbc-news,the-verge',
        #category='business',
        language='en',
        country='in')
    articles = top_headlines["articles"]
    return render_template("newsa.html", result=articles)


if __name__ == '__main__':
    app.run()
from newsapi import NewsApiClient

# Api Key
apiKey = NewsApiClient(api_key='7890d476b1164948bba5ff4929b76eed')
import urllib.request
import json
import datetime
import sys
import socket
import time
import requests
from newsapi import NewsApiClient

newsapi = NewsApiClient(api_key='b30f535c6b624930bb419501ba02cece')


def getNews():
    top_headlines = newsapi.get_top_headlines(language='en', country='us')
    # url ='https://newsapi.org/v2/top-headlines?contry=us&apiKey=b30f535c6b624930bb419501ba02cece'
    # response = requests.get(url)
    # # r1 = json.load(response.read())
    # r1 = response.json()
    r1 = top_headlines
    # print(r1['articles'])
    # r2 = r1['articles']
    return r1['articles']
    # response = urllib.request.urlopen(url, timeout=30)
    # str_response = response.read().decode('utf-8')
    # string = json.loads(str_response)
    # return string


# def getBuses(route=''):
#
#     #Base URL for MARTA API
Exemple #22
0
class news:
    def __init__(self):
        from newsapi import NewsApiClient
        self.newsapi = NewsApiClient(
            api_key='ae88bd8bd9d248cdbfba80e514599b60')

        self.category_code = 0  #Initialized with general category
        self.article = 0  #Intialized with 1st article
        self.articles = self.headlines(categories[self.category_code])

        #Lock
        self.lock = threading.Lock()  #Initially pause is on

        #Pause button
        self.pause_key = 0

        kb = threading.Thread(target=self.listenKeyboard)
        kb.start()

    """
	 Return top 20 head lines for a particular category
	"""

    def headlines(self, keyword):
        top_headlines = self.newsapi.get_top_headlines(category=keyword,
                                                       language='en',
                                                       country='in')
        return top_headlines['articles']

    """
	 Keyboard listening callback function
	"""

    def on_press(self, key):
        #Clear the input flush
        while msvcrt.kbhit():
            msvcrt.getch()

        try:
            print('You pressed {0}'.format(key.char))
            if key.char == 'q':
                self.exit()
            elif key.char == 'r':
                self.repeat()

        except AttributeError:
            print('You pressed {0}'.format(key))
            if (key == keyboard.Key.space) and self.pause_key:
                self.pause_key = 0
                self.lock.release()
                #self.resume()
            elif key == keyboard.Key.space:
                self.lock.acquire()
                self.pause()
            elif (key == keyboard.Key.up):
                speak.change_speed(1)
            elif (key == keyboard.Key.down):
                speak.change_speed(-1)
            elif (key == keyboard.Key.right):
                self.skip()
            elif (key == keyboard.Key.left):
                self.rewind()
            elif (key == keyboard.Key.esc):
                self.exit()

    """
	 Listen to keyboard keys
	"""

    def listenKeyboard(self):
        # Collect events until released
        with keyboard.Listener(on_press=self.on_press) as listener:
            listener.join()

    """
	 Speak the text
	"""

    def speak(self, text):
        self.lock.acquire()
        if (len(text) == 0):
            speak.say('None')
        else:
            speak.say(text)
        self.lock.release()

    """
	  Pause task
	"""

    def pause(self):
        self.pause_key = 1
        speak.say('Pausing')

    """
	 Resume news listening task
	"""

    def resume(self):
        #Wait until pause is not false
        try:
            # Get articles
            self.articles = self.headlines(categories[self.category_code])

            while self.article < len(self.articles):
                #if on first article of category,then tell category
                if self.article == 0:
                    self.speak(categories[self.category_code] + ' news')
                a = self.article
                if (self.article == a):
                    self.speak('Title ' +
                               self.articles[self.article]['title'].encode(
                                   'ascii', 'ignore').decode('ascii'))
                if (self.article == a):
                    self.speak('Desciption ' +
                               self.articles[self.article]['description'].
                               encode('ascii', 'ignore').decode('ascii'))
                self.article += 1
            self.category_code = (self.category_code + 1) % 7
            #self.resume()

        except:
            speak.say("Some error")
            return

    """
	 Exit pdf Reading Task
	"""

    def exit(self):
        speak.say('Exiting news speaking task')
        #Clear the input flush
        while msvcrt.kbhit():
            msvcrt.getch()
        os._exit(1)

    """
	  Repeat the last line
	"""

    def repeat(self):
        self.lock.acquire()
        speak.say('Repeating')
        self.article -= 1
        if (self.article < 0):
            self.article = 0
        self.lock.release()

    """
	 Move 10 lines back
	"""

    def rewind(self):
        self.lock.acquire()
        speak.say('Rewinding')
        self.article -= 10
        if (self.article < 0):
            self.article = 0
            self.category_code = (self.category_code - 1 + 7) % 7
        self.lock.release()

    """
	 Skip current and move to next page
	"""

    def skip(self):
        self.lock.acquire()
        speak.say('Moving to next Category')
        self.article = 0
        self.category_code = (self.category_code + 1) % 7
        # Get articles
        self.articles = self.headlines(categories[self.category_code])
        self.lock.release()

    """
	 Reading news for user
	"""

    def news_reader(self):
        self.speak('Welcome to news listening task')
        self.resume()
Exemple #23
0
import datetime
import dateutil.parser
import multiprocessing
import os
import sys

from fake_news.neo4j_conn import KnowledgeGraph
from fake_news.process import process_news
from newsapi import NewsApiClient

# Init
NEO4J_HOST = os.getenv('NEO4J_HOST', default='bolt://localhost:7687')
NEO4J_USER = os.getenv('NEO4J_USER', default='neo4j')
NEO4J_PASS = os.getenv('NEO4J_PASS', default='')

newsapi = NewsApiClient(api_key='c438ac7b7042471294fdb21c35237fb0')


def _process_news(url, date):
    try:
        neo = KnowledgeGraph(NEO4J_HOST, NEO4J_USER, NEO4J_PASS)
        date = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ')
        news = process_news(url, date=date)
        #
        neo.update_kg(news)
        print(url)
        return news
    except Exception:
        pass

# TOKEN = '1123378208:AAFnDmS4CA5aRsJYHYfvm8e-s_E6YIRYxxo'
TOKEN = '1237103929:AAH-UDwFKEuS_NlZGJifnv2tavcTXsF0gg4'
import telegram
import time
import random
# import RPi.GPIO as GPIO
import csv
import datetime
import smtplib
from email.message import EmailMessage
from newsapi import NewsApiClient
import requests
from gtts import gTTS
newsapi = NewsApiClient(api_key='1cec50aadff94c538a68e0d626bdb1f4')

jokes_list = [
    "Q: What’s the difference between England and a tea bag? "
    "A: The tea bag stays in the cup longer.",
    "A dyslexic man walks into a bra.",
    "A man walks into a bar with a roll of tarmac under his arm and says: “Pint please… and one for the road.",
    "I went to the doctor the other day and said: “Have you got anything for wind?” So he gave me a kite.",
    "I went to the zoo the other day. There was only a dog in it – it was a shihtzu.",
    "Two fish in a tank. One says: “How do you drive this thing?”",
    "A woman gets on a bus with her baby.   The driver says “Ugh – that’s the ugliest baby I’ve ever seen!”   "
    "The woman walks to the back of the bus and sits down."
    " She says to the man next to her: “The driver just insulted me!”"
    "The man says: “You go up there and tell him off. Go on. I’ll hold your monkey for you.",
    "Two men walk into a bar.   The third one DUCKS",
    "Why was six afraid of 7?   Because 7 ate 9",
    "You can't trust atoms! They make up everything.",
    "Why do potatoes argue?  Because they can't see eye to eye!",
Exemple #25
0
 def __init__(self, apikey, update_interval_seconds=(60 * 60)):
     self.newsapi = NewsApiClient(apikey)
     self.update_interval = update_interval_seconds
     self.last_request = float('-inf')
     self.tags = set()
Exemple #26
0
            if t[1] == "NNP" or t[1] == "NNI" or t[1] == "NN":
                matches.append(t[0])
        return matches


# Main method, just run "python np_extractor.py"
#def main():
#    sentence = "Earth is dangerous."
#    np_extractor = NPExtractor(sentence)
#    result = np_extractor.extract()
#    print("This sentence is about: %s" % ", ".join(result))

#if __name__ == '__main__':
#    main()

news = NewsApiClient(api_key='fe1bbf9f375842fcbae8cf90741f7fa5')
headlines = news.get_top_headlines(sources='bbc-news')
#client_credentials_manager = SpotifyClientCredentials(client_id="da5a1895b29f46b8aab5d2007dba9b9c", client_secret="4c5154e4ae7c4223ab08fee6540a2600")

login = tkinter.Tk()
tkinter.Label(login, text="Spotify Username").grid(row=0)
tkinter.Label(
    login, text="Login to spotify, goto account overview for Username").grid(
        row=1, column=0, columnspan=3)

usernameBox = tkinter.Entry(login)
usernameBox.grid(row=0, column=1)
tkinter.Button(login, text='OK', command=login.quit).grid(row=0,
                                                          column=2,
                                                          sticky=tkinter.W,
                                                          pady=4)
def headline():
    print("headline called")
    newsapi = NewsApiClient(api_key='fd4c0e4b873343a3a0a7b50168a89e9a')
    top_headlines = newsapi.get_top_headlines(language='en', page_size=30)
    return jsonify(top_headlines)
Exemple #28
0
from indicoio import config as indico_config
import indicoio
from newsapi import NewsApiClient
from typing import List, Set, Dict, TypedDict
from nltk.tokenize import word_tokenize
from collections import defaultdict
import yaml


with open('config.yaml') as file:
    config = yaml.full_load(file)
    indico_config.api_key = config['keys']['indico']
    newsapi = NewsApiClient(api_key=config['keys']['newsapi'])


# Not using 'class' notation here due to reserved word 'id'
SourceInfo = TypedDict('SourceInfo', {'id': str, 'name': str})


class ArticleInfo(TypedDict):
    source: SourceInfo
    author: str
    title: str
    description: str
    url: str
    urlToImage: str
    publishedAt: str
    content: str


class SentimentArticleInfo(ArticleInfo):
def sources():
    newsapi = NewsApiClient(api_key='fd4c0e4b873343a3a0a7b50168a89e9a')
    mysources = newsapi.get_sources()
    return jsonify(mysources)
Exemple #30
0
def main():
    # STEP 1: Use https://www.alphavantage.co
    # When STOCK price increase/decreases by 5% between yesterday and the day before yesterday then print("Get News").
    response = requests.get(url=STOCK_API_URL, params=STOCK_PARAMETERS)
    data_dict = response.json()

    # Get latest two closing prices
    last_two_days = [
        value for key, value in data_dict["Time Series (Daily)"].items()
    ][:2]
    print(last_two_days)

    # Compare both close prices
    # try:
    one_day_closing_price = float(last_two_days[0]["4. close"])
    two_days_closing_price = float(last_two_days[1]["4. close"])
    # except KeyError:
    # print("The markets are closed.")
    # return

    difference = one_day_closing_price - two_days_closing_price
    five_percent = two_days_closing_price / 10
    percent_diff = round(difference / two_days_closing_price * 100, 2)

    print(difference, five_percent)
    if abs(difference) >= abs(five_percent):
        print("Get News!")

        is_up = percent_diff >= 0
        up_or_down = "🔻"
        if is_up:
            up_or_down = "🔺"

        print(
            f"There was a {percent_diff}% {up_or_down} in the stock value for "
            f"the {COMPANY_NAME}. date: {dt.datetime.today().date()}")

        # STEP 2: Use https://newsapi.org
        # Instead of printing ("Get News"), actually get the first 3 news pieces for the COMPANY_NAME.
        news_response = NewsApiClient(api_key=NEWS_API_KEY)
        top_headlines = news_response.get_top_headlines(
            q=COMPANY_NAME, language="en")["articles"][:3]
        print(top_headlines)

        # STEP 3: Use https://www.twilio.com Send a separate message with the percentage change and each article's
        # title and description to your phone number. Twilio Client
        twilio_client = Client(username=TWILIO_SID, password=TWILIO_AUTH_TOKEN)

        # Send messages
        for data in top_headlines:
            date = data["publishedAt"].split("T")
            message = twilio_client.messages.create(
                from_=TWILIO_PHONE_NUMBER,
                to=USER_PHONE_NUMBER,
                body=f"{STOCK}: {up_or_down}{percent_diff}%\n"
                f"Title: {data['title']}\n"
                f"Brief: {data['description']}\n"
                f"date: {date[0]}\n"
                f"url: {data['url']}")
            print(message.sid)
        # Optional: Format the SMS message like this:
        """