#1 Le 23/08/2013, à 22:48
- zerp
[script/bash] Fond d'écran HD aléatoire de wallpaperfx.com
Hey
Un petit script permettant de modifier aléatoirement son fond d'écran (testé sous Unity, mais je pense que ça marche pour gnome).
L'image est gracieusement fournie gracieusement par wallpaperfx.com ^^
C'est un petit script basique, que j'ai commenté pour que l'on s'y retrouve (un peu quand même.................)
Le wallpaper téléchargé est automatiquement adapté à votre résolution, enfin si elle est supportées par le site. Si votre écran à une résolution inférieure à la HD, il faudra modifier la variable HD à false, sinon ça ne marche pas
Le script prend quelques secondes: plusieurs pages doivent être chargées pour accéder à l'image et la charger.
L'image est choisie aléatoirement parmi les quelques milliers du site, mais il est possible avec quelques modifications de ne choisir que parmi des mots clés (voitures, hello kitty, carwash, etc..). Si j'ai le temps j'essayerais peut-être d'ajouter l'option. si qqn trouve le script tout pourri et a des améliorations je prends
Bwef
#!/bin/bash
#
# Usage: randwall [-v]
# -v: verbose
#
#
# Répertoire des fichiers temporaires
#
DIR="$HOME/.randwall"
mkdir -p "$DIR"
#################################
#################################
#################################
blabla()
{
if $VERBOSE ; then
echo -e "$*"
fi
}
rand32()
{
if [ -z "$2" ]; then
min=0
max=$1
else
min=$1
max=$2
fi
rand=$(((($RANDOM << 16) + $RANDOM) % ($max - $min + 1) + $min))
echo $rand
}
setWallpaper()
{
gsettings set org.gnome.desktop.background picture-uri "file://$*"
}
getfrom_WallpaperFX()
{
local MAX_COUNT=10 # Si le wallpaper ne veut toujours pas se charger au bout de 380938098309 fois, pas la peine de continuer
local MAX_NUM=24 # 24 wallpapers s'affichent par page, sur leur site
local MAX_PAGE=300 # Il y a ~300 pages
local WFX_BASE_URL="http://www.wallpaperfx.com/resolutions/${WIDTH}x${HEIGHT}-hdtv-wallpapers"
local WFX_AJAX_URL="www.wallpaperfx.com/ajax/get_wallpaper_resolutions"
local WFX_PAGE_F="$DIR/wfx-page.html" # fichier ou sera enregistré la page HTML d'acceuil de WallpaperFX
local WFX_HTML_F="$DIR/wfx-wallpaper.html" # page HTML de présentation du wallpaper (l'image en elle-même n'est pas téléchargeable directement)
local WFX_DATA_F="$DIR/wfx-data.html" # données qui permettront d'accéder au wallpaper en lui-même
local WFX_WALLPAPER_F="$DIR/wfx-wallpaper.jpg" # fichier ou sera stocké le wallpaper
local USER_AGENT="User-Agent: Mozilla/5.0 (Linux; U; Android 4.0.4; fr-fr; MIDC409 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30"
local count=0
if ! $HD; then
WFX_BASE_URL="http://www.wallpaperfx.com/resolutions/${WIDTH}x${HEIGHT}-wallpapers" # Mais POURQUOI l'url HD est différente ??
fi
# Ce script ne semble pas marcher pour tous les wallpapers, il faut boucler au cas ou on tomberait sur un qui ne marche pas :'(
while [ true ]; do
((++count))
if [ $count -gt $MAX_COUNT ]; then
blabla "[WallpaperFX] I'm tired."
blabla "abort"
exit 1
fi
#
# Récupération de la page ou se trouve le wallpaper choisi au hasard
#
local num=$(rand32 $MAX_NUM)
local page=$(rand32 $MAX_PAGE)
local page_url="$WFX_BASE_URL/page-$page"
blabla "[WallpaperFX] Wallpaper page URL: $page_url (wallpaper n°$num, page $page)"
wget -qO "$WFX_PAGE_F" "$page_url" --header="$USER_AGENT"
#
# Récupération de la page html présentant le wallpaper
#
local html_url=$(cat "$WFX_PAGE_F" \
| egrep -m $num "wallpaper_title" \
| tail -n 1 | egrep -o "http://.+.htm")
if [ -z "$html_url" ]; then
blabla "[WallpaperFX] Wallpaper n°$id not found !!!!!!"
continue;
fi
blabla "[WallpaperFX] Wallpaper (HTML) URL: $html_url"
wget -qO "$WFX_HTML_F" "$html_url" --header="$USER_AGENT"
#
# Récupération du titre et de l'ID du wallpaper
#
local infos=$(cat "$WFX_HTML_F" \
| egrep -m 1 "getResolutions\('" \
| egrep -o "'[^']+'")
# Supression des apostrophes
local wallpaper_id=$(echo "$infos" | head -n 1 | sed "s/^.//" | sed "s/.$//")
local wallpaper_title=$(echo "$infos" | tail -n 1 | sed "s/^.//" | sed "s/.$//")
blabla "[WallpaperFX] Wallpaper ID: $wallpaper_id"
blabla "[WallpaperFX] Wallpaper title: $wallpaper_title"
if ! $VERBOSE; then
echo "$wallpaper_title"
echo "downloading..."
fi
#
# Récupération des données contenant ENFIN le lien vers l'image (si si)
#
blabla "[WallpaperFX] Wallpaper post-data URL: $WFX_AJAX_URL"
# header "X-Request-Width" hyper important, sinon le site renvoit une erreur 404
wget -qO "$WFX_DATA_F" \
--header="Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
--header="$USER_AGENT" \
--header="X-Requested-With: XMLHttpRequest" \
--post-data="width=$WIDTH&height=$HEIGHT&wallpaperid=$wallpaper_id&title=$wallpaper_title" \
"$WFX_AJAX_URL"
local wallpaper=$(cat "$WFX_DATA_F" \
| grep -m 1 "javascript:downloadWallpaper" \
| egrep -o "http://www.wallpaperfx.com/download/.+.jpe?g")
blabla "[WallpaperFX] YEEAAAAAAAAAAAAH. Wallpaper URL: $wallpaper"
blabla "[WallpaperFX] Setting wallpaper..."
wget "$wallpaper" -qO "$WFX_WALLPAPER_F"
setWallpaper "$WFX_WALLPAPER_F"
return;
done
}
#################################
#################################
#################################
dimensions=$(xdpyinfo | grep 'dimensions:' | egrep -om 1 "[0-9]+x[0-9]+" | head -n 1)
HD=true
WIDTH=$(echo "$dimensions" | cut -dx -f1)
HEIGHT=$(echo "$dimensions" | cut -dx -f2)
echo "Dimensions: ${WIDTH}x${HEIGHT}"
if [ "$*" == "-v" ]; then
VERBOSE=true
else
VERBOSE=false
fi
getfrom_WallpaperFX
Je conseille combiné à une tâche cron qui modifie le fond d'écran toutes es 5 minutes
Dernière modification par zerp (Le 24/08/2013, à 00:10)
Hors ligne