Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt
diff options
context:
space:
mode:
authorWalter Bender <walter@sugarlabs.org>2013-12-03 21:26:23 (GMT)
committer Walter Bender <walter@sugarlabs.org>2013-12-03 21:26:23 (GMT)
commitcfdf61e26893d1ecdd09cd98c04ee4ffe31f0bdd (patch)
treecefbb3c97ed67c2f3cb89ad9d1fe9a1ca4a1e653 /TurtleArt
parent3bb222d85b273afb68e8467b87f60b2633ee2bef (diff)
add 'get data from URL' block (Scimonster)
Diffstat (limited to 'TurtleArt')
-rw-r--r--TurtleArt/talogo.py22
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] /