File size: 2,366 Bytes
7119782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import requests
from client import RestClient
import io

# Function to call the DataForSEO API
def get_backlinks(api_key, target_url, filters):
    client = RestClient(api_key, api_key)
    post_data = {
        len(post_data): {
            "target": target_url,
            "limit": 1000,
            "mode": "as_is",
            "filters": filters
        }
    }
    response = client.post("/v3/backlinks/backlinks/live", post_data)
    if response["status_code"] == 20000:
        return response['results']
    else:
        st.error("Error: Code: %d Message: %s" % (response["status_code"], response["status_message"]))
        return None

# Streamlit layout
st.sidebar.title("DataForSEO API Parameters")
api_key = st.sidebar.text_input("API Key", type="password")
generate_button = st.sidebar.button("Generate All")
reset_button = st.sidebar.button("Reset")

# Filters
url_from_not_contain = st.sidebar.text_input("URL from does not contain (comma-separated)")
is_lost = st.sidebar.checkbox("Is Lost", value=False)
dofollow = st.sidebar.checkbox("Dofollow", value=True)
backlink_spam_score = st.sidebar.slider("Backlink Spam Score ≤", 0, 100, 10)
page_from_language = st.sidebar.selectbox("Page From Language", ['en', 'other'])

# Prepare filters for API call
filters = []
if url_from_not_contain:
    for url in url_from_not_contain.split(','):
        filters.append(["url_from", "not_like", url.strip()])
if is_lost:
    filters.append(["is_lost", "=", is_lost])
if dofollow:
    filters.append(["dofollow", "=", dofollow])
filters.append(["backlink_spam_score", "<=", backlink_spam_score])
filters.append(["page_from_language", "=", page_from_language])

# Main app layout
col1, col2 = st.columns(2)

with col1:
    st.header("Input")
    target_url = st.text_input("Enter the target URL")

with col2:
    st.header("Output")

# Generate CSV and download button
if generate_button and target_url:
    data = get_backlinks(api_key, target_url, filters)
    if data:
        df = pd.DataFrame(data)
        csv = df.to_csv(index=False)
        b64 = base64.b64encode(csv.encode()).decode()
        href = f'<a href="data:file/csv;base64,{b64}" download="backlinks.csv">Download CSV file</a>'
        st.markdown(href, unsafe_allow_html=True)

# Reset functionality
if reset_button:
    st.experimental_rerun()