Skip to content
Open
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
23 changes: 22 additions & 1 deletion backgroundchanger/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -66,13 +71,29 @@ 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()
if parsed.access:
keys['access_key'] = parsed.access
if parsed.secret:
keys['secret_key'] = parsed.secret
filter_params = get_filter_params(parsed)

try:
start_log()
Expand All @@ -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)


Expand Down
14 changes: 11 additions & 3 deletions backgroundchanger/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions backgroundchanger/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tkinter import Tk
from . import config


def reload_gala():
"""
this only is necessary for systems using the Gala
Expand All @@ -17,8 +18,8 @@ def reload_gala():
gets fixed
"""
Popen(['gala', '-r'],
stderr=DEVNULL,
stdout=DEVNULL)
stderr=DEVNULL,
stdout=DEVNULL)


def get_keys():
Expand Down Expand Up @@ -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',
Expand Down
16 changes: 8 additions & 8 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
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):
mock_platform.return_value = 'Darwin'
self.assertRaises(
ValueError, utils.get_background_cmd, "./tests/test.png")


def test_win_exception(self, mock_platform):
mock_platform.return_value = 'Windows'
self.assertRaises(
Expand All @@ -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')
Expand All @@ -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():
Expand All @@ -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'])