Created
July 1, 2012 09:02
-
-
Save shelbyKiraM/3027613 to your computer and use it in GitHub Desktop.
A python script that pulls images from wallbase and sorts based on a up or down vote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import pygame | |
import shutil | |
import urllib2 | |
import re | |
import threading | |
from urllib import urlretrieve | |
from bs4 import BeautifulSoup | |
from pygame.locals import * | |
from Queue import Queue | |
def getImage(imageQueue, images): | |
if imageQueue.empty(): | |
grabMainPage(imageQueue) | |
subPageOpen = urllib2.urlopen(imageQueue.get()) | |
subPage = subPageOpen.read() | |
subSoup = BeautifulSoup(subPage) | |
wallpaperURL = str(subSoup.find(id='bigwall').find_all('img')[0].get('src')) | |
filename = "C:\\walls\\" + re.search(r"/(wallpaper-[0-9]+\.[a-zA-Z]{3})", wallpaperURL).group(1) | |
wallpaper = urlretrieve(wallpaperURL, filename) | |
images.put(filename) | |
imageQueue.task_done() | |
#print re.search(r"/(wallpaper-[0-9]+\.[a-zA-Z]{3})", wallpaperURL).group(1) | |
def grabMainPage(imageQueue): | |
mainPageOpen = urllib2.urlopen("http://wallbase.cc/random/12/eqeq/1920x1080/0/100/60") | |
mainPage = mainPageOpen.read() | |
soup = BeautifulSoup(mainPage) | |
for link in soup.find_all('a'): | |
if re.match("http://wallbase.cc/wallpaper/[0-9]+", str(link.get('href'))) != None: | |
imageQueue.put(link.get('href')) | |
# begin main application # | |
prefix = "C:\\walls\\" | |
images = Queue() | |
imageQueue = Queue(maxsize=60) | |
preload = threading.Thread(target=grabMainPage, args=(imageQueue)) | |
preload.start() | |
for filename in os.listdir(prefix): | |
if (filename[0:9] == "wallpaper"): | |
images.put(filename) | |
pygame.init() | |
size = width, height = 1920, 1080 | |
screen = pygame.display.set_mode(size, pygame.FULLSCREEN) | |
pygame.mouse.set_visible(0) | |
changed = True | |
os.chdir(prefix) | |
while 1: | |
if images.empty(): | |
t = threading.Thread(target=getImage, args=(imageQueue, images,)) | |
t.start() | |
if pygame.font: | |
font = pygame.font.Font(None, 36) | |
text = font.render("Loading More Images", 1, (10, 10, 10)) | |
textpos = text.get_rect(centerx=screen.get_width()-80, centery=screen.get_height()-30) | |
screen.blit(text, textpos) | |
pygame.display.flip() | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
if event.type == KEYDOWN: | |
if event.key == K_ESCAPE: | |
sys.exit() | |
elif event.key == K_UP: | |
try: | |
shutil.move(imagePath, "great") | |
except: | |
print "Unexpected error:", sys.exc_info()[1] | |
changed = True | |
elif event.key == K_DOWN: | |
try: | |
shutil.move(imagePath, "delete") | |
except: | |
print "Unexpected error:", sys.exc_info()[1] | |
changed = True | |
elif event.key == K_RIGHT: | |
changed = True | |
if changed: | |
if pygame.font: | |
font = pygame.font.Font(None, 36) | |
text = font.render("Loading", 1, (10, 10, 10)) | |
textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2) | |
screen.blit(text, textpos) | |
pygame.display.flip() | |
imagePath = images.get() | |
currentImage = pygame.image.load(imagePath) | |
changed = False | |
screen.blit(currentImage, (0, 0)) | |
pygame.display.flip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment