Bits of selenium, time, tkinter, pyvirtualdisplay – Get jokes timely while you work

- - Applications, Python, Technology, Web

Making use of libraries available and building something useful has always been my favorite. However, I haven’t yet been involved in making/contributing to one. I don’t even know the procedure to be involved in building libraries. My latest program also makes use of various libraries such as selenium, time, tkinter. In a nutshell, the program fetches jokes from www.laughfactory.com and displays a message box with a joke in a timely interval. I usually run the program while I am at work(internship). I’ve set 10 minutes interval between the jokes to be shown. By the way, I used time module and it’s time.sleep(time_in_seconds) method for dealing with intervals between each message box to appear. The program isn’t full fledged and you can make additions to it on your own for customization according to your need. The earlier statement, I guess seemed very professional for a small program. Please bear with me 🙂

 

I made use of selenium module along with pyvirtualdisplay to fetch the jokes and mute the display of the browser, since it would be distracting. The program only fetches 20 latest jokes from laughfactory.com and periodically displays one at a time in a messagebox. A little humor is always good at work. Anyway that’s my personal thought you may disagree to. I used tkinter module for displaying the message box. That’s all about the program. Have a look at the codes. Thank god, the import statements < program statements.

Python codes to get timely jokes

import tkinter
import tkinter.messagebox as mbox
from time import sleep
from selenium import webdriver
from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get("http://www.laughfactory.com/jokes/latest-jokes")

sleep(40)
#print("finding jokes")
jokes = [str(joke.text) for joke in browser.find_elements_by_xpath("//div/p[starts-with(@id,'joke_')]")]

#print("found")
browser.quit()
display.stop()

window = tkinter.Tk()
window.wm_withdraw()

for joke in jokes:
    sleep(600)
    mbox.showinfo('Bored? Enjoy the JOk3!', joke)

Thank you for spending your time here. I have always tried to make my articles short and maybe now succeeded. You can utilize the saved time for another read in this blog, may be :p .Tell me how you felt the article was in the comments section below. This keeps me motivated to publish good content.

I am a Computer Science student at Deerwalk Institute of Technology, Nepal. I love automation and prefer python programming language for it. I run www.thetaranights.com and currently doing an internship at Deerwalk Services, Nepal. You can contact me via this forum or directly email me at bbhishan@gmail.com . You may suggest me on article to cover or program to write here Suggest Ideas

There Are 1 Comment On This Article.

Leave a Reply

Your email address will not be published. Required fields are marked *