From 9101c7147fe5eaefa5a7eb4683bd8940bc051101 Mon Sep 17 00:00:00 2001 From: Notoric Date: Sat, 1 Jun 2024 20:29:48 +0100 Subject: [PATCH] Added venv and basic scripts to connect to database --- .gitignore | 2 ++ script.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .gitignore create mode 100644 script.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f9fab25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.json +venv/* \ No newline at end of file diff --git a/script.py b/script.py new file mode 100644 index 0000000..401e023 --- /dev/null +++ b/script.py @@ -0,0 +1,50 @@ +import time +import json +import pymongo +import requests +from datetime import datetime, timedelta + +print(" _____ _ _ _____ ") +print(" / ____| \ | |/ ____|") +print(" | (___ | \| | |") +print(" \___ \| | |") +print(" ____) | |\ | |____") +print(" |_____/|_| \_|\_____|") + +# Load config +print("Loading config...") + +with open('config.json') as f: + config = json.load(f) + +print("Config loaded!") + +mongo_url = f"mongodb://{config['mongo']['host']}:{config['mongo']['port']}/" +mongo_db = config['mongo']['db'] + +# Connect to MongoDB +print("Connecting to MongoDB...") + +client = pymongo.MongoClient(mongo_url) +db = client[mongo_db] + +print("Connected to MongoDB!") + +# Create collections if they dont exist +def create_collections(): + collections = ['weather', 'newsfeed'] + for collection in collections: + if collection not in db.list_collection_names(): + db.create_collection(collection) + print(f"Created collection {collection}") + +# Get weather data + +def get_weather(): + url = f"http://api.openweathermap.org/data/2.5/weather?q={config['weather']['city']}&appid={config['weather']['api_key']}&units=metric" + response = requests.get(url) + data = response.json() + return data + +create_collections() +