Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session improvements - Add support for the new auth api and backbone of Unmanic plugin repos #451

Merged
merged 5 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .idea/dataSources.local.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .idea/runConfigurations/Run_Unmanic.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions .idea/runConfigurations/Run_Unmanic_Dev.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/unmanic.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion devops/update-docker-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __script_path=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd );
__project_root=$(realpath ${__script_path}/..);

function docker_env {
docker pull lsiobase/ubuntu:focal
docker pull josh5/unmanic:latest
docker run --rm -ti \
--workdir=/app \
--entrypoint='' \
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ marshmallow==3.13.0
peewee>=3.14.4
peewee_migrate==1.6.1
psutil==5.8.0
requests>=2.25.1
requests>=2.28.2
requests_toolbelt>=0.9.1
py-cpuinfo>=7.0.0

Expand Down
9 changes: 5 additions & 4 deletions unmanic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ def __init__(self, config_path=None, **kwargs):
self.ui_port = 8888

# Set default directories
self.config_path = os.path.join(common.get_home_dir(), '.unmanic', 'config')
self.log_path = os.path.join(common.get_home_dir(), '.unmanic', 'logs')
self.plugins_path = os.path.join(common.get_home_dir(), '.unmanic', 'plugins')
self.userdata_path = os.path.join(common.get_home_dir(), '.unmanic', 'userdata')
home_directory = common.get_home_dir()
self.config_path = os.path.join(home_directory, '.unmanic', 'config')
self.log_path = os.path.join(home_directory, '.unmanic', 'logs')
self.plugins_path = os.path.join(home_directory, '.unmanic', 'plugins')
self.userdata_path = os.path.join(home_directory, '.unmanic', 'userdata')

# Configure debugging
self.debugging = False
Expand Down
10 changes: 9 additions & 1 deletion unmanic/libs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@


def get_home_dir():
# Attempt to get the HOME_DIR environment variable
home_dir = os.environ.get('HOME_DIR')
if home_dir is None:
# If HOME_DIR is unset, empty, or specifically set to use the home directory
if not home_dir:
# Expand the tilde to the user's home directory
home_dir = os.path.expanduser("~")
else:
# For any value of HOME_DIR, ensure tilde and relative paths are correctly handled
# os.path.expanduser will handle tilde but won't affect absolute paths
# os.path.abspath will convert a relative path to an absolute path
home_dir = os.path.abspath(os.path.expanduser(home_dir))
return home_dir


Expand Down
40 changes: 21 additions & 19 deletions unmanic/libs/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
OR OTHER DEALINGS IN THE SOFTWARE.

"""
import base64
import hashlib
import json
import os
Expand All @@ -54,11 +55,6 @@ class PluginsHandler(object, metaclass=SingletonType):
"""
version: int = 2

"""
Set the default repo to main repo
"""
default_repo = 'https://api.unmanic.app/api/v1/unmanic-plugin-repo/uuid'

def __init__(self, *args, **kwargs):
self.settings = config.Config()
unmanic_logging = unlogger.UnmanicLogger.__call__()
Expand Down Expand Up @@ -88,28 +84,27 @@ def get_plugin_download_cache_path(self, plugin_id, plugin_version):
plugin_directory = self.settings.get_plugins_path()
return os.path.join(plugin_directory, "{}-{}.zip".format(plugin_id, plugin_version))

def get_default_repo(self):
return self.default_repo
@staticmethod
def get_default_repo():
return "default"

def get_plugin_repos(self):
"""
Returns a list of plugin repos

:return:
"""
session = Session()
uuid = session.get_installation_uuid()
default_repo = self.get_default_repo()
repo_list = [
{
"path": "{}/{}".format(default_repo, uuid)
"path": default_repo
}
]

repos = PluginRepos.select().order_by(PluginRepos.id.asc())
for repo in repos:
repo_dict = repo.model_to_dict()
if repo_dict.get('path') == "{}/{}".format(default_repo, uuid):
if repo_dict.get('path') == default_repo:
continue
repo_list.append(repo_dict)

Expand Down Expand Up @@ -138,11 +133,17 @@ def fetch_remote_repo_data(self, repo_path):
# Fetch remote JSON file
session = Session()
uuid = session.get_installation_uuid()
post_data = {
"uuid": uuid,
"repo_url": repo_path
}
return session.api_post(1, 'unmanic-plugin-repo/uuid/{}'.format(uuid), post_data)
level = session.get_supporter_level()
repo = base64.b64encode(repo_path.encode('utf-8')).decode('utf-8')
api_path = f'plugin_repos/get_repo_data/uuid/{uuid}/level/{level}/repo/{repo}'
data, status_code = session.api_get(
'unmanic-api',
1,
api_path,
)
if status_code >= 500:
self._log(f"Failed to fetch plugin repo from '{api_path}'. Code:{status_code}", level="debug")
return data

def update_plugin_repos(self):
"""
Expand All @@ -169,7 +170,6 @@ def update_plugin_repos(self):
json.dump(repo_data, f, indent=4)
except json.JSONDecodeError as e:
self._log("Unable to update plugin repo '{}'.".format(repo_path), str(e), level="error")

return True

def get_settings_of_all_installed_plugins(self):
Expand Down Expand Up @@ -307,7 +307,7 @@ def read_remote_changelog_file(self, changelog_url):

def notify_site_of_plugin_install(self, plugin):
"""
Notify the unmanic.app site API of the install.
Notify the unmanic.app site API of the installation.
This is used for metric stats so that we can get a count of plugin downloads.

:param plugin:
Expand All @@ -316,14 +316,16 @@ def notify_site_of_plugin_install(self, plugin):
# Post
session = Session()
uuid = session.get_installation_uuid()
level = session.get_supporter_level()
post_data = {
"uuid": uuid,
"level": level,
"plugin_id": plugin.get("plugin_id"),
"author": plugin.get("author"),
"version": plugin.get("version"),
}
try:
repo_data = session.api_post(1, 'unmanic-plugin/install', post_data)
repo_data = session.api_post('unmanic-api', 1, 'plugin_repos/record_install', post_data)
if not repo_data.get('success'):
session.register_unmanic()
except Exception as e:
Expand Down
Loading
Loading