diff --git a/backgroundchanger/__main__.py b/backgroundchanger/__main__.py index f87a5ba..7f101a4 100644 --- a/backgroundchanger/__main__.py +++ b/backgroundchanger/__main__.py @@ -48,6 +48,11 @@ def parse_arguments(): key_group.add_argument('-s', '--secret', help='Provide the Unsplash API secret key') + filter_group = args.add_argument_group('filter_group') + filter_group.add_argument('-c', '--collections', help="Give collection ID(s) to fetch image from.", nargs='+') + filter_group.add_argument('-t', '--topics', help="Give topic IDs to fetch image from.", nargs='+') + filter_group.add_argument('-q', '--query', help="Give search query to fetch image from.", nargs=1) + filter_group.add_argument('--username', help="Limit selection to photos matching a search term.") return args.parse_args() @@ -66,6 +71,21 @@ def do_wal(photo, do_colors=True): utils.reload_gala() +def get_filter_params(filter_params) -> dict: + filters = {} + if filter_params.collections: + filters['collections'] = filter_params.collections + if filter_params.topics: + filters['topics'] = filter_params.topics + if filter_params.query: + filters['query'] = filter_params.query + if filter_params.username: + filters['username'] = filter_params.username + if 'query' in filters and ('collections' in filters or 'topics' in filters): + raise Exception("You can’t use the collections or topics filtering with query parameters in the same request") + return filters + + def main(): keys = {} parsed = parse_arguments() @@ -73,6 +93,7 @@ def main(): keys['access_key'] = parsed.access if parsed.secret: keys['secret_key'] = parsed.secret + filter_params = get_filter_params(parsed) try: start_log() @@ -92,7 +113,7 @@ def main(): keys = utils.get_keys() logging.debug('Got the keys: {}'.format(keys)) api_object = api.Api(keys) - photo = api_object.get_random() + photo = api_object.get_random(filter_params) do_wal(photo, set_colors) diff --git a/backgroundchanger/api.py b/backgroundchanger/api.py index 3511200..cd73c4d 100644 --- a/backgroundchanger/api.py +++ b/backgroundchanger/api.py @@ -16,14 +16,22 @@ def __init__(self, keys): self.secret_key = keys['secret_key'] self.version_header = {'Accept-Version': 'v1'} - def get_random(self): + def get_random(self, params=None): """ gets a random photo returns the full path to the downloaded photo """ - # gets the url for the main request - url = self.__api_route__ + '/photos/random?client_id={}'.format(self.access_key) + if params is None: + params = {} + selection_params = [] + for key, ele in params.items(): + if ele: + selection_params.append('{}={}'.format(key, ','.join(ele))) + url = self.__api_route__ + '/photos/random?client_id={}'.format( + self.access_key) # gets the url for the main request + final_params = '&' + '&'.join(selection_params) + url = url + final_params res = self.request('GET', url, headers=self.version_header).json() # we need the screen size for getting the correct image size diff --git a/backgroundchanger/utils.py b/backgroundchanger/utils.py index 9a6af1e..4eb205b 100644 --- a/backgroundchanger/utils.py +++ b/backgroundchanger/utils.py @@ -8,6 +8,7 @@ from tkinter import Tk from . import config + def reload_gala(): """ this only is necessary for systems using the Gala @@ -17,8 +18,8 @@ def reload_gala(): gets fixed """ Popen(['gala', '-r'], - stderr=DEVNULL, - stdout=DEVNULL) + stderr=DEVNULL, + stdout=DEVNULL) def get_keys(): @@ -72,15 +73,15 @@ def get_background_cmd(photo_name: str): logging.info('Linux OS found; finding distro') dist = distro_name() logging.info('Found {}'.format(dist)) - if 'elementary' in dist or 'Ubuntu' in dist: + if 'elementary' in dist or 'Ubuntu' in dist or 'Pop!_OS' in dist: return [ - 'gsettings', + '/usr/bin/gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file://' + photo_name ] - elif not run('feh --help > /dev/null'): # Actually true, 0 (success) is cast to false. + elif not run('feh --help > /dev/null'): # Actually true, 0 (success) is cast to false. logging.info('Found Feh') return [ 'feh', diff --git a/tests/test_utils.py b/tests/test_utils.py index 09674fe..38bf8dc 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -8,6 +8,7 @@ from backgroundchanger import utils from open_file_mock import MockOpen + @patch('backgroundchanger.utils.platform_system') class TestExceptions(unittest.TestCase): def test_mac_exception(self, mock_platform): @@ -15,7 +16,6 @@ def test_mac_exception(self, mock_platform): self.assertRaises( ValueError, utils.get_background_cmd, "./tests/test.png") - def test_win_exception(self, mock_platform): mock_platform.return_value = 'Windows' self.assertRaises( @@ -26,7 +26,7 @@ def test_linux_cmd(self, mock_distro, mock_platform): mock_distro.return_value = 'Ubuntu' mock_platform.return_value = 'Linux' res = utils.get_background_cmd("./tests/test.png") - assert res[0] == 'gsettings' + assert res[0] in ('gsettings', '/usr/bin/gsettings') @patch('backgroundchanger.utils.Tk') @@ -47,19 +47,19 @@ def test_get_keys(): "access_key" : "ak", "secret_key" : "sk" }''' - config_file_path = path.join(Path.home(), '.config', 'python-backgroundchanger','unsplash_keys.json') + config_file_path = path.join(Path.home(), '.config', 'python-backgroundchanger', 'unsplash_keys.json') open_mock.set_read_data_for(path=config_file_path, data=jsonStr) keys = utils.get_keys() - assert keys['access_key'] =='ak' - assert keys['secret_key'] =='sk' + assert keys['access_key'] == 'ak' + assert keys['secret_key'] == 'sk' @patch('backgroundchanger.utils.Popen') def test_reload_gala(mock_popen): utils.reload_gala() mock_popen.assert_called_once_with(['gala', '-r'], - stderr=subprocess.DEVNULL, - stdout=subprocess.DEVNULL) + stderr=subprocess.DEVNULL, + stdout=subprocess.DEVNULL) def test_copy_file(): @@ -80,6 +80,6 @@ def test_copy_file(): @patch('backgroundchanger.utils.call') def test_change_background(mock_call): utils.get_background_cmd = MagicMock() - utils.get_background_cmd.return_value = ['dummy','cmd'] + utils.get_background_cmd.return_value = ['dummy', 'cmd'] utils.change_background("./tests/test.png") mock_call.assert_called_once_with(['dummy', 'cmd'])