Created
December 3, 2017 18:58
-
-
Save surajsahani/3a305e736cbf2675238e3a1c25c4591d to your computer and use it in GitHub Desktop.
News In python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import json as js | |
import urllib.request | |
choice=1 | |
def grabNews(source,sort,apikey): | |
url=' https://newsapi.org/v1/articles?'+'source='+source+'&sortBy='+sort+'&apikey='+apikey | |
# print(url) | |
data=urllib.request.urlopen(url).read().decode('utf-8') | |
newsdata = js.loads(data) | |
return newsdata | |
def grabSources(language): | |
url=' https://newsapi.org/v1/sources?language='+language | |
rawSource=urllib.request.urlopen(url).read().decode('utf-8') | |
allsources=js.loads(rawSource) | |
sourcesEnlist = [x['id'] for x in allsources['sources']] | |
return sourcesEnlist | |
while choice !='0': | |
temp = 0 | |
sources=grabSources('en') | |
print('select a news source you like[using number]') | |
for item in sources: | |
print(temp,'->',item) | |
temp=temp+1 | |
sltsource=int(input('enter your choice:')) | |
newsdata=grabNews(sources[sltsource],'top','PASTE YOUR API KEY HERE') | |
print('--------------------------------------------------------------------------------------') | |
for news in newsdata['articles']: | |
print(str(news['title']).upper()) | |
print(news['description']) | |
print('url-> ',news['url']) | |
print('--------------------------------------------------------------------------------------') | |
print(' ') | |
choice=str(input('enter "0" to leave news')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment