Skip to content

Instantly share code, notes, and snippets.

@Ryanb58
Created November 21, 2014 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryanb58/732165566dd6ece3d4b0 to your computer and use it in GitHub Desktop.
Save Ryanb58/732165566dd6ece3d4b0 to your computer and use it in GitHub Desktop.
Freshman Year 9Gag Viewer in Python

Found some of my first code that produced a simple 9GAG Viewer app from my freshman CSCI 230 class.

Requirements: Python 2.7 Tkinter ImageTk BeautifulSoup

Installation: sudo apt-get install python-tkinter sudo apt-get install python-imaging-tk sudo apt-get install python-beautifulsoup

Run the App: python FinalProject.py

'''
9GAG Viewer
Created by Taylor Brazelton
All Rights are Reserved to their respected owners.
I do not own any of the images viewed by this program and therefore take no responsibility.
'''
'''
Funny Picture Viewer / 9GAG Viewer
By: Taylor Brazelton
'''
from Tkinter import *
import tkFileDialog
import Image, ImageTk
from urllib import *
import os
from BeautifulSoup import BeautifulSoup
import re
import thread
from tkMessageBox import *
import shutil
picNum = 0
pageNum = 1
savedPicName = 0
dirName = ""
#futurePageNum = 0
#previousPageNum = 2
class App(Tk):
''' Create Gui'''
def __init__(self):
Tk.__init__(self)
self.title("9GAG Viewer")
self.CanImgViewer = Canvas(self, width = 700, height = 700)
showinfo("CAUTION!","WHAT YOU ARE ABOUT TO SEE WILL MAKE YOU LAUGH. IF YOU ARE UNDER 18 PLEASE LEAVE THE ROOM. I do not control the content about to be displayed. All content is user created and rated. \n \n \n Under 17 requires an accompanying parent or adult guardian. This application contains some adult material and parents are urged to learn more about this app before allowing their young children to use it. This app could contain rough and/or persistent violence and suggestive material, hard language and horror, crude sexual content, sexually-oriented nudity, and/or hard drug use. Admittance to these pictures is prohibited for anyone under the age of seventeen unless accompanied by a parent or guardian.")
'''
folderName = "temp/"
if not os.path.exists(folderName):
os.makedirs(folderName)
filename, header = urlretrieve("http://d24w6bsrhbeh9d.cloudfront.net/photo/5885696_460s.jpg", os.path.join(folderName, "filename.jpg"))
self.lblOutput["text"] = filename
'''
# Download the Images...
#self.DownloadImages()
folderName = "temp/0.jpg"
#Display the first image in the canvas
if os.path.exists(folderName):
self.image = Image.open("temp/0.jpg")
else:
self.image = Image.open("Images/Default.jpg")
#self.image = Image.open(filename)
w = self.CanImgViewer["width"]
h = self.CanImgViewer["height"]
self.image = self.image.resize((int(h),int(w)), Image.ANTIALIAS)
self.photo = ImageTk.PhotoImage(self.image)
self.CanImgViewer.create_image(int(w)/2,int(h)/2,anchor = CENTER, image=self.photo)
self.CanImgViewer.pack()
self.CanImgViewer.grid()
'''
#Code for back button
self.btnClickMe = Button(self, text = "Previous", command = self.PreviousPic)
self.btnClickMe.grid()
'''
self.btnNext = Button(self, text = "Next")
self.btnNext["command"] = self.NextImage
self.btnNext.grid()
self.btnNext = Button(self, text = "Save Image")
self.btnNext["command"] = self.SaveImage
self.btnNext.grid()
self.btnDownloadImages = Button(self, text = "Refresh Files")
self.btnDownloadImages["command"] = self.StartDownloadingImages
self.btnDownloadImages.grid()
''' Download Images into /temp/ folder'''
def StartDownloadingImages(self):
thread.start_new_thread(self.DownloadImages, ("1",))
''' Saved Picture to My Pictures Folder'''
def SaveImage(self):
folderName = "temp/"
global dirName
if dirName == "":
dirName = tkFileDialog.askdirectory(parent=self,initialdir="/",title='Please select a directory')
dirName = dirName + "/SavedFunnyPics/"
saveFolderName = dirName
else:
saveFolderName = dirName
global savedPicName
if not os.path.exists(folderName):
os.makedirs(folderName)
if not os.path.exists(saveFolderName):
os.makedirs(saveFolderName)
while os.path.exists(saveFolderName + str(savedPicName) + ".jpg"):
savedPicName = savedPicName + 1
try:
src = os.path.join(folderName + str(picNum) + ".jpg")
dst = os.path.join(saveFolderName + str(savedPicName) + ".jpg")
shutil.copyfile(src, dst)
except:
print "Error Saving Image"
def DownloadImages(self, PageNum):
if PageNum != False:
source = urlopen("http://tumblr.9gag.com/page/" + str(PageNum)).read()
else:
source = urlopen("http://tumblr.9gag.com/page/1").read()
soup = BeautifulSoup(''.join(source))
soup = soup.findAll('img')
print "----------------------------"
i=0
#for img in soup:
while i<9:
#print soup[1]
m = re.search('<img.*?src="(.*?)".*?alt=', str(soup[i]))
try:
quote = m.group(1)
print quote
print "%i.jpg" % i
folderName = "temp/"
if not os.path.exists(folderName):
os.makedirs(folderName)
filename, header = urlretrieve(quote, os.path.join(folderName, "%i.jpg" % i))
print "Downloaded %s", filename
i = i + 1
except:
print "Error"
def NextImage(self):
global picNum
if picNum == 0:
picNum = picNum + 1
self.image = Image.open("temp/" + str(picNum) + ".jpg")
elif picNum == 1:
picNum = picNum + 1
self.image = Image.open("temp/" + str(picNum) + ".jpg")
elif picNum == 2:
picNum = picNum + 1
self.image = Image.open("temp/" + str(picNum) + ".jpg")
elif picNum == 3:
picNum = picNum + 1
self.image = Image.open("temp/" + str(picNum) + ".jpg")
elif picNum == 4:
picNum = picNum + 1
self.image = Image.open("temp/" + str(picNum) + ".jpg")
elif picNum == 5:
picNum = picNum + 1
self.image = Image.open("temp/" + str(picNum) + ".jpg")
elif picNum == 6:
picNum = picNum + 1
self.image = Image.open("temp/" + str(picNum) + ".jpg")
elif picNum == 7:
picNum = picNum + 1
self.image = Image.open("temp/" + str(picNum) + ".jpg")
elif picNum == 8:
'''
Currently loops back around to Zero...
Future objective is to switch to next page...
'''
picNum = 0
#Tells downloader to download next page of pictures.
global pageNum
pageNum = pageNum + 1
#thread.start_new_thread(self.DownloadImages, (pageNum,))
self.DownloadImages(pageNum)
self.image = Image.open("temp/" + str(picNum) + ".jpg")
w = self.CanImgViewer["width"]
h = self.CanImgViewer["height"]
self.image = self.image.resize((int(h),int(w)), Image.ANTIALIAS)
self.photo = ImageTk.PhotoImage(self.image)
self.CanImgViewer.create_image(int(w)/2,int(h)/2,anchor = CENTER, image=self.photo)
'''
def getPicNum(self):
return self.__PicNun
def setPicNum(self, x):
self.__PicNum = x
picNum = property(getPicNum, setPicNum)
'''
def main():
a = App()
a.mainloop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment