import streamlit as st import pandas as pd import requests from client import RestClient import io import base64 import requests # Function to call the DataForSEO API with Basic Authentication def get_backlinks(api_login, api_key, target_url, filters): # Encoding credentials encoded_credentials = base64.b64encode(f"{api_login}:{api_key}".encode()).decode() # Setting headers with Basic Authentication headers = { 'Authorization': f'Basic {encoded_credentials}' } # Prepare post data post_data = { 0: { "target": target_url, "limit": 1000, "mode": "as_is", "filters": filters } } # Making the API request response = requests.post("https://api.dataforseo.com/v3/backlinks/backlinks/live", json=post_data, headers=headers) if response.status_code == 200: return response.json()['results'] else: st.error(f"Error: Code: {response.status_code} Message: {response.json().get('status_message')}") return None # Streamlit layout st.sidebar.title("DataForSEO API Parameters") api_login = st.sidebar.text_input("API Login", value="josh@expertphotography.com") api_key = st.sidebar.text_input("API Key", type="password") generate_button = st.sidebar.button("Generate All") reset_button = st.sidebar.button("Reset") # Make sure to pass api_login to the get_backlinks function if generate_button and target_url: data = get_backlinks(api_login, api_key, target_url, filters) # [Your existing code to handle the data and provide a download link for the CSV] # 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") # [Your code for displaying output] # Generate CSV and download button if generate_button and target_url: data = get_backlinks(api_login, 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'Download CSV file' st.markdown(href, unsafe_allow_html=True) # Reset functionality if reset_button: st.experimental_rerun()