Pages : 1
#1 Le 17/06/2011, à 16:07
- papy88140
Photos Flickr en papier peint
Bonjour,
voila un petit script python pour changer votre papier peint
(plus facile que pour celui de la cuisine ou de la chambre à coucher ...)
C'est du python, pas très propre mais ça fonctionne
Il suffit d'enregistrer le code dans un fichier, le rendre exécutable, ...
La routine quoi.
Attention, le script a besoin de curl pour fonctionner :
sudo apt-get install curl
J'attends quelques retour pour améliorer ou ... on verra
#!/usr/bin/python
# depend de Curl
import urllib2,random,time,datetime
import string,commands,re,os
# --- code issu du site : http://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates
def strTimeProp(start, end, format, prop):
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def randomDate(start, end, prop):
return strTimeProp(start, end, '%Y/%m/%d', prop)
# --- fin du code
reLien = re.compile("<a href=[^>]*>")
rePhoto = re.compile("""\"/photos/[^\"^/]*/[^\"^/]*/\"""")
command="curl http://www.flickr.com/explore/interesting/%s/page%02i/" % (randomDate("2004/07/01", "2011/06/16", random.random()),int(random.random()*50+1))
html = commands.getoutput(command)
photos=rePhoto.findall(html)
index=random.randint(1, len(photos))
cmd=("""curl -s www.flickr.com%s | grep url: | grep _z.jpg | cut -d "'" -f 2 """) % (photos[index][1:len(photos[index])-1])
adrPhoto = commands.getoutput(cmd)
opener1 = urllib2.build_opener()
page1 = opener1.open(adrPhoto)
my_picture = page1.read()
filename = "%s/.tmpPhoto.jpg" % (os.path.expanduser("~") )
fout = open(filename, "wb")
fout.write(my_picture)
fout.close()
commands.getoutput("gconftool-2 --type string --set /desktop/gnome/background/picture_filename $HOME/.tmpPhoto.jpg")
Linux à Contrexéville ?
http://leclug.free.fr/forum/
Hors ligne
#2 Le 17/06/2011, à 16:18
- AnsuzPeorth
Re : Photos Flickr en papier peint
Bjr,
J'ai juste survolé, mais pourquoi utiliser curl alors que tu as urllib ou urllib2, je comprends pas !
Il existe aussi une commande urllib pour télécharger un fichier ...
Sinon, ce genre de script, si tu dois installer curl, autant le faire en bash (parser ligne à ligne un petit fichier, bash s'en sort pas trop mal, un peu plus lent que python, mais bon, ici ca n'as pas grande importance)
Interface graphique pour bash, python ou autre: glade2script
Support Tchat: http://chat.jabberfr.org/muckl_int/inde … ade2script (Hors ligne)
Hors ligne
#3 Le 17/06/2011, à 19:04
- papy88140
Re : Photos Flickr en papier peint
Merci pour le conseil
Je vais y penser pour la 2ème version
A bientôt
Linux à Contrexéville ?
http://leclug.free.fr/forum/
Hors ligne
#4 Le 21/06/2011, à 12:58
- papy88140
Re : Photos Flickr en papier peint
Version 0.2 : suppression de la dépendance à Curl
#!/usr/bin/python
import urllib2,random,time,datetime
import string,commands,re,os
opener = urllib2.build_opener()
# --- code issu du site : http://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates
def strTimeProp(start, end, format, prop):
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def randomDate(start, end, prop):
return strTimeProp(start, end, '%Y/%m/%d', prop)
# --- fin du code
reLien = re.compile("<a href=[^>]*>")
rePhoto = re.compile("""\"/photos/[^\"^/]*/[^\"^/]*/\"""")
reUrl = re.compile("""url: \'.*_z.jpg'""")
page = opener.open("http://www.flickr.com/explore/interesting/%s/page%02i/" % (randomDate("2004/07/01", "2011/06/16", random.random()),int(random.random()*50+1)))
html = page.read()
page.close()
photos=rePhoto.findall(html)
index=random.randint(1, len(photos))
page=opener.open("http://www.flickr.com%s" % (photos[index][1:len(photos[index])-1]))
html=page.read()
adrPhoto=reUrl.findall(html)[0][6:-1]
page = opener.open(adrPhoto)
my_picture = page.read()
page.close()
filename = "%s/.tmpPhoto.jpg" % (os.path.expanduser("~") )
fout = open(filename, "wb")
fout.write(my_picture)
fout.close()
commands.getoutput("gconftool-2 --type string --set /desktop/gnome/background/picture_filename $HOME/.tmpPhoto.jpg")
Par contre, j'envisage une dépendance à imagemagick pour modifier la photo avant la mise en place comme papier peint.
Une bonne idée ?
Si vous avez d'autres suggestions, n'hésitez pas.
Pour le moment, je me suis mis ce petit script dans ma crontab et ça me va bien
Dernière modification par papy88140 (Le 21/06/2011, à 13:00)
Linux à Contrexéville ?
http://leclug.free.fr/forum/
Hors ligne
#5 Le 21/06/2011, à 14:15
- AnsuzPeorth
Re : Photos Flickr en papier peint
Bjr,
Attention, commands est déprécié, il faut utiliser subprocess.
Pour imagemagick, si tu n'as pas de grosse transformation, peut que PIL serait mieux, ca fait partie de python.
Sinon, flirck a un api !
http://www.flickr.com/services/api/
Même des versions python.
Sinon, pas mal, je vois que tu t'es bien amusé avec les dates (je galère tjrs avec ces modules !).
PS: Je vois que tu n'utilises pas urllib.urlretrieve pour dl la photo, c'est un choix ?
Dernière modification par AnsuzPeorth (Le 21/06/2011, à 14:16)
Interface graphique pour bash, python ou autre: glade2script
Support Tchat: http://chat.jabberfr.org/muckl_int/inde … ade2script (Hors ligne)
Hors ligne
#6 Le 21/06/2011, à 15:17
- papy88140
Re : Photos Flickr en papier peint
Bjr,
Attention, commands est déprécié, il faut utiliser subprocess.
Bon, correction planifiée.
Pour imagemagick, si tu n'as pas de grosse transformation, peut que PIL serait mieux, ca fait partie de python.
J'aurais dans l'idée d'adapter la taille de l'image
à la taille du bureau, et éventuellement d'ajouter en incrustation des infos
sur l'auteur de la photo. (nom ou pseudo)
Sinon, flirck a un api !
http://www.flickr.com/services/api/
Même des versions python.
Dingue ce flickr !
Sinon, pas mal, je vois que tu t'es bien amusé avec les dates (je galère tjrs avec ces modules !).
Heu, en fait, je m'amuse pas mal avec google et le copier/coller.
Ça doit bien faire 15ans que je développe plus sérieusement et je scripte un
peu ici et là pour ne pas "perdre la main".
PS: Je vois que tu n'utilises pas urllib.urlretrieve pour dl la photo, c'est un choix ?
Non, plutôt une méconnaissance.
Je sens que je vais en apprendre pas mal en bidouillant mon petit script
Linux à Contrexéville ?
http://leclug.free.fr/forum/
Hors ligne
#7 Le 21/06/2011, à 15:58
- AnsuzPeorth
Re : Photos Flickr en papier peint
J'aurais dans l'idée d'adapter la taille de l'image
à la taille du bureau, et éventuellement d'ajouter en incrustation des infos
sur l'auteur de la photo. (nom ou pseudo)
C'est possible avec PIL, je dois avoir un exemple de ce genre; en cherchant bien
Dingue ce flickr !
Je connais pas cette API, mais il doit être très configurable, donc surement trier par taille voulu. Et puis ça évitera de parser du html ! Le retour en json est un plaisir à travailler.
Je sens que je vais en apprendre pas mal en bidouillant mon petit script
C'était pas le but ?
Interface graphique pour bash, python ou autre: glade2script
Support Tchat: http://chat.jabberfr.org/muckl_int/inde … ade2script (Hors ligne)
Hors ligne
#8 Le 22/06/2011, à 09:57
- papy88140
Re : Photos Flickr en papier peint
J'aurais dans l'idée d'adapter la taille de l'image
à la taille du bureau, et éventuellement d'ajouter en incrustation des infos
sur l'auteur de la photo. (nom ou pseudo)C'est possible avec PIL, je dois avoir un exemple de ce genre; en cherchant bien
Dingue ce flickr !
Je connais pas cette API, mais il doit être très configurable, donc surement trier par taille voulu. Et puis ça évitera de parser du html ! Le retour en json est un plaisir à travailler.
Je sens que je vais en apprendre pas mal en bidouillant mon petit script
C'était pas le but
?
Y a toujours un peu de ça, oui, quand même.
Franchement, on aurait eu ce langage à la fac au lieu de ce @!"#??#@ de Eiffel ...
Allez, voici la nouvelle version :
#!/usr/bin/python
import urllib2,random,time,datetime
import string,subprocess,re,os
opener = urllib2.build_opener()
# --- code issu du site : http://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates
def strTimeProp(start, end, format, prop):
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def randomDate(start, end, prop):
return strTimeProp(start, end, '%Y/%m/%d', prop)
# --- fin du code
reLien = re.compile("<a href=[^>]*>")
rePhoto = re.compile("""\"/photos/[^\"^/]*/[^\"^/]*/\"""")
reUrl1 = re.compile("""url: \'.*_b.jpg'""")
reUrl2 = re.compile("""url: \'.*_z.jpg'""")
reUrl3 = re.compile("""url: \'[^_]*.jpg'""")
page = opener.open("http://www.flickr.com/explore/interesting/%s/page%02i/" % (randomDate("2004/07/01", "2011/06/16", random.random()),int(random.random()*50+1)))
html = page.read()
page.close()
photos=rePhoto.findall(html)
index=random.randint(1, len(photos))
page=opener.open("http://www.flickr.com%s" % (photos[index][1:len(photos[index])-1]))
html=page.read()
adrPhoto=reUrl1.findall(html)
print adrPhoto
if len(adrPhoto) == 0 :
adrPhoto=reUrl2.findall(html)
print adrPhoto
if len(adrPhoto) == 0 :
adrPhoto=reUrl3.findall(html)
print adrPhoto
adrPhoto=adrPhoto[0][6:-1]
page = opener.open(adrPhoto)
my_picture = page.read()
page.close()
filename = "%s/.tmpPhoto.jpg" % (os.path.expanduser("~") )
fout = open(filename, "wb")
fout.write(my_picture)
fout.close()
subprocess.Popen(args=["gconftool-2","--type","string","--set","/desktop/gnome/background/picture_filename",filename])
Alors , au programme :
Suppression de la dépendance curl
Suppression de la commande dépréciée commands et remplacement par subprocess
Correction d'un problème lié à la présence ou nom de formats d'image "_z" ou "_b"
Maintenant, reste plus qu'à affiner la présentation de la photo sur le bureau et à placer une icone dans la zone de notification
pour éventuellement mettre la photo de côté puisqu'elle est écrasée à chaque fois.
Linux à Contrexéville ?
http://leclug.free.fr/forum/
Hors ligne