Spaces:
Sleeping
Sleeping
Delete parser.py
Browse files
parser.py
DELETED
|
@@ -1,184 +0,0 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import uuid
|
| 3 |
-
from datetime import datetime, timedelta
|
| 4 |
-
|
| 5 |
-
from fake_headers import Headers
|
| 6 |
-
from logger import setup_logging # Import your logging setup
|
| 7 |
-
|
| 8 |
-
logger = setup_logging()
|
| 9 |
-
|
| 10 |
-
class SemRush:
|
| 11 |
-
|
| 12 |
-
userId = ""
|
| 13 |
-
apiKey = ""
|
| 14 |
-
searchItem = ""
|
| 15 |
-
|
| 16 |
-
def __init__(self,userId,apiKey,searchItem):
|
| 17 |
-
|
| 18 |
-
self.userId = userId
|
| 19 |
-
self.apiKey = apiKey
|
| 20 |
-
self.searchItem = searchItem
|
| 21 |
-
self.endPoint = 'https://www.semrush.com/dpa/rpc'
|
| 22 |
-
|
| 23 |
-
def getHeders(self):
|
| 24 |
-
header = Headers(
|
| 25 |
-
browser="chrome", # Generate only Chrome UA
|
| 26 |
-
headers=True # generate misc headers
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
return header.generate()
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def getData(self,json_data):
|
| 33 |
-
|
| 34 |
-
cookies = {
|
| 35 |
-
'visit_first': '1721674555218',
|
| 36 |
-
'ga_exp_c41234be21bd4796bbf8e763': '1',
|
| 37 |
-
'_sm_bot': '9a7590d6486dd5c795e9b97d7e034b5790b60ecdc1ee7f9614d1b03450c2187b',
|
| 38 |
-
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
headers = self.getHeders()
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# proxies = {
|
| 45 |
-
# 'https': 'http://1z8bRM:A1ha8v@138.59.5.67:9124',
|
| 46 |
-
# }
|
| 47 |
-
# response = requests.post(self.endPoint, cookies=cookies, headers=headers, json=json_data, proxies=proxies)
|
| 48 |
-
|
| 49 |
-
response = requests.post(self.endPoint, cookies=cookies, headers=headers, json=json_data)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
return response.json()
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def domainOverview(self):
|
| 58 |
-
statistic = dict()
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
try:
|
| 62 |
-
|
| 63 |
-
data = self.getData(json_data={
|
| 64 |
-
'id': 15,
|
| 65 |
-
'jsonrpc': '2.0',
|
| 66 |
-
'method': 'organic.OverviewTrend',
|
| 67 |
-
'params': {
|
| 68 |
-
'request_id': uuid.uuid4().urn,
|
| 69 |
-
'report': 'domain.overview',
|
| 70 |
-
'args': {
|
| 71 |
-
'dateType': 'monthly',
|
| 72 |
-
'searchItem': self.searchItem,
|
| 73 |
-
'searchType': 'domain',
|
| 74 |
-
'dateRange': None,
|
| 75 |
-
'database': 'us',
|
| 76 |
-
'global': True,
|
| 77 |
-
},
|
| 78 |
-
'userId': self.userId,
|
| 79 |
-
'apiKey': self.apiKey,
|
| 80 |
-
},
|
| 81 |
-
})
|
| 82 |
-
|
| 83 |
-
# Find the most recent date in data['result']
|
| 84 |
-
last_date_str = max(element['date'] for element in data['result'])
|
| 85 |
-
last_date = datetime.strptime(last_date_str, "%Y%m%d")
|
| 86 |
-
|
| 87 |
-
# Calculate the date two years prior to the last date
|
| 88 |
-
two_years_ago = last_date - timedelta(days=2 * 365)
|
| 89 |
-
|
| 90 |
-
# Filter dates and populate the statistic dictionary
|
| 91 |
-
for element in data['result']:
|
| 92 |
-
element_date = datetime.strptime(element['date'], "%Y%m%d")
|
| 93 |
-
|
| 94 |
-
# Only add if the date is within the last two years
|
| 95 |
-
if element_date >= two_years_ago:
|
| 96 |
-
statistic[element['date']] = element['organicTraffic']
|
| 97 |
-
|
| 98 |
-
print(type(element['date']))
|
| 99 |
-
return statistic
|
| 100 |
-
|
| 101 |
-
except Exception as e:
|
| 102 |
-
logger.error(f"Error processing task domainOverview: {e} for {self.searchItem} {data}")
|
| 103 |
-
return {"error": data}
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
def organicSummary(self):
|
| 107 |
-
statistic = dict()
|
| 108 |
-
request_id = str(uuid.uuid4())
|
| 109 |
-
|
| 110 |
-
url_no_https = self.searchItem.split('://')[1] if '://' in self.searchItem else self.searchItem
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
try:
|
| 114 |
-
|
| 115 |
-
json_data={
|
| 116 |
-
"id": 20,
|
| 117 |
-
"jsonrpc": "2.0",
|
| 118 |
-
"method": "organic.Summary",
|
| 119 |
-
"params": {
|
| 120 |
-
"request_id": request_id,
|
| 121 |
-
"report": "domain.overview",
|
| 122 |
-
"args": {
|
| 123 |
-
'searchItem': url_no_https,
|
| 124 |
-
"searchType": "domain",
|
| 125 |
-
"dateType": "monthly",
|
| 126 |
-
"database": "us",
|
| 127 |
-
"global": True,
|
| 128 |
-
"dateFormat": "date"
|
| 129 |
-
},
|
| 130 |
-
'userId': self.userId,
|
| 131 |
-
'apiKey': self.apiKey,
|
| 132 |
-
}
|
| 133 |
-
}
|
| 134 |
-
|
| 135 |
-
data = self.getData(json_data)
|
| 136 |
-
|
| 137 |
-
for element in data['result']:
|
| 138 |
-
if 'mobile-' in element['database']:
|
| 139 |
-
continue
|
| 140 |
-
# statistic[element['database']] = element['organicPositions']
|
| 141 |
-
statistic[element['database']] = element['organicTraffic']
|
| 142 |
-
|
| 143 |
-
return statistic
|
| 144 |
-
|
| 145 |
-
except Exception as e:
|
| 146 |
-
logger.error(f"Error processing task organicSummary: {e} for {url_no_https} {data}")
|
| 147 |
-
return { "error": data}
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
def backlinksSummary(self):
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
try:
|
| 158 |
-
|
| 159 |
-
data = self.getData(json_data={
|
| 160 |
-
"id": 21,
|
| 161 |
-
"jsonrpc": "2.0",
|
| 162 |
-
"method": "backlinks.Summary",
|
| 163 |
-
"params": {
|
| 164 |
-
"request_id": uuid.uuid4().urn,
|
| 165 |
-
"report": "domain.overview",
|
| 166 |
-
"args": {
|
| 167 |
-
"searchItem": self.searchItem,
|
| 168 |
-
"searchType": "domain"
|
| 169 |
-
},
|
| 170 |
-
'userId': self.userId,
|
| 171 |
-
'apiKey': self.apiKey,
|
| 172 |
-
}
|
| 173 |
-
})
|
| 174 |
-
|
| 175 |
-
return data['result']
|
| 176 |
-
|
| 177 |
-
except Exception as e:
|
| 178 |
-
logger.error(f"Error processing task backlinksSummary: {e} for { self.searchItem} {data}")
|
| 179 |
-
return { "error": data}
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|