Last active
August 19, 2023 12:45
-
-
Save fabriziosalmi/5319e4839e37a56ba0246b2756ca85b6 to your computer and use it in GitHub Desktop.
Create sqlite database from blacklists repo to save historical data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
import requests | |
from datetime import datetime | |
import os | |
# SQLite setup | |
DB_PATH = "blacklisted.db" | |
def setup_db(): | |
conn = sqlite3.connect(DB_PATH) | |
c = conn.cursor() | |
c.execute('''CREATE TABLE IF NOT EXISTS domains | |
(fqdn TEXT, timestamp TEXT)''') | |
conn.commit() | |
conn.close() | |
# Fetch the text file | |
def fetch_blacklist_txt(): | |
URL = "https://get.domainsblacklists.com/blacklist.txt" | |
response = requests.get(URL) | |
with open("blacklist.txt", "w") as file: | |
file.write(response.text) | |
# Insert data into SQLite | |
def insert_data(): | |
conn = sqlite3.connect(DB_PATH) | |
c = conn.cursor() | |
with open("blacklist.txt", "r") as file: | |
for line in file: | |
fqdn = line.strip() | |
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
c.execute("INSERT INTO domains (fqdn, timestamp) VALUES (?, ?)", (fqdn, timestamp)) | |
conn.commit() | |
conn.close() | |
# Main function to execute all tasks | |
def main(): | |
setup_db() | |
fetch_blacklist_txt() | |
insert_data() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment