Skip to content

Commit

Permalink
Fixed entertainment dependency, started working on settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksa Ognjanovic committed Apr 5, 2020
1 parent 82e7648 commit 7c69063
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 121 deletions.
66 changes: 61 additions & 5 deletions PiTV/application.glade
Original file line number Diff line number Diff line change
Expand Up @@ -324,21 +324,20 @@ Author: GrbavaCigla
<placeholder/>
</child>
<child>
<object class="GtkBox" id="main_divider">
<property name="name">main_divider</property>
<object class="GtkBox" id="home_divider">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">6</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkStack" id="main_stack">
<object class="GtkStack" id="home_stack">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="transition_type">slide-up-down</property>
<child>
<object class="GtkScrolledWindow" id="home_scroll_view">
<object class="GtkScrolledWindow" id="home_trending_scroll_view">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">never</property>
Expand Down Expand Up @@ -383,14 +382,71 @@ Author: GrbavaCigla
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="home_settings">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="name">page2</property>
<property name="title" translatable="yes">page2</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkBox" id="settings_divider">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">6</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkStack" id="settings_stack">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="transition_type">slide-up-down</property>
<child>
<object class="GtkScrolledWindow" id="settings_general_scroll_view">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="shadow_type">none</property>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
<packing>
<property name="name">page0</property>
<property name="title" translatable="yes">page0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</interface>
78 changes: 41 additions & 37 deletions PiTV/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
from threading import Thread
from screeninfo import get_monitors
import requests
from entertainment import Weather, Location
from weather import Weather
from location import Location
from utils import check_internet
from sidebar import SideBar, ListTile, WeatherBox
from category import Category


# Bypass linters
if True:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib, Gio


HOST = "http://127.0.0.1"
HOME_DIR = os.path.dirname(os.path.abspath(__file__))

MONITOR_WIDTH = get_monitors()[0].width
Expand All @@ -32,7 +32,7 @@
# "TV Shows",
# "TV",
# "Songs"
# "Settings"
"Settings"
]

SIDEBAR_ICONS = [
Expand All @@ -41,7 +41,7 @@
# "TV Shows",
# "TV",
# "Songs"
# "Settings"
"open-menu"
]


Expand Down Expand Up @@ -69,8 +69,11 @@ def __init__(self):

# Website host URL (As I currenly don't have website,
# localhost is development sollution)
# Switched to heroku basic plan (free)
self.host = "https://pitv.herokuapp.com"
# Switched to heroku free plan
# Switched to Azure donated by Maker NS
self.host = HOST

self.fetch_weather = True

self.login_init() # Switch this back after debugging

Expand All @@ -79,17 +82,22 @@ def __init__(self):
def home_refresh(self):
# Fetch weather info
# TODO:Prompt user for his openweathermap api key
if not self.weather_info:
self.weather_info = Weather(
self.openweather_apikey,
self.unit_system,
self.city_and_country
)
self.weather_box.set_weather_object(self.weather_info, update=True)

else:
self.weather_box.update_data()
self.weather_box.refresh()
if self.fetch_weather:
if not self.weather_info:
self.weather_info = Weather(
self.openweather_apikey,
self.unit_system,
self.city_and_country
)
self.weather_box.set_weather_object(
self.weather_info,
update=True
)

else:
self.weather_box.update_data()
self.weather_box.refresh()

GLib.timeout_add(
REFRESH_MILLS,
Expand All @@ -99,30 +107,37 @@ def home_refresh(self):

def recheck_network(self):
self.network_state = check_internet()

GLib.idle_add(self.current_thread.join)

def home_init(self):
# Setting objects public variables to their object
self.main_divider = self.builder.get_object("main_divider")
self.main_stack = self.builder.get_object("main_stack")
self.home_divider = self.builder.get_object("home_divider")
self.home_stack = self.builder.get_object("home_stack")
self.category_view = self.builder.get_object("category_view")
self.home_scroll_view = self.builder.get_object("home_scroll_view")
self.home_trending_scroll_view = self.builder.get_object(
"home_trending_scroll_view")

# Scrolling adjustment
self.category_view.set_focus_vadjustment(
self.home_scroll_view.get_vadjustment()
self.home_trending_scroll_view.get_vadjustment()
)

# Add Sidebar to window and place it in the beginning
self.sidebar = SideBar(self.main_stack)
self.main_divider.pack_start(self.sidebar, False, False, 0)
self.main_divider.reorder_child(self.sidebar, 0)
self.sidebar = SideBar(self.home_stack)
self.home_divider.pack_start(self.sidebar, False, False, 0)
self.home_divider.reorder_child(self.sidebar, 0)

# Fetch some environment variables
self.openweather_apikey = os.environ.get("OPEN_WEATHER_API_KEY")
self.unit_system = os.environ.get("UNIT_SYSTEM")

# Check if they are empty
if not self.unit_system:
self.unit_system = "metric"

if not self.openweather_apikey:
self.fetch_weather = False

# Setting weather_info to None
self.weather_info = None

Expand All @@ -146,7 +161,7 @@ def home_init(self):
# Make formatted location for open weather map
self.city_and_country = "{},{}".format(
self.location_info.city,
self.location_info.countryCode
self.location_info.country_code
)

# Fetch all data that needs to be refreshed every 2 minutes
Expand Down Expand Up @@ -286,17 +301,6 @@ def validate_signup(self):
self.signup_error_label.set_text(
"Error code:" + str(response.status_code))

# def on_sidebar_row_selected(self, listbox, listbox_row):
# index = listbox_row.get_index()
# item = SIDEBAR_LABELS[index]
# getattr(self, item.lower()+"_stack")()

# def home_stack(self):
# print("Home")

# def movies_stack(self):
# print("Movies")


if __name__ == "__main__":
app = PiTV()
Expand Down
42 changes: 42 additions & 0 deletions PiTV/location.py
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"]
89 changes: 89 additions & 0 deletions PiTV/settings.py
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()
Loading

0 comments on commit 7c69063

Please sign in to comment.