Skip to content
Snippets Groups Projects
Commit 4889c269 authored by Joonas's avatar Joonas
Browse files

Python news program

parents
No related branches found
No related tags found
No related merge requests found
File added
from myNewsClass import NewsByCountry
print()
while(True):
print("If you need instructions, type 'info'")
print("What country do you want news from?: ")
NewsByCountry.callNews(input())
\ No newline at end of file
from asyncio.windows_events import NULL
import requests
import json
import textwrap
class NewsByCountry:
def callNews(countryString):
if(countryString=="info"):
print()
print("Hello! Welcome to my news-program!")
print("This program uses 'newsapi.org' to get news from different sources")
print()
print("To get news from a country, input country's short form, eg. 'us'")
print("Country names must be written in lower-case")
print()
print("All possible country inputs are: ")
print("at, au, be, bg, br, ca, ch, cn, co, cu, cz, de, eg, fr, gb, gr, hk, hu, id, ie, il, in, it, jp, kr, lt, lv, ma, mx, my, ng, nl, no, nz, ph, pl, pt, ro, rs, ru, sa, se, sg, si, sk, th, tr, tw, ua, us, ve, za")
print("Please note that news can can be in other languages than English!")
print()
else:
print()
print("Top 5 news from '" + countryString + "':")
url = "https://newsapi.org/v2/top-headlines?country=" + countryString + "&apiKey=9a5f1610264d4831b94ba00b1bdbd885"
someCountry = requests.get(url)
content = json.loads(someCountry.content)
if(content["totalResults"]<5):
print()
print("LESS THAN 5 RESULTS")
print("This many results: ")
print(content["totalResults"])
print()
print("This probably means that country's name is wrong or country not supported")
print()
else:
articles = content["articles"]
print()
for i in range(5):
print(((articles[i])["source"])["name"] + ":")
if(articles[i]["description"]==NULL or articles[i]["description"]=="" or articles[i]["description"]==" "):
#Description is null, trying content:
if(articles[i]["content"]==NULL or articles[i]["content"]=="" or articles[i]["content"]==" "):
print("The content is also null, no can do")
else:
article = articles[i]["content"]
publishableArticle = textwrap.wrap(article, 80)
for i in publishableArticle:
print(i)
print()
else:
article = articles[i]["description"]
publishableArticle = textwrap.wrap(article, 80)
for i in publishableArticle:
print(i)
print()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment