-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.py
114 lines (81 loc) · 2.47 KB
/
news.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
import requests
from datetime import date
from googletrans import Translator
translator = Translator()
n=translator.translate('bonjour moi')
# # import re
# print(type(n))
today = date.today()
searchArray = sys.argv[1:]
# print(len(searchArray))
search=""
# print(searchArray[0])
for i in searchArray:
search+=i + " "
# regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
# if( not (regex.search(string)==None)):
# print("No Special Characters Please")
# exit()
# regex = re.compile('@_!$%^&*()<>?/|\}{~:"')
# def removeChar(text):
# remove regex.sub('', text)
# search = removeChar(search)
# print(search)
# if ' ' in search:
# print("true")
# search=search.replace(" ", "%20")
# print(search)
link = "https://newsapi.org/v2/top-headlines?q={}&from={}&sortBy=publishedAt&apiKey=4341fffce2704e9e991191574134ed38".format(search, today)
response = requests.get(link)
response=response.json()
# print(response)
# print(len(response))
count = 0
# print(response)
# print(len(response))
totalResults = response['totalResults']
# if len(response)>1:
if totalResults>1:
if totalResults<4:
maxArticles=totalResults
if totalResults>3:
maxArticles=3
while count < maxArticles:
articles = response['articles'][count]
# print(articles)
print("Article " + str(count+1) + ": ")
if(articles['title'] is not None):
print("Title: " + articles['title'][:100] +"...")
# print(articles['title'])
if(articles['content'] is not None):
print("Content: " + articles['content'][:200]+"..." +"\n")
# print(articles['content'])
# if(articles['description'] is not None):
# print("Description: ")
# print(articles['description'][:150] +"..." + "\n")
count+=1
if totalResults == 1:
articles=response['articles'][0]
print("Title: " + articles['title'])
print("Content: " + articles['content'])
# print("Description: " + articles['description'])
print("\n")
if totalResults ==0:
print("Sorry, no available news for '{}'".format(search))
# print(articles['description'] + "\n")
# else:
# print("nothing")
# why does this loop work
# articles= response['articles'][0]
# print(articles['title'])
# print(articles)
# title=response.get('articles')
# print(title.get('title'))
# for x in response:
# print(response.get(x))
# content = response['articles']['content']
# url = response['articles']['url']
# for i in range(10):
# print(content)
# print(url)