File size: 7,255 Bytes
dbb9b6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e6d815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbb9b6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from http.client import HTTPSConnection
from base64 import b64encode
from json import loads
from json import dumps
from keys import data_for_seo_email, data_for_seo_password

class RestClient:
    domain = "api.dataforseo.com"

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def request(self, path, method, data=None):
        connection = HTTPSConnection(self.domain)
        try:
            base64_bytes = b64encode(
                ("%s:%s" % (self.username, self.password)).encode("ascii")
                ).decode("ascii")
            headers = {'Authorization' : 'Basic %s' %  base64_bytes, 'Content-Encoding' : 'gzip'}
            connection.request(method, path, headers=headers, body=data)
            response = connection.getresponse()
            return loads(response.read().decode())
        finally:
            connection.close()

    def get(self, path):
        return self.request(path, 'GET')

    def post(self, path, data):
        if isinstance(data, str):
            data_str = data
        else:
            data_str = dumps(data)
        return self.request(path, 'POST', data_str)



client = RestClient(data_for_seo_email, data_for_seo_password)
# client = RestClient("deepankar@warewe.com", "cb1661e9ec7c1fba")



def generate_keyword_intent_list(list_of_keywords: list):
    post_data = dict()
    # simple way to set a task
    post_data[len(post_data)] = dict(
        keywords= list_of_keywords,
        language_name="English"
    )
    # POST /v3/dataforseo_labs/google/search_intent/live
    response = client.post("/v3/dataforseo_labs/google/search_intent/live", post_data)
    # you can find the full list of the response codes here https://docs.dataforseo.com/v3/appendix/errors
    if response["status_code"] == 20000:
        # print(response)
        return response["tasks"][0]["result"][0]["items"]
        # do something with result
    else:
        print("error. Code: %d Message: %s" % (response["status_code"], response["status_message"]))

    



def generate_seo_metatitle(keyword, num_query_results=10):
    post_data = dict()
    # You can set only one task at a time
    post_data[len(post_data)] = dict(
        language_code="en",
        location_code=2840,
        keyword=keyword
    )
    
    response = client.post("/v3/serp/google/organic/live/regular", post_data)



    if response["status_code"] == 20000:
        # print(response)
        d= response['tasks'][0]
        # print(d)
        result_dict= d['result'][0]['items']
        summary= ''
        for i in result_dict[:num_query_results]:
            # summary+= i['title']+' '
            x= i['description']
            if x:
                summary+= i['title']+' '+ i['domain']+' '+ i['url']+' '+x+' '
        summary= summary.replace('\n', ' ') 
        summary= summary.replace(',', ' ') 
        summary= summary.replace(';', ' ') 
        print(summary)
        return summary
        
        
        # do something with result
    else:
        print("error. Code: %d Message: %s" % (response["status_code"], response["status_message"] , f" for keyword {keyword}"))
        

def generate_seo_metatitle_train(keyword, num_query_results=10):
    post_data = dict()
    # You can set only one task at a time
    post_data[len(post_data)] = dict(
        language_code="en",
        location_code=2840,
        keyword=keyword
    )
    
    response = client.post("/v3/serp/google/organic/live/regular", post_data)



    if response["status_code"] == 20000:
        # print(response)
        d= response['tasks'][0]
        # print(d)
        result_dict= d['result'][0]['items']
        summary= ''
        for i in result_dict[:num_query_results]:
            # summary+= i['title']+' '
            x= i['description']
            if x:
                summary+= i['title']+' '+ i['domain']+' '+ i['url']+' '+x+' '
        # print(summary)
        return summary.replace('\n', ' ') 
        
        # do something with result
    else:
        print("error. Code: %d Message: %s" % (response["status_code"], response["status_message"] , f" for keyword {keyword}"))
        
        

def generate_top_urls(keyword, num_query_results=10)->list:
    # print(keyword)
    post_data = dict()
    # You can set only one task at a time
    post_data[len(post_data)] = dict(
        language_code="en",
        location_code=2840,
        keyword=keyword
    )
    
    response = client.post("/v3/serp/google/organic/live/regular", post_data)



    if response["status_code"] == 20000:
        # print(response)
        d= response['tasks'][0]
        # print(d)
        result_dict= d['result'][0]['items']
        urls= []
        for i in result_dict[:num_query_results]:
            # summary+= i['title']+' '
            urls.append(i['url'])
            # x= i['description']
            # if x:
            #     summary+= i['title']+' '+ i['domain']+' '+ i['url']+' '+x+' '
        # print(summary)
        # print(urls)
        return (urls)
        
        # do something with result
    else:
        print("error. Code: %d Message: %s" % (response["status_code"], response["status_message"] , f" for keyword {keyword}"))
        
        





def generate_seo_metatitle_train_for_kg(keyword, num_query_results=10):
    post_data = dict()
    # You can set only one task at a time
    post_data[len(post_data)] = dict(
        language_code="en",
        location_code=2840,
        keyword=keyword
    )
    
    response = client.post("/v3/serp/google/organic/live/regular", post_data)



    if response["status_code"] == 20000:
        # print(response)
        d= response['tasks'][0]
        # print(d)
        result_dict= d['result'][0]['items']
        summary= ''
        for i in result_dict[:num_query_results]:
            # summary+= i['title']+' '
            x= i['description']
            if x:
                # summary+= i['title']+' '+ i['domain']+' '+ i['url']+' '+x+' '
                summary+= i['title']+' '+x+' '
        # print(summary)
        return summary.replace('\n', ' ') 
        
        # do something with result
    else:
        print("error. Code: %d Message: %s" % (response["status_code"], response["status_message"] , f" for keyword {keyword}"))
        
        
        
def generate_intent_using_dataforseo(keyword_list):
    post_data = dict()
    # simple way to set a task
    post_data[len(post_data)] = dict(
        keywords= keyword_list,
        language_name="English"
    )
    
    response = client.post("/v3/dataforseo_labs/google/search_intent/live", post_data)
    # you can find the full list of the response codes here https://docs.dataforseo.com/v3/appendix/errors
    if response["status_code"] == 20000:
        # print(response)
        # do something with result
        d= response['tasks'][0]
        # print(d)
        result_dict= d['result'][0]['items']
        intent_dict= dict()
        for i in result_dict:
            key= i['keyword']
            value= i['keyword_intent']['label']
            intent_dict[key]= value
        return intent_dict
    else:
        print("error. Code: %d Message: %s" % (response["status_code"], response["status_message"]))