Skip to content

Instantly share code, notes, and snippets.

@riccardobl
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardobl/ca9f122b3d79cbea257f to your computer and use it in GitHub Desktop.
Save riccardobl/ca9f122b3d79cbea257f to your computer and use it in GitHub Desktop.
A script that can be used with ScriptedWallpaper to pick a random image from Wallhaven and set it as background for kde.
rm -Rf tmp
sudo apt-get install qt5-default qttools5-dev-tools kde-workspace-dev
mkdir tmp
wget http://kde-apps.org/CONTENT/content-files/156317-Scripted_Wallpaper.tar.gz -O tmp/scw.tar.gz
cd tmp
tar -xzf scw.tar.gz
cd Scripted_Wallpaper
mkdir -p build
cd build
cmake -DQT_QMAKE_EXECUTABLE=/usr/bin/qmake-qt5 ../src -DCMAKE_INSTALL_PREFIX=`kde4-config --prefix`
make
sudo make install
A script that can be used with ScriptedWallpaper to pick a random image from Wallhaven and set it as background for kde.
All the code is released under public domain.
#!/usr/bin/python
import os,re,pycurl,StringIO
from optparse import OptionParser
from random import randint
_WALLPAPER_PAGES_REGEX = re.compile("https?:\\/\\/alpha\\.wallhaven\\.cc\\/wallpaper\\/([0-9]+)",re.MULTILINE|re.IGNORECASE)
SEARCH_URL = "http://alpha.wallhaven.cc/search"
CATEGORY = {
"general":4,
"anime":2,
"people":1
}
PURITY = {
"sfw":4,
"sketchy":2
}
def download(url,out):
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.WRITEDATA, out)
curl.setopt(pycurl.USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201")
curl.perform()
curl.close()
def get(url):
buffer = StringIO.StringIO()
download(url,buffer);
return buffer.getvalue()
def getRandomWallpaperID(query,categories,purity,resolutions,ratios):
c=0
p=0
for ct in categories:
try:
c|=CATEGORY[ct]
except: pass
for pt in purity:
try:
p|=PURITY[pt]
except: pass
url = SEARCH_URL+"?q="+query+"&categories="+"{0:03b}".format(c,3)+"&purity="+"{0:03b}".format(p,3)
if resolutions != None: url+="&resolutions="+",".join(resolutions)
if ratios != None: url+="&ratios="+"".join(ratios)
url+="&sorting=random&order=desc"
page = get(url)
wallpapers = _WALLPAPER_PAGES_REGEX.findall(page)
if wallpapers==None: return None
return wallpapers[randint(0,len(wallpapers)-1)]
def getWallpaperURLById(wid):
return "http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-"+wid+".jpg"
parser = OptionParser( usage = "usage: %prog <options>\n" )
parser.add_option( "-i", "--instance-id", action="store", dest="ID", default="monitor1", help="" )
parser.add_option( "-c", "--categories", action="store", dest="CATEGORIES", default="general,anime,people", help="" )
parser.add_option( "-p", "--purity", action="store", dest="PURITY", default="sfw", help="" )
parser.add_option( "-r", "--resolutions", action="store", dest="RESOLUTIONS", default=None, help="" )
parser.add_option( "-x", "--ratios", action="store", dest="RATIOS", default=None, help="" )
parser.add_option( "-q", "--query", action="store", dest="QUERY", default="", help="" )
(o,args) = parser.parse_args()
instance_id=o.ID
working_dir = os.path.expanduser("~")+"/.kwallh/"
for f in os.listdir(working_dir) :
fl=os.path.join(working_dir,f)
if os.path.isfile(fl):
if f.startswith(instance_id+"-"):
os.remove(fl)
if not os.path.exists(working_dir):
os.makedirs(working_dir)
wallpaper_id=getRandomWallpaperID(
o.QUERY,
o.CATEGORIES.split(","),
o.PURITY.split(","),
o.RESOLUTIONS.split(",") if o.RESOLUTIONS!=None else None,
o.RATIOS.split(",") if o.RATIOS!=None else None
)
wallpaper_url=getWallpaperURLById(wallpaper_id)
output_path=working_dir+instance_id+"-"+wallpaper_id+".jpg"
of=open(output_path,"wb")
download(wallpaper_url, of)
of.close();
print output_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment