joshuadunlop commited on
Commit
7119782
·
verified ·
1 Parent(s): 6f89878

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ from client import RestClient
5
+ import io
6
+
7
+ # Function to call the DataForSEO API
8
+ def get_backlinks(api_key, target_url, filters):
9
+ client = RestClient(api_key, api_key)
10
+ post_data = {
11
+ len(post_data): {
12
+ "target": target_url,
13
+ "limit": 1000,
14
+ "mode": "as_is",
15
+ "filters": filters
16
+ }
17
+ }
18
+ response = client.post("/v3/backlinks/backlinks/live", post_data)
19
+ if response["status_code"] == 20000:
20
+ return response['results']
21
+ else:
22
+ st.error("Error: Code: %d Message: %s" % (response["status_code"], response["status_message"]))
23
+ return None
24
+
25
+ # Streamlit layout
26
+ st.sidebar.title("DataForSEO API Parameters")
27
+ api_key = st.sidebar.text_input("API Key", type="password")
28
+ generate_button = st.sidebar.button("Generate All")
29
+ reset_button = st.sidebar.button("Reset")
30
+
31
+ # Filters
32
+ url_from_not_contain = st.sidebar.text_input("URL from does not contain (comma-separated)")
33
+ is_lost = st.sidebar.checkbox("Is Lost", value=False)
34
+ dofollow = st.sidebar.checkbox("Dofollow", value=True)
35
+ backlink_spam_score = st.sidebar.slider("Backlink Spam Score ≤", 0, 100, 10)
36
+ page_from_language = st.sidebar.selectbox("Page From Language", ['en', 'other'])
37
+
38
+ # Prepare filters for API call
39
+ filters = []
40
+ if url_from_not_contain:
41
+ for url in url_from_not_contain.split(','):
42
+ filters.append(["url_from", "not_like", url.strip()])
43
+ if is_lost:
44
+ filters.append(["is_lost", "=", is_lost])
45
+ if dofollow:
46
+ filters.append(["dofollow", "=", dofollow])
47
+ filters.append(["backlink_spam_score", "<=", backlink_spam_score])
48
+ filters.append(["page_from_language", "=", page_from_language])
49
+
50
+ # Main app layout
51
+ col1, col2 = st.columns(2)
52
+
53
+ with col1:
54
+ st.header("Input")
55
+ target_url = st.text_input("Enter the target URL")
56
+
57
+ with col2:
58
+ st.header("Output")
59
+
60
+ # Generate CSV and download button
61
+ if generate_button and target_url:
62
+ data = get_backlinks(api_key, target_url, filters)
63
+ if data:
64
+ df = pd.DataFrame(data)
65
+ csv = df.to_csv(index=False)
66
+ b64 = base64.b64encode(csv.encode()).decode()
67
+ href = f'<a href="data:file/csv;base64,{b64}" download="backlinks.csv">Download CSV file</a>'
68
+ st.markdown(href, unsafe_allow_html=True)
69
+
70
+ # Reset functionality
71
+ if reset_button:
72
+ st.experimental_rerun()