0

到目前为止,我已经完成了大部分代码,用于浏览 subreddit,并在请求时下载顶部图像。一旦我得到他们的链接,我就可以使用 PRAW 和 urllib 来下载图像。我坚持的最后一部分是将图像文件放在一个数组中,并将它们实际设置为我的背景。这是我所拥有的

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()
4

1 回答 1

2

这可能有效,也可能无效;它未经测试。

阅读os.system函数,了解使用系统程序设置背景的方法,例如 linux 中的 xsetbg。在这里查看设置 Windows 背景(它只涉及破解注册表)。

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()
于 2013-08-01T13:21:09.263 回答