Navigation Menu

Skip to content

Commit

Permalink
Add tags to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Dec 30, 2018
1 parent 16e22c0 commit 6220b0a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion config.json
@@ -1,5 +1,6 @@
{

"allowNsfw" : 0
"allowNsfw" : 0,
"tags" : ""

}
33 changes: 22 additions & 11 deletions main.py
Expand Up @@ -7,11 +7,14 @@
import time
import json

def GetImageUrl(allowNsfw):
maxPage = int(xml.etree.ElementTree.fromstring(urllib.request.urlopen("https://konachan.com/post.xml?limit=1").read()).get("count"))
xmlContent = xml.etree.ElementTree.fromstring(urllib.request.urlopen("https://konachan.com/post.xml?limit=1&page=" + str(random.randint(0, maxPage + 1))).read())[0]
if allowNsfw == False and xmlContent.get("rating") != "s":
return (GetImageUrl(allowNsfw))
def GetImageUrl(maxPage, counter, allowNsfw, tags):
xmlContent = xml.etree.ElementTree.fromstring(urllib.request.urlopen("https://konachan.com/post.xml?limit=1&page=" + str(random.randint(0, maxPage + 1)) + "&tags=" + tags).read())[0]
if not allowNsfw and xmlContent.get("rating") != "s":
if counter == 0:
print("No image found after 10 iterations. That may mean there is no safe image available with the tags you choosed")
return (None)
else:
return (GetImageUrl(maxPage, counter - 1, allowNsfw, tags))
url = xmlContent.get("file_url")
fileName = "wallpaper." + url.split('.')[-1]
urllib.request.urlretrieve(url, fileName)
Expand All @@ -20,18 +23,26 @@ def GetImageUrl(allowNsfw):
def GetImage():
jsonContent = json.loads(open("config.json").read())
allowNsfw = jsonContent["allowNsfw"] == 1
tags = jsonContent["tags"].replace(' ', '+')
print("Current configuration:")
print("Allow NSFW: " + str(allowNsfw))
print("Tags: " + ("None" if (tags == "") else tags))
print("\nDownloading image...")
image = GetImageUrl(allowNsfw);
print("Done, cleaning...")
maxPage = int(xml.etree.ElementTree.fromstring(urllib.request.urlopen("https://konachan.com/post.xml?limit=1&tags=" + tags).read()).get("count"))
if maxPage == 0:
print("There is no image with the tags you specified, please correct them on the configuration file")
return (None)
image = GetImageUrl(maxPage, 10, allowNsfw, tags)
if image is not None:
print("Done, cleaning...")
return (image)

if __name__ == "__main__":
if os.name == 'nt':
fileName = GetImage()
ctypes.windll.user32.SystemParametersInfoW(20, 0, os.path.dirname(os.path.realpath(__file__)) + "\\" + fileName, 0)
time.sleep(2)
os.remove(fileName)
if fileName is not None:
ctypes.windll.user32.SystemParametersInfoW(20, 0, os.path.dirname(os.path.realpath(__file__)) + "\\" + fileName, 0)
time.sleep(2)
os.remove(fileName)
else:
print("Your OS is not supported")
print("Your OS is not supported")

0 comments on commit 6220b0a

Please sign in to comment.