-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNews_API.py
90 lines (69 loc) · 2.28 KB
/
News_API.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
# importing json and requests
import requests
import json
API_KEY='ENTER_API_KEY_HERE'
#Sources Request Function
def request_sources():
#Dictionary of possible categories
categories = {
1. : 'entertainment',
2. : 'general',
3. : 'health',
4. : 'science',
5. : 'sports',
6. : 'technology'
}
#List Possible Switches
print('[*] 1 => entertainment')
print('[*] 2 => general')
print('[*] 3 => health')
print('[*] 4 => science')
print('[*] 5 => sports')
print('[*] 6 => technology')
query = int(input("[?] Enter Sources Category Number => "))
#If query is not in range circle back to initializer
if (query >= 0) and (query < 7):
category = categories[query]
#Sources Request Call
sources = requests.get('https://newsapi.org/v2/sources?category=%s&apiKey=%s' % (category, API_KEY))
data = json.loads(sources.text)
#Loop through sources key
for source in data['sources']:
print('[*] ID => %s' %(source['id']))
print('[*] DESCRIPTION => %s' %(source['description']))
print('\n')
else:
print('[!] Bad Category request')
#Article Request Function
def request_article():
#Input Query
print('\n')
query = raw_input("[?] Enter Article Request => ")
print('\n')
#Articles Request Call
articles = requests.get('https://newsapi.org/v2/everything?q=%s&apiKey=%s' % (query, API_KEY))
#JSON Convertion
data = json.loads(articles.text)
#Loop through articles key
for article in data['articles']:
print('[*] ARTICLE => %s' %(article['content']))
print('[*] URL => %ss' %(article['url']))
print('\n')
#Initializer
if (__name__ == "__main__"):
#Function call switch statement
api_function ={
1. : request_sources,
2. : request_article
}
#Selection Loop
selection = 0
while(selection != 3):
print('[*] 1 => request sources')
print('[*] 2 => request articles')
print('[*] 3 => quit')
print('\n')
selection = int(input("[?] Select a API Option Number: "))
#Will Quit if Input is greater than 3
if (selection >= 0) and (selection < 3):
api_function[selection]()