Skip to content

Commit

Permalink
Merge branch 'v0.5.7-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
milouse committed Jul 19, 2021
2 parents 5eaf575 + c614c73 commit 9630456
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 141 deletions.
2 changes: 1 addition & 1 deletion chwall/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.6"
__version__ = "0.5.7"
30 changes: 22 additions & 8 deletions chwall/fetcher/natgeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@

def fetch_pictures(config):
pictures = {}
url = "https://www.nationalgeographic.com/photography/photo-of-the-day/" \
"_jcr_content/.gallery.{}-{}.json"
t = date.today()
data = requests.get(url.format(t.year, t.strftime("%m"))).json()
for p in data["items"]:
px = p["image"]["uri"]
year = t.year
month_label = ["january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november",
"december"]
month_idx = t.month - 2
if month_idx == -1:
month_idx = 11
month = month_label[month_idx]
final_uri = "https://www.nationalgeographic.co.uk/page-data/" \
f"photo-of-the-day/{year}/{month}/page-data.json"
data = requests.get(final_uri).json() \
.get("result", {}).get("pageContext", {}) \
.get("node", {}).get("data", {}).get("content", {})
pic_url = "https://www.nationalgeographic.co.uk/photo-of-the-day/" \
f"{year}/{month}?image="
for p in data.get("images", []):
purl = p["entity"]["mediaImage"]["url"]
px = f"https://static.nationalgeographic.co.uk{purl}"
pid = purl[purl.rindex("/")+1:purl.rindex(".")]
pictures[px] = {
"image": px,
"copyright": "{} - {}".format(p["image"]["title"],
p["image"]["credit"]),
"url": p["pageUrl"],
"copyright": "{} - {}".format(p["entity"]["mediaImage"]["title"],
p["entity"]["credit"]),
"url": pic_url + pid,
"type": "National Geographic"
}
return pictures
Expand Down
14 changes: 12 additions & 2 deletions chwall/fetcher/unsplash.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def fetch_pictures(config):
return {}
width = us_conf.get("width", 1600)
nb_pic = us_conf.get("count", 10)
params = ["w=%d" % width, "count=%d" % nb_pic]
ct_fltr = us_conf.get("content_filter", "low")
params = ["count=%d" % nb_pic, "content_filter=%s" % ct_fltr]
if "query" in us_conf:
params.append("query=" + us_conf["query"])
if "collections" in us_conf:
Expand All @@ -34,7 +35,7 @@ def fetch_pictures(config):
final_uri = "{}?{}".format(url, "&".join(params))
data = requests.get(final_uri).json()
for p in data:
px = p["urls"]["custom"]
px = "{u}&w={w}".format(u=p["urls"]["raw"], w=width)
if p["description"] is None:
label = _("Picture")
else:
Expand Down Expand Up @@ -69,6 +70,15 @@ def preferences():
"widget": "number",
"default": 10
},
"content_filter": {
"widget": "select",
"values": [
("low", _("Low")),
("high", _("High"))
],
"default": "low",
"label": _("Content filtering")
},
"access_key": {
"widget": "text",
"label": _("API access key")
Expand Down
31 changes: 10 additions & 21 deletions chwall/gui/preferences.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import pkgutil
from importlib import import_module

from chwall.utils import read_config, write_config, reset_pending_list, \
cleanup_cache, ServiceFileManager, BASE_CACHE_PATH
count_broken_pictures_in_cache, cleanup_cache, \
compute_cache_size, ServiceFileManager

import gi
gi.require_version("Gtk", "3.0")
Expand Down Expand Up @@ -635,12 +635,7 @@ def on_cleanup_cache(widget, update_label, clear_all=False):
dialog.run()
dialog.destroy()

pic_cache = "{}/pictures".format(BASE_CACHE_PATH)
broken_files = 0
if os.path.exists(pic_cache):
for pic in os.scandir(pic_cache):
if pic.stat().st_size == 0:
broken_files += 1
broken_files = count_broken_pictures_in_cache()

label = gettext.ngettext(
"{number} broken picture currently in cache",
Expand All @@ -667,26 +662,14 @@ def _update_broken_label(sibling):
)
genbox.pack_start(prefbox, False, False, 0)

cache_total = 0
if os.path.exists(pic_cache):
for pic in os.scandir(pic_cache):
cache_total += pic.stat().st_size
cache_total = cache_total / 1000
if cache_total > 1000000:
cache_size = "{} Go".format(str(round(cache_total/1000000, 2)))
elif cache_total > 1000:
cache_size = "{} Mo".format(str(round(cache_total/1000, 2)))
else:
cache_size = "{} ko".format(str(round(cache_total, 2)))

def _update_empty_label(sibling):
if isinstance(sibling, Gtk.Label):
sibling.set_label(
_("Picture cache use {size}").format(size="0.0 ko")
)

prefbox = self.make_button_row(
_("Picture cache use {size}").format(size=cache_size),
_("Picture cache use {size}").format(size=compute_cache_size()),
_("Clear picture cache"),
on_cleanup_cache,
"destructive-action",
Expand Down Expand Up @@ -793,3 +776,9 @@ def _show_systemd_install(widget):
framebox.pack_start(frame, False, False, 0)

return framebox


if __name__ == "__main__":
prefwin = PrefDialog(None, 0)
prefwin.run()
prefwin.destroy()
2 changes: 1 addition & 1 deletion chwall/gui/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def is_current_wall_favorite(self, wallinfo):
def show_preferences_dialog(self, widget):
if self.app is None:
flags = 0
# flags 3 = MODAL | DESTROY_WITH_PARENT
else:
# flags 3 = MODAL | DESTROY_WITH_PARENT
flags = 3
prefwin = PrefDialog(self.app, flags)
prefwin.run()
Expand Down
40 changes: 39 additions & 1 deletion chwall/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
import yaml
import hashlib
import logging
import subprocess
from xdg.BaseDirectory import xdg_cache_home, xdg_config_home
Expand Down Expand Up @@ -97,13 +98,50 @@ def reset_pending_list(*opts):
os.unlink(road_map)


def compute_cache_size():
pic_cache = "{}/pictures".format(BASE_CACHE_PATH)
if not os.path.exists(pic_cache):
return 0
cache_total = 0
for pic in os.scandir(pic_cache):
cache_total += pic.stat().st_size
cache_total = cache_total / 1000
if cache_total > 1000000:
return "{} Go".format(str(round(cache_total/1000000, 2)))
elif cache_total > 1000:
return "{} Mo".format(str(round(cache_total/1000, 2)))
return "{} ko".format(str(round(cache_total, 2)))


def is_broken_picture(picture):
common_sums = [
# reddit broken picture
"35a0932c61e09a8c1cad9eec75b67a03602056463ed210310d2a09cf0b002ed5"
]
check = hashlib.sha256()
with open(picture, "rb") as f:
check.update(f.read())
return check.hexdigest() in common_sums


def count_broken_pictures_in_cache():
pic_cache = "{}/pictures".format(BASE_CACHE_PATH)
if not os.path.exists(pic_cache):
return 0
broken_files = 0
for pic in os.scandir(pic_cache):
if pic.stat().st_size == 0 or is_broken_picture(pic):
broken_files += 1
return broken_files


def cleanup_cache(clear_all=False):
pic_cache = "{}/pictures".format(BASE_CACHE_PATH)
if not os.path.exists(pic_cache):
return 0
deleted = 0
for pic in os.scandir(pic_cache):
if clear_all or pic.stat().st_size == 0:
if clear_all or pic.stat().st_size == 0 or is_broken_picture(pic):
os.unlink(pic.path)
deleted += 1
return deleted
Expand Down
16 changes: 15 additions & 1 deletion chwall/wallpaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# chwall imports
from chwall.utils import BASE_CACHE_PATH, get_screen_config, get_wall_config, \
get_logger
get_logger, is_broken_picture

import gettext
# Uncomment the following line during development.
Expand Down Expand Up @@ -299,6 +299,16 @@ def _write_current_wallpaper_info(current_wall):
os.unlink(pic_file)
return None, None

# Now check file with common placeholder, like broken image on reddit
if is_broken_picture(pic_file):
# Remove useless picture
os.unlink(pic_file)
# Blacklist it
_write_current_wallpaper_info(current_wall)
blacklist_wallpaper()
# Pick next
return "next", None

_write_current_wallpaper_info(current_wall)
return pic_file, current_wall[0]

Expand Down Expand Up @@ -345,6 +355,10 @@ def pick_wallpaper(config, backward=False, guard=False):
"later.")
)
return None
if lp == "next":
# fetch_wallpaper already clean up thing, thus only return a new
# pick_wallpaper call.
return pick_wallpaper(config, backward)
data["pictures"].remove(wp)
data["history"].append(wp)
with open(road_map, "w") as f:
Expand Down
Loading

0 comments on commit 9630456

Please sign in to comment.