Skip to content

Instantly share code, notes, and snippets.

@artizirk
Created August 31, 2013 22:35
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 artizirk/6401085 to your computer and use it in GitHub Desktop.
Save artizirk/6401085 to your computer and use it in GitHub Desktop.
Automaticly upload screenshots to the internet
#!/usr/bin/python2
################################################################################
## Automatic screenshot uploader ##
## ##
## WARNING, CONTAINS BAD CODE ##
## AND SECURITY HOLES! ##
## ##
## AUTHOR: Arti Zirk <arti.zirk@gmail.com> ##
## LICENSE: WTFPL ##
################################################################################
import gtk
import pynotify
from subprocess import Popen, PIPE
import sys
import requests
from HTMLParser import HTMLParser
class asktoupload():
def __init__(self, scfile):
#Initing the pynotif
self.scfile = scfile
pynotify.init("ask_upload")
n = pynotify.Notification("New screenshot added", "Do you want to upload it?")
n.set_urgency(pynotify.URGENCY_LOW)
n.add_action("action_upload", "Upload", self.upload)
n.add_action("no_upload", "No upload", self.doNothing)
n.show()
gtk.main()
def upload(self, notifyObj, action):
print "uploading"
file_addr = upload_to_upload_ee(self.scfile)
print "img addr:", file_addr
# Will break if someone uploads a file named "screenshot.png;rm -rf /"
Popen("echo {} | xclip -selection clipboard".format(file_addr), shell=True)
Popen('notify-send "Upload done" "{}"'.format(file_addr), shell=True)
notifyObj.close()
gtk.main_quit()
def doNothing(self,notifyObj, action):
print "Do not upload"
notifyObj.close()
gtk.main_quit()
def upload_to_upload_ee(file_to_upload):
""" Uploads file to upload.ee page
"""
session_id = requests.get('http://www.upload.ee/ubr_link_upload.php').text[48:][:-6]
files = {'upfile_0': open(file_to_upload, 'rb')}
upload_page = requests.post("http://www.upload.ee/cgi-bin/ubr_upload.pl?upload_id={}".format(session_id), files=files)
imgs = []
parser = HTMLParser()
def ret_imgs(tag, attrs):
for attr, val in attrs:
if attr == "id":
if val == "image_src":
imgs.append(attrs)
parser.handle_starttag = ret_imgs
parser.feed(upload_page.text)
for img in imgs:
for attr, val in img:
if attr == "value":
return val
if __name__ == "__main__":
# any new file that is created in this dir will create a event and this
# script will try to upload that new file
picture_dir = "/home/arti/Pildid"
events = Popen("inotifywait -m -e create "+picture_dir, shell=True, bufsize=16, stdout=PIPE).stdout
while True:
event = events.readline().split()
event = { "dir": event[0],
"type": event[1],
"file": " ".join(event[2:]) }
event["path"] = event["dir"] + event["file"]
print event["path"]
obj = asktoupload(event["path"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment