Skip to content

Commit

Permalink
Added config class to make it easier to parse variables into correct …
Browse files Browse the repository at this point in the history
…data type
  • Loading branch information
parker1992 committed Sep 10, 2022
1 parent ddde009 commit d56dd29
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ cython_debug/
.DS_Store
static/
iptv.m3u
epg.xml
2 changes: 1 addition & 1 deletion config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ M3U_HOST =
# The port that the app is listening on. If running locally, set it to the same value as M3U_PORT.
LISTEN_PORT =

# Use https instead of http for URLs (if you are behind a reverse proxy), True or leave empty
# Use https instead of http for URLs (if you are behind a reverse proxy), True/False; default False
USE_HTTPS =

# How often, in minutes, should the app pull the latest version of the m3u file.
Expand Down
38 changes: 38 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
from configparser import ConfigParser
from distutils.util import strtobool

class Config:
_config: ConfigParser

def __init__(self, root_path: str):
self._config = ConfigParser(allow_no_value=True)
self._config.read(os.path.join(root_path, 'config.ini'))

@property
def M3U_LOCATION(self):
return os.getenv('M3U_LOCATION', self._config.get('APP', 'M3U_LOCATION'))

@property
def XMLTV_LOCATION(self):
return os.getenv('XMLTV_LOCATION', self._config.get('APP', 'XMLTV_LOCATION')) or ''

@property
def M3U_PORT(self):
return int(os.getenv('M3U_PORT', self._config.get('APP', 'M3U_PORT')) or 0)

@property
def M3U_HOST(self):
return os.getenv('M3U_HOST', self._config.get('APP', 'M3U_HOST'))

@property
def LISTEN_PORT(self):
return int(os.getenv('LISTEN_PORT', self._config.get('APP', 'LISTEN_PORT')))

@property
def USE_HTTPS(self):
return strtobool(os.getenv('USE_HTTPS', self._config.get('APP', 'USE_HTTPS')) or 'False') == 1

@property
def RELOAD_INTERVAL_MIN(self):
return int(os.getenv('RELOAD_INTERVAL_MIN', self._config.get('APP', 'RELOAD_INTERVAL_MIN')))
37 changes: 14 additions & 23 deletions proxy.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import json
import os
import logging
from config import Config
import parser
import xmltv
from http import HTTPStatus
from configparser import ConfigParser
from threading import Timer
from flask import Flask, Response
from requests import get
from urllib.parse import urlparse

app = Flask(__name__)
config = ConfigParser(allow_no_value=True)
config.read(os.path.join(app.root_path, 'config.ini'))
config = Config(app.root_path)
m3u_parser = parser.Parser()
xmltv = xmltv.Xmltv()

Expand Down Expand Up @@ -92,32 +90,25 @@ def reload_timer():
except Exception as err:
app.logger.exception('END: Error reloading m3u file', err)

Timer(60 * int(get_variable(config, 'RELOAD_INTERVAL_MIN')), lambda: reload_timer(config)).start()
Timer(60 * config.RELOAD_INTERVAL_MIN, lambda: reload_timer(config)).start()

def reload(config: ConfigParser):
def reload(config: Config):
m3u_parser.parse_m3u(
get_variable(config, 'M3U_LOCATION'),
get_variable(config, 'M3U_HOST'),
int(get_variable(config, 'M3U_PORT') or 0),
bool(get_variable(config, 'USE_HTTPS') or False),
config.M3U_LOCATION,
config.M3U_HOST,
config.M3U_PORT,
config.USE_HTTPS,
os.path.join(app.static_folder, 'iptv.m3u')
)

xmltv.fetch_xmltv(
str(get_variable(config, 'XMLTV_LOCATION')) or '',
get_variable(config, 'M3U_HOST'),
int(get_variable(config, 'M3U_PORT') or 0),
bool(get_variable(config, 'USE_HTTPS') or False),
config.XMLTV_LOCATION,
config.M3U_HOST,
config.M3U_PORT,
config.USE_HTTPS,
os.path.join(app.static_folder, 'epg.xml')
)

def get_variable(config: ConfigParser, var: str):
"""
This gets the configs first from environment variables,
and if it's not set, it will check the config.ini file.
"""
return os.getenv(var, config.get('APP', var))

if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
Expand All @@ -126,8 +117,8 @@ def get_variable(config: ConfigParser, var: str):
reload_timer()

if __name__ == '__main__':
host = get_variable(config, 'M3U_HOST')
host = config.M3U_HOST

reload_timer()

app.run(host='0.0.0.0', port=int(get_variable(config, 'LISTEN_PORT')))
app.run(host='0.0.0.0', port=config.LISTEN_PORT)
3 changes: 1 addition & 2 deletions xmltv.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ def fetch_xmltv(self, xmltv_location: str, host:str, port: int, use_https: bool,
content = input.read()

with open(str(output_path), 'w') as output:

port_str = f':{str(port)}' if port != 0 else ''

# Prefix all URLs in the xml file with the proxy endpoints.
content = re.sub(r'(http|https)', rf'http://{host}{port_str}/proxy/data/\1', content, flags=re.M)
if use_https == True:
content = re.sub(rf'(http://{host})',rf'https://{host}', content, flags=re.M)
content = re.sub(rf'(http://{host})', rf'https://{host}', content, flags=re.M)
output.write(content)

0 comments on commit d56dd29

Please sign in to comment.