user1943221
user1943221

Reputation: 77

Reddit bot that changes windows background with downloaded images

As of right now I have a majority of the code done for browsing a subreddit, and downloading the top images at the time of the request. I was able to do this using PRAW and urllib to download the images once i get their link. The final part that i am stuck on is putting the images files in an array and actually setting them as my background. Here is what i have

import praw
import time
import os
import urllib as ul
import os    

def backGroundChanger(sub):

    USER_AGENT='wall paper changer for linux/windows by /u/**********' #specifies what my bot does and by who

    REDDIT_ID= #reddit id
    REDDIT_PASS= #reddit password

    reddit=praw.Reddit(USER_AGENT) #creates bot
    reddit.login(REDDIT_ID,REDDIT_PASS) #logsin
    print reddit.is_logged_in()
    images=reddit.get_subreddit(sub)


    while True:
        count=0
        for sub in images.get_hot(limit=10):
            imageLink=sub.url
            print imageLink
            n=str(count)
            ul.urlretrieve(imageLink, "i" + n )

            count+=1
        file=[]
        dir=os.getcwd()
        for files in os.listdir("."):
            if(files.endswith(".jpg|| .png"): # not sure if this will work
                file.append(files)

        changeBackGround(file,dir)


def changeBackGround(file, dir):
    #Do back ground changing stuff here


def main():
    subreddit=input("What subreddit would you like me to pull images from? ") 
    print "You chose " + subreddit
    backGroundChanger(subreddit)

main()

Upvotes: 0

Views: 330

Answers (1)

dilbert
dilbert

Reputation: 3098

This might work, maybe not; its untested.

Read up on the os.system function for a means to use system programs to set the background, like xsetbg in linux. Look here for setting the windows background (it only involves hacking the registry).

import os
import glob
import random
import sys
import time
import urllib

import praw

def backGroundChanger(sub):

    USER_AGENT = 'wall paper changer for linux/windows by /u/**********' #specifies what my bot does and by who

    REDDIT_ID = #reddit id
    REDDIT_PASS = #reddit password

    reddit = praw.Reddit(USER_AGENT) #creates bot
    reddit.login(REDDIT_ID, REDDIT_PASS) #logsin
    print reddit.is_logged_in()
    images = reddit.get_subreddit(sub)

    while True:
    count = 0
    for sub in images.get_hot(limit = 10):
        imageLink = sub.url
        print imageLink
        n = str(count)
        urllib.urlretrieve(imageLink, "i" + n )

        count += 1

    files = glob.glob("*.jpg") + glob.glob("*.png")

    changeBackGround(files)


def changeBackGround(ifiles):
    #Do back ground changing stuff here
    the_file = ifiles[random.randint(0, len(ifiles) - 1)]
    if(sys.platform.startswith("win")): # Windows
        # Do this yourself
        pass
    elif(sys.platform.startswith("linux")): # Linux
        os.system("xsetbg -center %s" % the_file)

def main():
    subreddit = input("What subreddit would you like me to pull images from? ") 
    print "You chose " + subreddit
    backGroundChanger(subreddit)

main()

Upvotes: 2

Related Questions