-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.py
More file actions
288 lines (228 loc) · 8.63 KB
/
main.py
File metadata and controls
288 lines (228 loc) · 8.63 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import requests
import json
import base64
import csv
import config
from datetime import datetime
import math
import os
import time
from json.decoder import JSONDecodeError
from os import path
def banner():
bname = [98, 121, 32, 65, 110, 117, 112, 32, 80, 117, 115, 104, 112, 97, 100]
res = ''.join(map(chr, bname))
gitul = [104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 97, 110, 117, 112, 112, 117, 115, 104, 112, 97, 100, 47, 71, 111, 77, 111, 122, 67, 104, 101, 99, 107, 101, 114]
ngitul = ''.join(map(chr, gitul))
RED = "\033[0;31m"
BLUE = "\033[0;34m"
CYAN = "\033[0;36m"
WHITE = "\033[1;37m"
YELLOW = "\033[1;33m"
OFF = "\033[0;0m"
intro = (BLUE + """
_____ __ __ _____ _ _
/ ____| | \/ | / ____| | | |
| | __ ___ | \ / | ___ ___| | | |__ ___ ___| | _____ _ __
| | |_ |/ _ \| |\/| |/ _ \_ / | | '_ \ / _ \/ __| |/ / _ \ '__|
| |__| | (_) | | | | (_) / /| |____| | | | __/ (__| < __/ |
\_____|\___/|_| |_|\___/___|\_____|_| |_|\___|\___|_|\_\___|_|
""" + CYAN + str(res) + "\n" + YELLOW + "\n" + "\n" + "✨ The Github Repository" + "\n" + "\n" + WHITE + str(ngitul) + "\n" + "\n" + OFF)
print(intro)
banner()
global m #url counter
m = 0
global starttime
starttime = 0
base_url = "https://lsapi.seomoz.com/v2/url_metrics"
global filename
filename = ""
axha = len(open('urls.txt', 'r').readlines())
##adding headers to csv
head = ['Domain Name', 'DA', 'PA', 'Spam Score']
filename = datetime.now().strftime("%d/%m/%Y %H:%M")
filename = filename.replace(" ", "_")
filename = filename.replace("/", "-")
with open(filename + ".csv", "a") as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(head)
csvfile.close()
##done adding headers
def clean_url():
x = open("urls.txt")
y = x.read() ##regexto do this ^(https://)|(http://)|(www.)
z = y.replace("https://", "")
z = z.replace("http://", "")
z = z.replace("www.", "")
with open('urls.txt', 'w+') as f:
z = z.replace("'", "\"")
f.write(z)
f.close()
calculations()
def hash_auth_token(urlarray, urlbunch, count, leftover):
creds = json.load(open('config.json', 'r'))
for i in range(0, len(creds['id'])):
access_id = creds['id'][i]
sec_hash = creds['pass'][i]
bauth = access_id + ":" + sec_hash
bauth1 = bauth.encode("ascii")
bauth2 = base64.b64encode(bauth1)
finbauth = bauth2.decode("ascii")
j = requests.post(base_url, headers={"Authorization": "Basic " + finbauth})
if j.status_code == 400:
print("Using the API : " + str(access_id))
req_loop(finbauth, urlarray, urlbunch, count, leftover)
if j.status_code == 401:
print("API limit reached!")
print("Change API. Run the addapi.py file.")
break
else:
break
def calculations():
global starttime
starttime = time.perf_counter()
global count
count = len(open('urls.txt').readlines())
if count>50:
leftover = count % 50
urlbunch = math.floor(count//50)
else:
leftover = 0
urlbunch = 1
url_read(count, leftover, urlbunch)
def url_read(count, leftover, urlbunch):
with open('urls.txt') as f:
urlarray = f.read().split('\n')
hash_auth_token(urlarray, urlbunch, count, leftover)
def req_loop(finbauth, urlarray, urlbunch, count, leftover):
urllistlen = len(urlarray) - 1
if urllistlen < 50:
send_req(finbauth, urlarray, urllistlen)
else:
for i in range(0, urlbunch):
modurlarray = urlarray[int(i*50):int(i*50+50)]
#print("mod" +str(modurlarray))
send_req(finbauth, modurlarray, urllistlen)
######request for leftover
for i in range(0, 1):
leftoverurlarray = urlarray[-int(leftover):]
tempvar = len(leftoverurlarray) - 1
leftoverurlarray.pop(tempvar - 1) #remove last empty index
#print("leftover "+str(leftoverurlarray))
send_leftreq(finbauth, leftoverurlarray, tempvar)
def send_leftreq(finbauth, urlarray, urllistlen):
headers = {"Authorization": "Basic " + finbauth}
finurlarray = []
for z in urlarray:
if z != "":
finurlarray.append(z)
else:
pass
finurlarray = str(finurlarray)
finurlarray = finurlarray.replace("'", "\"")
data = "{\"targets\": " + finurlarray + "}"
r = requests.post(base_url, headers=headers, data=data)
try:
x = json.loads(r.content)
except JSONDecodeError:
print("Error : " + str(x['name']))
###for le
count = urllistlen
leftparse(x, count)
def leftparse(x, count):
global m
for b in range(0, count):
try:
domain = x['results'][b]['root_domain']
except(KeyError, IndexError):
try:
if x['message'] == (("Your monthly row limit reached. To increase your monthly request limit, see: http://moz.com/products/api/pricing") and ("This request exceeds the limit allowed by your current plan. To increase your request limit, see: http://moz.com/products/api/pricing")):
print("API limit reached")
#use next api
except:
break
try:
da = x['results'][b]['domain_authority'] #domain_authority
except(IndexError, KeyError):
break
try:
pa = x['results'][b]['page_authority'] #page_authority
except(IndexError, KeyError):
break
spamscore = x['results'][b]['spam_score'] #spam_score
m += 1
csv_input(domain, da, pa, spamscore)
def send_req(finbauth, urlarray, urllistlen):
headers = {"Authorization": "Basic " + finbauth}
finurlarray = []
for z in urlarray:
if z != "":
finurlarray.append(z)
else:
pass
finurlarray = str(finurlarray)
finurlarray = finurlarray.replace("'", "\"")
data = "{\"targets\": " + finurlarray + "}"
r = requests.post(base_url, headers=headers, data=data)
try:
x = json.loads(r.content)
except JSONDecodeError:
print("Error : " + str(x['name']))
print("Error!!!! Try adding new API or wait for some time.")
###for le
count = urllistlen
jsonparse(x, count)
def jsonparse(x, count):
global m
for b in range(0, 50):
try:
domain = x['results'][b]['root_domain']
except(KeyError, IndexError):
try:
if x['message'] == (("Your monthly row limit reached. To increase your monthly request limit, see: http://moz.com/products/api/pricing") and ("This request exceeds the limit allowed by your current plan. To increase your request limit, see: http://moz.com/products/api/pricing")):
print("API limit reached")
#use next api
except:
pass
try:
da = x['results'][b]['domain_authority'] #domain_authority
except(IndexError, KeyError):
break
try:
pa = x['results'][b]['page_authority'] #page_authority
except(IndexError, KeyError):
break
spamscore = x['results'][b]['spam_score'] #spam_score
m += 1
csv_input(domain, da, pa, spamscore)
def csv_input(domain, da, pa, spamscore):
rows = []
rows.append(domain)
rows.append(da)
rows.append(pa)
rows.append(spamscore)
makecsv(rows)
def makecsv(rows):
global filename
global m
global ci
global axha
with open(filename + ".csv", "a") as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(rows)
csvfile.close()
totaltime = axha // 50 + 1
timetaken = m // 50
timerem = round(totaltime - timetaken, 2)
if timerem < 0:
timerem == 5
else:
timerem = timerem
print("[+] Processed " + str(m) + " urls." + " " + "Time Remaining: " + str(timerem) + " seconds", end="\r")
clean_url()
stoptime = time.perf_counter()
if path.exists(filename+".csv") == True:
print("Returned " + str(axha) +" rows in " + str(round(stoptime - starttime, 2)) + " seconds.")
else:
print("Error Occured. Check API/url file.")
# Babye!