-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_news.py
35 lines (26 loc) · 918 Bytes
/
extract_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
from hdfs import InsecureClient
from datetime import datetime
import requests
from json import dumps
import os
dirs = "/home/bdm/proj/"
# dirs = ""
with open(dirs+'input/key.txt') as f:
key = f.readlines()[0]
url = "https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey="+key
payload = {}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
date = datetime.now().strftime("%Y_%m_%d-%H:%M:%S")
# get connected with hdfs server
client = InsecureClient("http://10.4.41.50:9870", user='bdm')
ddate = date.split("-")
# fnames = client.list('/user/bdm')
# write file to hdfs server
client.makedirs('stock/news/'+ddate[0])
tmp_fold = 'stock/news/'+ddate[0]
tmp_file = ddate[1].split(':')[0]
file_name = tmp_fold + '/file_' + tmp_file + '.json'
client.write(file_name, dumps(data))
# client.delete('stock/news/2022_03_29', recursive=True)