Skip to content

Instantly share code, notes, and snippets.

View DerekHawkins's full-sized avatar

DerekHawkins

View GitHub Profile
### Essentials ###
import pandas as pd # for working with the data
import requests # calling on various APIs used in this process
import urllib # for developing URLs for REST API calls
# Time Manipulation #
from calendar import monthrange # for developing timeframes on a monthly basis
import datetime # for compiling dates so we never have to load in time variables every month
import time # for basic time manipulaltion
### SERP Presence ###
### Load SEMRush Credentials and URLs for API call.
api_key = 'api key goes here'
service_url = 'https://api.semrush.com'
client_url = 'https://www.clientsite.com/'
### Various time manipulations for each part of the report ###
today = datetime.date.today()
first = today.replace(day=1)
service_url = 'https://api.semrush.com/analytics/v1/'
def backlink_url_build():
params = {
"?type": "backlinks",
'key': api_key,
'target': "clientsite.com",
'target_type': 'root_domain',
'display_limit': '30000',
'display_sort': 'first_seen_desc'
}
### Record All Data ###
writer = pd.ExcelWriter('kpi_report_%s.xlsx' % (date_main), engine='xlsxwriter')
# Write each dataframe to a different worksheet.
final_search_presence.to_excel(writer, sheet_name='SERP Presence')
serp_results.to_excel(writer, sheet_name='SERP Engagement')
new_bl.to_excel(writer, sheet_name="{} Backlinks".format(len(new_bl.first_seen)))
### SERP Engagement ###
def serp_engagement():
account = searchconsole.authenticate(client_config='C:/Users/My.Computer/search_console_credentials.json',
credentials='C:/Users/My.Computer/search_console_serialized_creds.json')
webproperty = account['https://www.clientsite.com/']
frames = []
report = webproperty.query.range(search_console_date, days=day).dimension('query').get()
sc_data = []
for kw_row in report.rows:
keyword = kw_row[0]
# Essentials #
import pandas as pd
import urllib
import requests
import time
import sys
# UI #
import easygui
from easygui import *
data_list_mobile = []
firstContent_mobile = str(response["lighthouseResult"]['audits']['first-contentful-paint']['displayValue'])
timetoInteractive_mobile = str(response["lighthouseResult"]['audits']['interactive']['displayValue'])
speedData_mobile = str(response["lighthouseResult"]['audits']['speed-index']['displayValue'])
data_list_mobile.append((firstContent_mobile, timetoInteractive_mobile, speedData_mobile))
# for Plotly API Credentials, create a json file with credentials
import json
with open('path_to_credentials.json') as f:
creds = json.load(f)
# Essentials
import math
x = float('nan')
import pandas as pd
import numpy as np
service_url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed/"
def speed_test_url(url, device):
params = {
"?url": url,
'strategy': device,
'key': key,
}
data = urllib.parse.urlencode(params, doseq=True)
main_call = urllib.parse.urljoin(service_url, data)
main_call = main_call.replace(r'%3F', r'?')