File size: 1,589 Bytes
31448b7
8167fe5
 
 
31448b7
8167fe5
 
ab9ff1e
8167fe5
 
 
31448b7
8167fe5
 
 
 
2d4d1cc
8167fe5
 
 
 
 
31448b7
8167fe5
 
 
 
 
 
2d4d1cc
8167fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31448b7
 
8167fe5
 
 
 
 
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
import requests
import sqlite3
import pandas as pd
import matplotlib.pyplot as plt

# Fetch data from the non-SSL API (example API URL)
def fetch_data_from_api():
    api_url = "http://example.com/api"  # Replace with your actual non-SSL API URL
    response = requests.get(api_url)
    data = response.json()  # Assumes the response is in JSON format
    return data

# Store data in SQLite
def store_data_in_sqlite(data):
    conn = sqlite3.connect('data.db')
    cursor = conn.cursor()

    # Create table if it does not exist
    cursor.execute('''CREATE TABLE IF NOT EXISTS api_data (
                        id INTEGER PRIMARY KEY AUTOINCREMENT,
                        name TEXT,
                        value INTEGER)''')

    # Insert data into the database
    for item in data:
        cursor.execute("INSERT INTO api_data (name, value) VALUES (?, ?)", (item['name'], item['value']))
    
    conn.commit()
    conn.close()

# Function to retrieve data for the chart
def get_data_for_chart():
    conn = sqlite3.connect('data.db')
    df = pd.read_sql_query("SELECT name, value FROM api_data", conn)
    conn.close()
    return df

# Function to plot a chart
def plot_chart(df):
    plt.figure(figsize=(8,6))
    plt.bar(df['name'], df['value'])
    plt.xlabel('Name')
    plt.ylabel('Value')
    plt.title('API Data Visualization')
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    # Fetch, store data and visualize
    data = fetch_data_from_api()
    store_data_in_sqlite(data)
    df = get_data_for_chart()
    plot_chart(df)