-
Notifications
You must be signed in to change notification settings - Fork 0
/
naver_api.py
48 lines (39 loc) · 1.49 KB
/
naver_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
import os
import urllib.request
import datetime
import json
from dotenv import load_dotenv
def load_api_keys():
load_dotenv()
client_id = os.environ.get('client_id')
client_secret = os.environ.get('client_secret')
return client_id, client_secret
def getRequestUrl(url, client_id, client_secret):
req = urllib.request.Request(url)
req.add_header("X-Naver-Client-Id", client_id)
req.add_header("X-Naver-Client-Secret", client_secret)
try:
response = urllib.request.urlopen(req)
if response.getcode() == 200:
print("[%s]Url Request Success" % datetime.datetime.now())
return response.read().decode('utf-8')
except Exception as e:
print(e)
print("[%s] Error for URL : %s" % (datetime.datetime.now(), url))
return None
def getNaverSearch(node, srcText, start, display, client_id, client_secret):
# Check if the file exists, if it does, skip the API call
file_name = f"./cache/{srcText}_naver_{node}.json"
if os.path.exists(file_name):
print(f"{file_name} already exists. Skipping the API call.")
return None
base = "https://openapi.naver.com/v1/search"
node = "/%s.json" % node
parameters = "?query=%s&start=%s&display=%s" % (
urllib.parse.quote(srcText), start, display)
url = base + node + parameters
responseDecode = getRequestUrl(url, client_id, client_secret)
if responseDecode is None:
return None
else:
return json.loads(responseDecode)