Beispiel #1
0
class NewsApiClientTest(unittest.TestCase):
    def setUp(self):
        key = os.environ.get("news_api_secret")
        self.api = NewsApiClient(key)

    def test_api_top_headline(self):
        # Raise TypeError if Keyword/Phrase param is not of type str
        q = 0
        with self.assertRaises(TypeError):
            self.api.get_top_headlines(q=q)
Beispiel #2
0
def index(request):
    api_key = os.getenv('key')
    # api_key = os.environ.get('KEY')

    newsapi = NewsApiClient(api_key)
    top = newsapi.get_top_headlines(sources='techcrunch')

    l = top['articles']
    desc = []
    title = []
    img = []
    author = []
    url = []
    source = []

    for i in range(len(l)):
        f = l[i]
        title.append(f['title'])
        desc.append(f['description'])
        img.append(f['urlToImage'])
        author.append(f['author'])
        url.append(f['url'])
        source.append(f['source']['name'])

    mylist = zip(title, desc, img, url, author, source)

    return render(request, 'index.html', context={"mylist": mylist})
class News:
    '''
    External API Service for news feed
    '''
    api_key = "552022408c394577825bfd63f2d59a42"

    def __init__(self):
        self._news = NewsApiClient(api_key=self.api_key)

    def get_news(self, source: str) -> List[NewsInterface]:
        news = self._news.get_top_headlines(sources=source)
        if not news['articles']:
            return []
        else:
            articles = list()
            for article in news["articles"]:
                source = article.get("source", {}).get("name")
                articles.append(
                    NewsInterface(
                        title=article.get("title"),
                        description=article.get("description"),
                        url=article.get("url"),
                        source=source,
                        author=article.get("author"),
                        thumbnail_url=article.get("urlToImage"),
                    )
                )
            return articles
Beispiel #4
0
class NewsAPI:
    def __init__(self):
        self.api = NewsApiClient(api_key=apikey)

    def headlines(self, keyword=None, sources=None, country=None, category=None,  lang="en"):
        source_str = None
        if sources != None:
            source_str = ""
            for i in range(len(sources)-1):
                source_str += sources[i] + ","
            source_str += sources[len(sources)-1]
        top_headlines = self.api.get_top_headlines(q=keyword,
                                                   sources=source_str,
                                                   category=category,
                                                   language=lang,
                                                   country=country)
        return top_headlines['articles']

    def update_sources(self):
        sources = self.api.get_sources()['sources']
        db = open(database, 'w')
        print("Database opened for writing")
        json.dump(sources, db)
        db.close()
        print("Database closed for writing")

    def get_sources(self):
        print("Preparing news...")
        db = open(database, "r")
        print("Database opened for reading")
        sources = json.load(db)
        db.close()
        print("Database closed for reading")
        return sources
Beispiel #5
0
def index(request):

    newsapi = NewsApiClient(api_key="a59e5f24831a4322b535578654582973")
    topheadlines = newsapi.get_top_headlines(category='business', country='in')
    articles = topheadlines['articles']

    desc = []
    news = []
    img = []
    author = []
    publishedAt = []
    url = []

    for i in range(len(articles)):
        myarticles = articles[i]

        news.append(myarticles['title'])
        desc.append(myarticles['description'])
        img.append(myarticles['urlToImage'])
        author.append(myarticles['author'])
        publishedAt.append(myarticles['publishedAt'])
        url.append(myarticles['url'])

    mylist = zip(news[:3], desc, img, author, publishedAt, url)

    return render(request, 'accounts/index.html', context={"mylist": mylist})
Beispiel #6
0
def news_agg(request):
    newsapi = NewsApiClient(api_key='3fe5b067769946879821f0ee5afdab83')
    top_news = newsapi.get_top_headlines(sources='recode')
    print(len(top_news))
    articles = top_news['articles']

    desc = []
    news = []
    img = []
    url = []

    for i in range(len(articles)):

        myarticles = articles[i]

        desc.append(myarticles['description'])
        url.append(myarticles['url'])
        img.append(myarticles['urlToImage'])
        news.append(myarticles['title'])

        mylist = zip(url, img, desc, news)

    return render(request,
                  'base/news_aggregator.html',
                  context={'mylist': mylist})
Beispiel #7
0
 def get_top(self,
             fields=["image", "title", "image", "link"],
             limit=20,
             **kargs):
     newsapi = NewsApiClient(api_key=self.__key)
     tops = newsapi.get_top_headlines(**kargs)
     return self.__transformDate(tops.get("articles"), fields, limit)
Beispiel #8
0
def top_headlines(outputfile, query, sources, language, country, category):

    api_key = const.SAMPLE_API_KEY
    newsapi = NewsApiClient(api_key)
    try:
        headlines = newsapi.get_top_headlines(query,
                                              sources,
                                              language,
                                              country,
                                              category,
                                              page_size=100)
        if headlines['status'] == "ok":
            #print (headlines['totalResults'])

            sqlclient = SqlClient(const.DB_NAME)
            connection = sqlclient.create_connection()
            sqlclient.create_table(connection, const.TABLE)
            sqlclient.add_newsitem(connection, const.TABLE, headlines)

            #Export news to csv file
            sqlclient.export_to_csv(connection, const.TABLE, outputfile)
        else:
            print(headlines['message'])
    except NewsAPIException as inst:
        print("Error: " + inst.get_message())
    except Exception as inst:
        print(inst)
Beispiel #9
0
def index_page():
    global is_auth, current_user_logged_id
    if request.method == 'POST':
        if is_auth == 0:
            print(str(current_user_logged_id))
            return redirect(url_for('user_login'))
        if 'player' in request.form:
            print('Goes to All music Here with ' + str(is_auth) +
                  " and user id is " + str(current_user_logged_id))
            return redirect(url_for('all_music'))
        if 'profile' in request.form:
            return redirect(url_for('profile', u_id=current_user_logged_id))
        if 'logout' in request.form:
            return redirect(url_for('log_out'))
    news_client = NAC_client(api_key='f8fd249bda5f49c1b1ed79d08e99440d')
    try:
        all_news = news_client.get_top_headlines(q='music')
    except:
        all_news = None
    all_users = User.query.all()
    all_songs = Music.query.all()
    all_albums = Album.query.all()
    all_artists = Artist.query.all()
    dashboard_data = dashboard_wrapper(all_users, all_songs, all_albums,
                                       all_artists)
    news_list = []
    if all_news == None:
        news_list = 'No Internet Error Encountered while Fetching News'
    else:
        for news_obj in all_news['articles']:
            news_list.append(news_obj)
    return render_template("index.html",
                           news_data=news_list,
                           dash_data=dashboard_data,
                           is_auth=is_auth)
Beispiel #10
0
def topheadlines(request):
    newsapi = NewsApiClient(api_key='e714e075a7534f85b7e0bdfd2330c611')
    top_headlines = newsapi.get_top_headlines(category='business',
                                              language='en',
                                              country='us')
    #top_headlines = top_headlines['articles']

    return Response(top_headlines)
Beispiel #11
0
def get_news():

    api = '23e4c7e51a9a49d39dc4e7261305dd02'
    newsapi = NewsApiClient(api_key=api)
    top_headlines = newsapi.get_top_headlines(country='us',
                                              category='business',
                                              page_size=70,
                                              language='en')
    return top_headlines
Beispiel #12
0
 def get_news(self, src):
     NEWS_API = NewsApiClient(api_key=NEWS_KEY)
     self.all_news = NEWS_API.get_top_headlines(sources=src)
     self.articles = self.all_news['articles']
     rand_headline = random.randint(0, 9)
     self.headline = self.articles[rand_headline]
     self.source = self.headline['source']
     self.author = self.headline['author']
     self.news_name = self.source['name']
Beispiel #13
0
def news_update(n):
    load_dotenv()
    api_key = os.environ.get("NEWS_API_KEY")
    newsapi = NewsApiClient(api_key=api_key)
    top_headlines = newsapi.get_top_headlines(
        q="covid-19", language="en", country="ca", page_size=10
    )
    article = top_headlines["articles"]
    news = [html.H5("News about Covid-19 in Canada")] + [
        html.H6(html.A(i["title"], href=i["url"], target="_blank")) for i in article
    ]
    return news
def getInfo(query):
    #Key to access GoogleNews API
    query = query.lower()
    # query = urllib.parse.quote_plus(query)
    newsapi = NewsApiClient(api_key='edf0afe93d6644d198d8539e640134c9')
    # print(query)

    headlines = newsapi.get_top_headlines(q=query, language='en')
    # print(headlines)
    # headlines = newsapi.get_top_headlines(q=query, language='en')

    newsTitles = list()
    newsContent = list()
    newsSources = list()
    newsURL = list()
    # print("number of articles found = " + str(len(headlines['articles'])))

    #Adds all relevant information to separate lists
    numberOfArticles = len(headlines['articles'])
    if numberOfArticles > 5:
        numberOfArticles = 5
    for x in range(numberOfArticles):
        source = headlines['articles'][x]['source']['name']
        if source == "Google News" or source == "Reuters" or source == "Financial Times":
            print(source)
            # x -= 1
            continue
        newsTitles.append(headlines['articles'][x]['title'])
        newsContent.append(headlines['articles'][x]['content'])
        newsSources.append(headlines['articles'][x]['source']['name'])
        newsURL.append(headlines['articles'][x]['url'])

    if len(newsTitles) < 5:
        today = datetime.datetime.today()
        start_day = today - datetime.timedelta(days=1)
        headlines_all = newsapi.get_everything(q=query,
                                               from_param=str(start_day),
                                               to=str(today),
                                               language='en',
                                               sort_by='relevancy')
        for x in range(5 - len(newsTitles)):
            source = headlines_all['articles'][x]['source']['name']
            if source == "Google News" or source == "Reuters" or source == "Financial Times":
                print(source)
                # x -= 1
                continue
            newsTitles.append(headlines_all['articles'][x]['title'])
            newsContent.append(headlines_all['articles'][x]['content'])
            newsSources.append(headlines_all['articles'][x]['source']['name'])
            newsURL.append(headlines_all['articles'][x]['url'])

    return newsTitles, newsContent, newsSources, newsURL
def get_news_data(_category='general', _language='en', _country='us', _pagesize=100, _page=1):
    # set api key
    newsapi = NewsApiClient(api_key=config["news_api_key"])

    # get news data from the news api
    top_headlines = newsapi.get_top_headlines(
        category=_category,
        language=_language,
        country=_country,
        page_size=_pagesize,
        page=_page
    )
    return top_headlines
Beispiel #16
0
 def connect_to_news_api():
     newsapi = NewsApiClient(api_key='1fa3d77b9ae7460c833ef91fe447eca4'
                             )  # generated my own api key by registering
     country = "gb"
     category = "technology"
     top_titles = newsapi.get_top_headlines(category=category,
                                            language='en',
                                            country=country)
     top_titles = json_normalize(
         top_titles['articles'])  # top_headlines organised in json format
     print(top_titles)
     new_df = top_titles[["title", "url"
                          ]]  # grabbing each top titles' title and urls
     dic = new_df.set_index('title')['url'].to_dict()
     return dic
Beispiel #17
0
def get_cnn_news():

    newsapi = NewsApiClient(api_key='ff705166e5c34413962cabd6d1b3d66f')

    top_headlines = newsapi.get_top_headlines(sources='cnn')
    # print(top_headlines)

    news_data = {}  ### create dictionary naming as 'news_data'

    news_data['title'] = top_headlines['articles'][0]['title']
    news_data['description'] = top_headlines['articles'][0]['description']
    news_data['url'] = top_headlines['articles'][0]['url']
    news_data['image_url'] = top_headlines['articles'][0]['urlToImage']

    return news_data
Beispiel #18
0
def newsPaper(request):
    newsapi = NewsApiClient(api_key='573dab4634604cb0a5bc4a55de0f9e50')
    top = newsapi.get_top_headlines(sources='new-scientist')

    articles = top['articles']
    desc = []
    news = []
    img = []

    for i in range(len(articles)):
        newsInfo = articles[i]
        news.append(newsInfo['title'])
        desc.append(newsInfo['description'])
        img.append(newsInfo['urlToImage'])
    Newslist = zip(news, desc, img)

    return render(request, 'news.html', context={"Newslist": Newslist})
Beispiel #19
0
def news(request):

    newsapi = NewsApiClient(api_key='5ace4197601b4040a907e288f079f9d2')
    #top = newsapi.get_top_headlines(sources ='techcrunch')
    top = newsapi.get_top_headlines(sources='business-insider')
    l = top['articles']
    desc = []
    news = []
    img = []

    for i in range(len(l)):
        f = l[i]
        news.append(f['title'])
        desc.append(f['description'])
        img.append(f['urlToImage'])
    mylist = zip(news, desc, img)

    return render(request, 'news.html', context={"mylist": mylist})
Beispiel #20
0
def Index(request):
    newsapi = NewsApiClient(api_key="fa4ae8ffbcc04cc6bf11d166b9487b5a")
    topheadlines = newsapi.get_top_headlines(sources="the-times-of-india")

    articles = topheadlines['articles']

    desc = []
    news = []
    img = []

    for i in range(len(articles)):
        myarticles = articles[i]
        news.append(myarticles['title'])
        img.append(myarticles['description'])
        img.append(myarticles['urlToImage'])

    mylist = zip(news, desc, img)

    return render(request, 'index.html', context={"mylist": mylist})
Beispiel #21
0
    def getNews():
        news = ''

        newsapi = NewsApiClient(api_key='c2cca8b3de6844e3a0e789acf524f939')

        top_headlines = newsapi.get_top_headlines(
            q='',
            language='en',
            sources='business-insider, Mybroadband.co.za, Ewn.co.za')

        for article in top_headlines['articles']:
            newsTitle = article['title']
            newsDescription = article['description']
            newsUrl = article['url']
            #print('Title: ', article['title'])
            #print('Description: ', article['description'],'\n\n')
            #print(news)
            break
        return ('Title: ' + newsTitle + '\nDescription: ' + newsDescription +
                '\n' + newsUrl)
Beispiel #22
0
def top_headlines(country, topic):  
   newsapi = NewsApiClient(api_key="3d4657a3a0ff42db9243b2e01d8357b8")          
   top_headlines =newsapi.get_top_headlines(category=topic,
   language='en',country=country)     
   top_headlines=json_normalize(top_headlines['articles'])   
   newdf = top_headlines[["title","url"]]
   newdfBackup = newdf
   newdf = Preprocessing(newdf)

   PositiveHeadlines, NegativeHeadlines = ClassifyHeadlines(newdf, 120000, 28309, 29183)

   print(PositiveHeadlines, "\n--------------------------------\n", NegativeHeadlines)

   PositiveUrl = {}
   for i in PositiveHeadlines:
      PositiveUrl[newdfBackup.iloc[i]['title']] = newdfBackup.iloc[i]['url']

   print(PositiveUrl)

   return PositiveUrl
Beispiel #23
0
def News(request):
    newsapi = NewsApiClient(api_key='ad25ee8229784a7eaaaa655bccd78d20')
    top_headlines = newsapi.get_top_headlines(sources='the-times-of-india')
    articles = top_headlines['articles']
    dates = datetime.now().date()

    desc = []
    news = []
    img = []
    url = []

    for i in range(len(articles)):
        myarticles = articles[i]
        url.append(myarticles['url'])
        news.append(myarticles['title'])
        desc.append(myarticles['description'])
        img.append(myarticles['urlToImage'])

    mylist = zip(news, desc, img, url)
    return render(request, "Crona/news.html", context={"mylist": mylist, 'dates': dates})
Beispiel #24
0
def bbc():
    newsapi = NewsApiClient(api_key="460f54dfe2d749b49c9c9bdb2509e2d0")
    topheadlines = newsapi.get_top_headlines(sources="bbc-news")

    articles = topheadlines['articles']

    desc = []
    news = []
    img = []

    for i in range(len(articles)):
        myarticles = articles[i]

        news.append(myarticles['title'])
        desc.append(myarticles['description'])
        img.append(myarticles['urlToImage'])

    mylist = zip(news, desc, img)

    return render_template('bbc.html', context=mylist)
Beispiel #25
0
def wired(request):
    newsapi = NewsApiClient(api_key="b0f75ce660c0466a9a98c2478f8abb62")
    topheadlines = newsapi.get_top_headlines(sources='wired')

    articles = topheadlines['articles']

    desc = []
    news = []
    img = []
    ur = []

    for i in range(len(articles)):
        myarticles = articles[i]

        news.append(myarticles['title'])
        desc.append(myarticles['description'])
        img.append(myarticles['urlToImage'])
        ur.append(myarticles['url'])

    mylist = zip(news, desc, img, ur)

    return render(request, 'wired.html', context={"mylist": mylist})
Beispiel #26
0
def get_newsurls(limit=5, query=None):
    """Returns list of news urls from newsapi.org."""

    # initialise newsapi
    KEY = 'key'  # insert your newsapi key
    newsapi = NewsApiClient(api_key=KEY)

    results = list()
    if query is None:  # get top headlines for Singapore news
        top_headlines_results = newsapi.get_top_headlines(language='en',
                                                          country='sg')
        results.extend(top_headlines_results['articles'][:limit])

    else:  # get search results from a query
        search_results = newsapi.get_everything(
            q=query,
            language='en',
            domains='channelnewsasia.com, todayonline.com, straitstimes.com')
        results.extend(search_results['articles'][:limit])

    urls = [article['url'] for article in results]

    return urls
Beispiel #27
0
def customtopheadlines(request):
    json_content = []
    newsapi = NewsApiClient(api_key='e714e075a7534f85b7e0bdfd2330c611')
    top_headlines = newsapi.get_top_headlines(category='business',
                                              language='en',
                                              country='us')
    top_headlines = top_headlines['articles']
    for i in range(1, 2):
        news_object = top_headlines[i]
        results, temp, temp_2 = assign_roles(news_object['url'], 2.0, 0.25)
        custom_object = {
            "hero": results[0],
            "victim": results[1],
            "villian": results[2],
            "source": news_object["source"]["name"],
            "author": news_object["author"],
            "title": news_object['title'],
            "urlToImage": news_object["urlToImage"],
            "shortdescription": news_object['description'],
            "url": news_object['url'],
        }
        json_content.append(custom_object)

    return Response(json_content)
Beispiel #28
0
def alldatafunc():
    freq = {}
    fq = []
    resultfin = []
    cnnup = defaultdict(list)
    foxup = defaultdict(list)
    renderup = defaultdict(list)
    newsapi = NewsApiClient(api_key="d11761b89fdb4599b1497bf951690000")
    cnn = newsapi.get_top_headlines(sources="CNN", page_size=20)
    fox = newsapi.get_top_headlines(sources="FOX-News", page_size=20)
    render = newsapi.get_top_headlines(page_size=100)

    for i in cnn['articles']:
        if i['title'] is None or i['title'] == "" or i['title'] == "null" or i[
                'author'] is None or i['author'] == "" or i[
                    'author'] == "null" or i['description'] is None or i[
                        'description'] == "" or i['description'] == "null" or i[
                            'source'] is None or i['source'] == "" or i[
                                'source'] == "null" or i['url'] is None or i[
                                    'url'] == "" or i['url'] == "null" or i[
                                        'urlToImage'] is None or i[
                                            'urlToImage'] == "" or i[
                                                'urlToImage'] == "null":
            pass
        else:
            cnnup['articles'].append(i)

    for i in fox['articles']:
        if i['title'] is None or i['title'] == "" or i['title'] == "null" or i[
                'author'] is None or i['author'] == "" or i[
                    'author'] == "null" or i['description'] is None or i[
                        'description'] == "" or i['description'] == "null" or i[
                            'source'] is None or i['source'] == "" or i[
                                'source'] == "null" or i['url'] is None or i[
                                    'url'] == "" or i['url'] == "null" or i[
                                        'urlToImage'] is None or i[
                                            'urlToImage'] == "" or i[
                                                'urlToImage'] == "null":
            pass
        else:
            foxup['articles'].append(i)

    for i in render['articles']:
        if i['title'] is None or i['title'] == "" or i['title'] == "null" or i[
                'author'] is None or i['author'] == "" or i[
                    'author'] == "null" or i['description'] is None or i[
                        'description'] == "" or i['description'] == "null" or i[
                            'source'] is None or i['source'] == "" or i[
                                'source'] == "null" or i['url'] is None or i[
                                    'url'] == "" or i['url'] == "null" or i[
                                        'urlToImage'] is None or i[
                                            'urlToImage'] == "" or i[
                                                'urlToImage'] == "null":
            pass
        else:
            renderup['articles'].append(i)

    for i in render['articles']:
        fq.append(i['title'])
    fq1 = dict(Counter(" ".join(fq).lower().split()))

    stop_words = [
        'a', "a's", 'able', 'about', 'above', 'according', 'accordingly',
        'across', 'actually', 'after', 'afterwards', 'again', 'against',
        "ain't", 'all', 'allow', 'allows', 'almost', 'alone', 'along',
        'already', 'also', 'although', 'always', 'am', 'among', 'amongst',
        'an', 'and', 'another', 'any', 'anybody', 'anyhow', 'anyone',
        'anything', 'anyway', 'anyways', 'anywhere', 'apart', 'appear',
        'appreciate', 'appropriate', 'are', "aren't", 'around', 'as', 'aside',
        'ask', 'asking', 'associated', 'at', 'available', 'away', 'awfully',
        'b', 'be', 'became', 'because', 'become', 'becomes', 'becoming',
        'been', 'before', 'beforehand', 'behind', 'being', 'believe', 'below',
        'beside', 'besides', 'best', 'better', 'between', 'beyond', 'both',
        'brief', 'but', 'by', 'c', "c'mon", "c's", 'came', 'can', "can't",
        'cannot', 'cant', 'cause', 'causes', 'certain', 'certainly', 'changes',
        'clearly', 'co', 'com', 'come', 'comes', 'concerning', 'consequently',
        'consider', 'considering', 'contain', 'containing', 'contains',
        'corresponding', 'could', "couldn't", 'course', 'currently', 'd',
        'definitely', 'described', 'despite', 'did', "didn't", 'different',
        'do', 'does', "doesn't", 'doing', "don't", 'done', 'down', 'downwards',
        'during', 'e', 'each', 'edu', 'eg', 'eight', 'either', 'else',
        'elsewhere', 'enough', 'entirely', 'especially', 'et', 'etc', 'even',
        'ever', 'every', 'everybody', 'everyone', 'everything', 'everywhere',
        'ex', 'exactly', 'example', 'except', 'f', 'far', 'few', 'fifth',
        'first', 'five', 'followed', 'following', 'follows', 'for', 'former',
        'formerly', 'forth', 'four', 'from', 'further', 'furthermore', 'g',
        'get', 'gets', 'getting', 'given', 'gives', 'go', 'goes', 'going',
        'gone', 'got', 'gotten', 'greetings', 'h', 'had', "hadn't", 'happens',
        'hardly', 'has', "hasn't", 'have', "haven't", 'having', 'he', "he's",
        'hello', 'help', 'hence', 'her', 'here', "here's", 'hereafter',
        'hereby', 'herein', 'hereupon', 'hers', 'herself', 'hi', 'him',
        'himself', 'his', 'hither', 'hopefully', 'how', 'howbeit', 'however',
        'i', "i'd", "i'll", "i'm", "i've", 'ie', 'if', 'ignored', 'immediate',
        'in', 'inasmuch', 'inc', 'indeed', 'indicate', 'indicated',
        'indicates', 'inner', 'insofar', 'instead', 'into', 'inward', 'is',
        "isn't", 'it', "it'd", "it'll", "it's", 'its', 'itself', 'j', 'just',
        'k', 'keep', 'keeps', 'kept', 'know', 'knows', 'known', 'l', 'last',
        'lately', 'later', 'latter', 'latterly', 'least', 'less', 'lest',
        'let', "let's", 'like', 'liked', 'likely', 'little', 'look', 'looking',
        'looks', 'ltd', 'm', 'mainly', 'many', 'may', 'maybe', 'me', 'mean',
        'meanwhile', 'merely', 'might', 'more', 'moreover', 'most', 'mostly',
        'much', 'must', 'my', 'myself', 'n', 'name', 'namely', 'nd', 'near',
        'nearly', 'necessary', 'need', 'needs', 'neither', 'never',
        'nevertheless', 'new', 'next', 'nine', 'no', 'nobody', 'non', 'none',
        'noone', 'nor', 'normally', 'not', 'nothing', 'novel', 'now',
        'nowhere', 'o', 'obviously', 'of', 'off', 'often', 'oh', 'ok', 'okay',
        'old', 'on', 'once', 'one', 'ones', 'only', 'onto', 'or', 'other',
        'others', 'otherwise', 'ought', 'our', 'ours', 'ourselves', 'out',
        'outside', 'over', 'overall', 'own', 'p', 'particular', 'particularly',
        'per', 'perhaps', 'placed', 'please', 'plus', 'possible', 'presumably',
        'probably', 'provides', 'q', 'que', 'quite', 'qv', 'r', 'rather', 'rd',
        're', 'really', 'reasonably', 'regarding', 'regardless', 'regards',
        'relatively', 'respectively', 'right', 's', 'said', 'same', 'saw',
        'say', 'saying', 'says', 'second', 'secondly', 'see', 'seeing', 'seem',
        'seemed', 'seeming', 'seems', 'seen', 'self', 'selves', 'sensible',
        'sent', 'serious', 'seriously', 'seven', 'several', 'shall', 'she',
        'should', "shouldn't", 'since', 'six', 'so', 'some', 'somebody',
        'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhat',
        'somewhere', 'soon', 'sorry', 'specified', 'specify', 'specifying',
        'still', 'sub', 'such', 'sup', 'sure', 't', "t's", 'take', 'taken',
        'tell', 'tends', 'th', 'than', 'thank', 'thanks', 'thanx', 'that',
        "that's", 'thats', 'the', 'their', 'theirs', 'them', 'themselves',
        'then', 'thence', 'there', "there's", 'thereafter', 'thereby',
        'therefore', 'therein', 'theres', 'thereupon', 'these', 'they',
        "they'd", "they'll", "they're", "they've", 'think', 'third', 'this',
        'thorough', 'thoroughly', 'those', 'though', 'three', 'through',
        'throughout', 'thru', 'thus', 'to', 'together', 'too', 'took',
        'toward', 'towards', 'tried', 'tries', 'truly', 'try', 'trying',
        'twice', 'two', 'u', 'un', 'under', 'unfortunately', 'unless',
        'unlikely', 'until', 'unto', 'up', 'upon', 'us', 'use', 'used',
        'useful', 'uses', 'using', 'usually', 'uucp', 'v', 'value', 'various',
        'very', 'via', 'viz', 'vs', 'w', 'want', 'wants', 'was', "wasn't",
        'way', 'we', "we'd", "we'll", "we're", "we've", 'welcome', 'well',
        'went', 'were', "weren't", 'what', "what's", 'whatever', 'when',
        'whence', 'whenever', 'where', "where's", 'whereafter', 'whereas',
        'whereby', 'wherein', 'whereupon', 'wherever', 'whether', 'which',
        'while', 'whither', 'who', "who's", 'whoever', 'whole', 'whom',
        'whose', 'why', 'will', 'willing', 'wish', 'with', 'within', 'without',
        "won't", 'wonder', 'would', 'would', "wouldn't", 'x', 'y', 'yes',
        'yet', 'you', "you'd", "you'll", "you're", "you've", 'your', 'yours',
        'yourself', 'yourselves', 'z', 'zero'
    ]

    stop_char = ['-', '!', '@', '#', '$', '%', '^', '&', '*', '|', '+', '-']

    for i, v in fq1.items():
        if i not in stop_words:
            if i not in stop_char:
                freq[i] = int(v)

    result = dict(sorted(freq.items(), key=lambda t: t[1], reverse=True))
    flag = 0
    for key, val in result.items():
        flag += 1
        resultfin.append({"word": key, "size": val})
        if flag == 30:
            break

    masterdic = {}
    masterdic["cnnnews"] = cnnup
    masterdic["foxnews"] = foxup
    masterdic["rendernews"] = renderup
    masterdic["wordcloud"] = resultfin

    return jsonify(masterdic)
Beispiel #29
0
class Newsy:

    def __init__(self):

        self.root = tk.Tk()
        self.root.geometry("1920x1080")
        self.root.title("News App")

        self.newsapi = NewsApiClient(api_key=API_KEY)

        self.top_headlines = tk.StringVar()
        self.all_articles = tk.StringVar()

        self.query = None

        self.createAndDisplay()

    def exception_handler(func):

        def wrapper(self, *args, **kwargs):

            try:

                return func(self, *args, **kwargs)

            except:

                self.articles_list.config(
                    text=("There was error in processing your request."))

        return wrapper

    @exception_handler
    def createAndDisplay(self):

        self.articles_list = tk.Label(
            self.root, text="", font=('Helvetica', 15))
        self.articles_list.place(
            relx=0, rely=0.25, relheight='0.7', relwidth='1')

        title = tk.Label(self.root, text="News App", font=('Modern', 40))
        title.place(relx=0.6, rely=0.01)

        top_headlines = self.newsapi.get_top_headlines(
            language='en', country='in')
        all_articles = self.newsapi.get_everything(sources='bbc-news,the-verge', domains='bbc.co.uk,techcrunch.com',
                                                   language='en', sort_by='relevancy', page=5)

        top_headlines = [news['url'] for news in top_headlines['articles']]
        all_articles = [news['url'] for news in all_articles['articles']]

        drop = tk.OptionMenu(self.root, self.top_headlines, *top_headlines)
        drop.place(relx=0, rely=0)

        drop2 = tk.OptionMenu(self.root, self.all_articles, *all_articles)
        drop2.place(relx=0, rely=0.1)

        url_button = tk.Button(self.root, text="Select", bd=1,
                               activebackground="#ffee96", command=self.get_url)
        url_button.place(relx=0.8, rely=0)

        headline = tk.Button(self.root, text="India News", bd=1,
                             activebackground="#ffee96", command=self.get_headlines)
        headline.place(relx=0.3, rely=0.2)

        all_news = tk.Button(self.root, text="World News", bd=1,
                             activebackground="#ffee96", command=self.get_news)
        all_news.place(relx=0.4, rely=0.2)

        self.query = tk.Entry(self.root, font=('Courier', 10), bd=0)
        self.query.place(relx=0.5, rely=0.2)

        search = tk.Button(self.root, text="Search", bd=1,
                           activebackground="#ffee96", command=self.search_query)
        search.place(relx=0.65, rely=0.2)

    @exception_handler
    def get_url(self):

        webbrowser.open(self.top_headlines.get())

        webbrowser.open(self.all_articles.get())

    @exception_handler
    def get_headlines(self):

        articles = self.newsapi.get_top_headlines(
            language='en', country='in')

        self.articles_list.config(
            text=(" ".join([news['title'] + "\n" for news in articles['articles']])))

    @exception_handler
    def get_news(self):

        articles = self.newsapi.get_everything(sources='bbc-news,the-verge', domains='bbc.co.uk,techcrunch.com',
                                               language='en', sort_by='relevancy', page=5)

        self.articles_list.config(text=" ".join(
            [news['title'] + "\n" for news in articles['articles']]))

    @exception_handler
    def search_query(self):

        articles = self.newsapi.get_everything(
            q=self.query.get(), language='en')

        self.articles_list.config(
            text=(" ".join([news['title'] + "\n" for news in articles['articles']])))
class NewsApiClientTest(unittest.TestCase):
    def setUp(self):
        key = os.environ.get("news_api_secret")
        self.api = NewsApiClient(key)

    def test_api_top_headline(self):
        # Raise TypeError if Keyword/Phrase param is not of type str
        q = 0
        with self.assertRaises(TypeError):
            self.api.get_top_headlines(q=q)

        # Raise ValueError if sources param in not None and country param or category param is not None
        sources = "techcrunch"
        country = "us"
        category = "business"
        with self.assertRaises(ValueError):
            self.api.get_top_headlines(sources=sources,
                                       country=country,
                                       category=category)

        # Raise TypeError if sources param is not of type str
        sources = 0
        with self.assertRaises(TypeError):
            self.api.get_top_headlines(sources=sources)

        # Raise TypeError if language param is not of type str
        language = 0
        with self.assertRaises(TypeError):
            self.api.get_top_headlines(language=language)

        # Raise ValueError if language param is invalid
        language = "xx"
        with self.assertRaises(ValueError):
            self.api.get_top_headlines(language=language)

        # Raise TypeError if country param is not of type str
        country = 0
        with self.assertRaises(TypeError):
            self.api.get_top_headlines(country=country)

        # Raise ValueError if country param is invalid
        country = "xx"
        with self.assertRaises(ValueError):
            self.api.get_top_headlines(country=country)

        # Raises TypeError if category param is not of type str
        category = 0
        with self.assertRaises(TypeError):
            self.api.get_top_headlines(category=category)

        # Raises ValueError if category param is invalid
        category = "x0x"
        with self.assertRaises(ValueError):
            self.api.get_top_headlines(category=category)

        # Raises TypeError if page_size param is not an int
        page_size = "1"
        with self.assertRaises(TypeError):
            self.api.get_top_headlines(page_size=page_size)

        # Raises ValueError if page_size param is less than zero(0) or greater than 100
        page_size = -1
        with self.assertRaises(ValueError):
            self.api.get_top_headlines(page_size=page_size)

        page_size = 1000
        with self.assertRaises(ValueError):
            self.api.get_top_headlines(page_size=page_size)

        # Raises a TypeError is page param is not an int
        page = "1"
        with self.assertRaises(TypeError):
            self.api.get_top_headlines(page=page)

        # Raises a ValueError if page param is less than zero(0)
        page = -1
        with self.assertRaises(ValueError):
            self.api.get_top_headlines(page=page)

    def test_api_get_everything(self):
        # Raise TypeError if Keyword/Phrase param is None
        q = 0
        with self.assertRaises(TypeError):
            self.api.get_everything(q=q)

        # Raise TypeError if sources param is not of type str
        sources = 0
        with self.assertRaises(TypeError):
            self.api.get_everything(sources=sources)

        # Raise TypeError is domains param is not of type str
        domains = 0
        with self.assertRaises(TypeError):
            self.api.get_everything(domains=domains)

        # Raise TypeError is exclude_domains param is not of type str
        exclude_domains = 0
        with self.assertRaises(TypeError):
            self.api.get_everything(exclude_domains=exclude_domains)

        # Raise TypeError if language param is not of type str
        language = 0
        with self.assertRaises(TypeError):
            self.api.get_everything(language=language)

        # Raise ValueError if language param is invalid
        language = "xx"
        with self.assertRaises(ValueError):
            self.api.get_everything(language=language)

        # Raise TypeError is sort_by param is not of type str
        sort_by = 1
        with self.assertRaises(TypeError):
            self.api.get_everything(sort_by=sort_by)

        # Raise ValueError if soft_by param is invalid
        sort_by = "sort"
        with self.assertRaises(ValueError):
            self.api.get_everything(sort_by=sort_by)

        # Raises TypeError if page_size param is not an int
        page_size = "1"
        with self.assertRaises(TypeError):
            self.api.get_everything(page_size=page_size)

        # Raises ValueError if page_size param is less than zero(0) or greater than 100
        page_size = -1
        with self.assertRaises(ValueError):
            self.api.get_everything(page_size=page_size)

        page_size = 1000
        with self.assertRaises(ValueError):
            self.api.get_everything(page_size=page_size)

        # Raises a TypeError is page param is not an int
        page = "1"
        with self.assertRaises(TypeError):
            self.api.get_everything(page=page)

        # Raises a ValueError if page param is less than zero(0)
        page = -1
        with self.assertRaises(ValueError):
            self.api.get_everything(page=page)

    def test_api_get_sources(self):
        # Raise TypeError if language param is not of type str
        language = 0
        with self.assertRaises(TypeError):
            self.api.get_sources(language=language)

        # Raise ValueError if language param is invalid
        language = "xx"
        with self.assertRaises(ValueError):
            self.api.get_sources(language=language)

        # Raise TypeError if country param is not of type str
        country = 0
        with self.assertRaises(TypeError):
            self.api.get_sources(country=country)

        # Raise ValueError if country param is invalid
        country = "xx"
        with self.assertRaises(ValueError):
            self.api.get_sources(country=country)

        # Raises TypeError if category param is not of type str
        category = 0
        with self.assertRaises(TypeError):
            self.api.get_sources(category=category)

        # Raises ValueError if category param is invalid
        category = "x0x"
        with self.assertRaises(ValueError):
            self.api.get_sources(category=category)