-
Notifications
You must be signed in to change notification settings - Fork 0
/
datalab_api.py
79 lines (69 loc) · 3.08 KB
/
datalab_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
#PLEASE READ THIS!!!!!
#THIS 'API_KEYS' IS A PYTHON FILE OF MINE THAT SOTERS MY PRIVATE API KEYS.
from API_KEYS import * #SO DELETE THIS LINE!!!!!
import json, urllib.request
class DatalabShoppingAPI():
#네이버 데이터 랩 쇼핑인사이트
def __init__(self) -> None:
self.client_id = data_lab_client_id #type your own client_id here. like, #client_id = "your_client_id"
self.client_secret = data_lab_client_secret #type your own client_secret here.
self.url = "https://openapi.naver.com/v1/datalab/shopping/categories"
def CreateBody(self, startDate: str, endDate: str, category: list) -> json:
#category is like: [{"name":"출산/육아", "param":["50000005"]}, {"name":"식품", "param":["50000006"]}],
body = {
"startDate":startDate,
"endDate":endDate,
"timeUnit":"date",
"category": category,
"device":"",
"ages":[],
"gender":""
}
body = json.dumps(body)
return body
def GetResponse(self, body: json):
request = urllib.request.Request(self.url)
request.add_header("X-Naver-Client-Id",self.client_id)
request.add_header("X-Naver-Client-Secret",self.client_secret)
request.add_header("Content-Type","application/json")
response = urllib.request.urlopen(request, data=body.encode("utf-8"))
return response
class DatalabSearchAPI():
#네이버 데이터 랩 통합검색어
def __init__(self) -> None:
self.client_id = data_lab_search_client_id #type your own client_id here. like, #client_id = "your_client_id"
self.client_secret = data_lab_search_client_secret #type your own client_secret here.
self.url = "https://openapi.naver.com/v1/datalab/search"
def CreateKeywordsGroups(self, keywords: list) -> dict:
'''
Arguments: list of five keywords
Returns: dictionary containing the infos
This process is required because the http body requires the keywords to be in specific format.
'''
keyword_groups = []
for i in range(5):
keyword_group = {
'groupName': keywords[i],
'keywords': [keywords[i]]
}
keyword_groups.append(keyword_group)
return keyword_groups
def CreateBody(self, startDate: str, endDate: str, keywords_groups: list) -> json:
body = {
"startDate": startDate,
"endDate": endDate,
"timeUnit": "date",
"keywordGroups": keywords_groups,
"device": "",
"ages": [],
"gender": ""
}
body = json.dumps(body, ensure_ascii=False)
return body
def GetResponse(self, body: json):
request = urllib.request.Request(self.url)
request.add_header("X-Naver-Client-Id",self.client_id)
request.add_header("X-Naver-Client-Secret",self.client_secret)
request.add_header("Content-Type","application/json")
response = urllib.request.urlopen(request, data=body.encode("utf-8"))
return response