diff options
Diffstat (limited to 'TurtleArt/talogo.py')
-rw-r--r-- | TurtleArt/talogo.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index ba76085..11e98f7 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -29,6 +29,8 @@ from operator import isNumberType import os from os.path import exists as os_path_exists from UserDict import UserDict +import urllib2 +import tempfile try: from sugar.graphics import style @@ -1060,6 +1062,26 @@ class LogoCode: gobject.idle_add(self.tw.send_event, event) os.remove(tmp_file) + def get_from_url(self, url): + """ Get contents of URL as text or tempfile to image """ + if "://" not in url: # no protocol + url = "http://" + url # assume HTTP + try: + req = urllib2.urlopen(url) + except urllib2.HTTPError: + debug_output("Couldn't open %s" % (url), self.tw.running_sugar) + raise logoerror(url) + + if req.info().getheader("Content-Type")[0:5] == "image": + # it can't be deleted immediately, or else we won't ever access it + tmp = tempfile.NamedTemporaryFile(delete=False) + tmp.write(req.read()) # prepare for writing + tmp.flush() # actually write it + obj = Media('media', value=tmp.name) + return obj + else: + return req.read() + def showlist(self, objects): """ Display list of media objects """ x = (self.tw.turtles.get_active_turtle().get_xy()[0] / |