Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import json
|
3 |
+
from googleapiclient.discovery import build
|
4 |
+
|
5 |
+
# API key input (default provided; users can get their own at the link below)
|
6 |
+
default_api_key = "AIzaSyAYdqy4sQzUPFXuSXVDJn3WHIEaHMXMv5I"
|
7 |
+
API_KEY = st.text_input(
|
8 |
+
"Enter your PageSpeed Insights API Key",
|
9 |
+
value=default_api_key,
|
10 |
+
help="Don't have one? Get your API key from [Google PageSpeed Insights Get Started](https://developers.google.com/speed/docs/insights/v5/get-started)."
|
11 |
+
)
|
12 |
+
|
13 |
+
# Function to run PageSpeed Insights API call
|
14 |
+
def run_pagespeed(service, url, strategy="DESKTOP"):
|
15 |
+
st.info(f"Running PageSpeed Insights for {url} with strategy {strategy}...")
|
16 |
+
try:
|
17 |
+
result = service.pagespeedapi().runpagespeed(url=url, strategy=strategy).execute()
|
18 |
+
except Exception as e:
|
19 |
+
st.error(f"Error running PageSpeed Insights on {url}: {e}")
|
20 |
+
return None
|
21 |
+
return result
|
22 |
+
|
23 |
+
# Function to process the API result
|
24 |
+
def process_result(result):
|
25 |
+
data = {}
|
26 |
+
try:
|
27 |
+
lighthouse_result = result.get("lighthouseResult", {})
|
28 |
+
categories = lighthouse_result.get("categories", {})
|
29 |
+
for cat, details in categories.items():
|
30 |
+
score = details.get("score", None)
|
31 |
+
data[cat] = round(score * 100, 2) if score is not None else None
|
32 |
+
data["requestedUrl"] = result.get("id", "")
|
33 |
+
config_settings = lighthouse_result.get("configSettings", {})
|
34 |
+
data["strategy"] = config_settings.get("formFactor", "")
|
35 |
+
data["fetchTime"] = lighthouse_result.get("fetchTime", "")
|
36 |
+
except Exception as e:
|
37 |
+
st.error(f"Error processing result: {e}")
|
38 |
+
return data
|
39 |
+
|
40 |
+
# Main Streamlit app function
|
41 |
+
def main():
|
42 |
+
st.title("PageSpeed Insights Analyzer")
|
43 |
+
st.write("Enter a URL and select a strategy to run a PageSpeed Insights analysis.")
|
44 |
+
|
45 |
+
url = st.text_input("URL", "https://www.ocoya.com/")
|
46 |
+
strategy = st.radio("Select Strategy", ("DESKTOP", "MOBILE"), index=0)
|
47 |
+
|
48 |
+
if st.button("Run Analysis"):
|
49 |
+
# Build the PageSpeed Insights API service object
|
50 |
+
service = build('pagespeedonline', 'v5', developerKey=API_KEY)
|
51 |
+
with st.spinner("Running analysis..."):
|
52 |
+
result = run_pagespeed(service, url, strategy=strategy)
|
53 |
+
if result:
|
54 |
+
metrics = process_result(result)
|
55 |
+
st.subheader("Extracted Metrics")
|
56 |
+
st.write(metrics)
|
57 |
+
st.subheader("Full JSON Response")
|
58 |
+
st.json(result)
|
59 |
+
|
60 |
+
if __name__ == "__main__":
|
61 |
+
main()
|