-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed entertainment dependency, started working on settings
- Loading branch information
Aleksa Ognjanovic
committed
Apr 5, 2020
1 parent
82e7648
commit 7c69063
Showing
11 changed files
with
446 additions
and
121 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from requests import get | ||
from json import loads | ||
import socket | ||
|
||
|
||
# Returns hostname of machine | ||
def getHostname(): | ||
return socket.getfqdn() | ||
|
||
|
||
# Returns local IP address | ||
def getLocalIP(): | ||
return socket.gethostbyname(socket.gethostname()) | ||
|
||
|
||
class Location: | ||
def __init__(self): | ||
self.location_info = loads(get("http://ip-api.com/json/").text) | ||
|
||
@property | ||
def city(self): | ||
return self.location_info["city"] | ||
|
||
@property | ||
def public_ip(self): | ||
return self.location_info["query"] | ||
|
||
@property | ||
def timezone(self): | ||
return self.location_info["timezone"] | ||
|
||
@property | ||
def country(self): | ||
return self.location_info["country"] | ||
|
||
@property | ||
def country_code(self): | ||
return self.location_info["countryCode"] | ||
|
||
@property | ||
def region(self): | ||
return self.location_info["regionName"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import sqlite3 | ||
|
||
CREATE_TABLE = \ | ||
"CREATE TABLE IF NOT EXISTS users ("\ | ||
"id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, "\ | ||
"username TEXT NOT NULL UNIQUE);" | ||
|
||
|
||
TYPES = { | ||
"int": "INTEGER", | ||
"str": "TEXT", | ||
"bool": "BOOL", | ||
"datetime": "DATE", | ||
"float": "REAL", | ||
"Percentage": "REAL" | ||
} | ||
|
||
SET = "" | ||
|
||
TABLES = "PRAGMA table_info(users);" | ||
|
||
|
||
class UserSettings(sqlite3.Connection): | ||
"""Saves user settings to database | ||
:param username: username | ||
:param location: full path to save the database | ||
""" | ||
|
||
def __init__(self, username, location, *args): | ||
super().__init__(location, *args) | ||
self.username = username | ||
self.cursor = self.cursor() | ||
self.cursor.execute(CREATE_TABLE) | ||
|
||
self.cursor.execute( | ||
"SELECT username FROM users WHERE username=?", | ||
(username,) | ||
) | ||
|
||
if not self.cursor.fetchone(): | ||
self.cursor.execute( | ||
"INSERT INTO users (username) VALUES (?)", | ||
(username,) | ||
) | ||
|
||
def _get_type(self, value): | ||
""" | ||
:param value: value | ||
:returns: SQLite3 type equivalent | ||
""" | ||
val_type = type(value).__name__ | ||
return TYPES[val_type] if val_type in TYPES.keys() else "BLOB" | ||
|
||
def set(self, col, value): | ||
"""Sets a value for setting | ||
:param col: setting | ||
:param value: value for setting | ||
""" | ||
columns = [i[1] for i in self.cursor.execute(TABLES)] | ||
|
||
if not col in columns: | ||
self.cursor.execute( | ||
f"ALTER TABLE users ADD COLUMN {col} {self._get_type(value)}" | ||
) | ||
|
||
self.cursor.execute( | ||
f"UPDATE users SET {col}=? WHERE username=?", | ||
(value, self.username) | ||
) | ||
|
||
def setsave(self, col, value): | ||
"""Sets a value for setting | ||
:param col: setting | ||
:param value: value for setting | ||
""" | ||
self.set(col, value) | ||
self.save() | ||
|
||
def save(self): | ||
"""Saves changes to the database""" | ||
self.commit() |
Oops, something went wrong.