Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import ctypes
- import random
- import re
- import requests
- import os
- from PIL import Image
- import time
- # CONSTANTS
- user32 = ctypes.windll.user32
- sub = 'photoshopbattles'
- SPI_SETDESKWALLPAPER = 0x14 #which command (20)
- SPIF_UPDATEINIFILE = 0x0
- attempts = 0
- jpgsrc = 'wallpaper.jpg'
- pngsrc = 'wallpaper.png'
- abspath = os.getcwd()
- jpg_fp = os.path.join(abspath , jpgsrc)
- png_fp = os.path.join(abspath, pngsrc)
- reddit = praw.Reddit(user_agent='Desktop Changer (by /u/claythearc)',
- client_id=os.environ['REDDIT_ID'],
- client_secret=os.environ['REDDIT_SECRET'])
- def parse_imgur_url(url):
- return re.search(r'(https?\://(.*)imgur\.com/[^\)]+)', url).group(1)
- post = reddit.subreddit(sub).random()
- if __name__ == "__main__":
- post = reddit.subreddit(sub).random()
- while True:
- if attempts > 5:
- attempts = 0
- post = reddit.subreddit(sub).random()
- rand_comment = random.choice(post.comments.list())
- try:
- if 'imgur' in rand_comment.body:
- attempts += 1
- url = parse_imgur_url(rand_comment.body)
- if url.endswith('jpg'):
- r = requests.get(url)
- else:
- r = requests.get(url + '.jpg')
- with open(jpgsrc, "wb") as f:
- f.write(r.content)
- if os.path.getsize(jpg_fp) > 2000:
- try:
- Image.open(jpg_fp).save(png_fp)
- except Exception as e:
- continue
- ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 1, png_fp, SPIF_UPDATEINIFILE)
- time.sleep(60)
- else:
- attempts += 1
- continue
- break
- if 'that are not a photoshop' in rand_comment.body:
- attempts += 1
- except Exception as e:
- print(url)
- print(e.args)
- attempts += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement