2013-07-31 15 views
0

截至目前,我已經完成了大部分代碼,用於瀏覽subreddit,並在請求時下載頂部圖像。我能夠使用PRAW和urllib在獲取鏈接後下載圖像。我堅持的最後一部分是將圖像文件放在一個數組中,並將它們設置爲我的背景。這是我有什麼用下載的圖像改變windows背景的Reddit bot

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

回答

2

這可能工作,也許不是;它未經測試。

請閱讀os.system函數,以獲取使用系統程序設置背景的方法,例如linux中的xsetbg。在這裏尋找設置窗口背景(它只涉及黑客註冊表)。

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() 
+0

謝謝,當我可以找到一些空閒時間,我會測試這一點,讓你知道。謝謝您的幫助! – user1943221