Python 使用下载的图像更改windows背景的Reddit bot
到目前为止,我已经完成了浏览subreddit和在请求时下载顶级图像的大部分代码。我可以使用PRAW和urllib在获得链接后下载图片。最后一个让我难以忘怀的部分是将图像文件放入一个数组中,并实际将它们设置为我的背景。这是我的Python 使用下载的图像更改windows背景的Reddit bot,python,linux,windows,background,Python,Linux,Windows,Background,到目前为止,我已经完成了浏览subreddit和在请求时下载顶级图像的大部分代码。我可以使用PRAW和urllib在获得链接后下载图片。最后一个让我难以忘怀的部分是将图像文件放入一个数组中,并实际将它们设置为我的背景。这是我的 import praw import time import os import urllib as ul import os def backGroundChanger(sub): USER_AGENT='wall paper changer for
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()
这也许行得通,也许不行;这是未经测试的 阅读函数,了解使用系统程序设置后台的方法,如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()