Commits

dan mackinlay committed d4af9f6

basic default-size image grabbing now works and test passes accordingly

  • Participants
  • Parent commits fbe3cf5

Comments (0)

Files changed (2)

File resocializedtext/flickr.py

-# Import Docutils document tree nodes module.
+"""
+http://www.flickr.com/services/api/misc.urls.html
+"""
+
 from docutils import nodes
-# Import Directive base class.
 from docutils.parsers.rst import Directive, directives
 from docutils.parsers.rst.directives.images import Image
+import flickrapi
+from string import Template
+_photo_uri_t = Template(
+  "http://farm${farm_id}.static.flickr.com/${server_id}/${id}_${secret}.jpg")
+_photo_page_t = Template(
+  "http://www.flickr.com/photos/${user_id}/${id}")
 
 class FlickrImage(Image):
-
+    FLICKR_KEY = None
+    flickr = None
+    option_spec = Image.option_spec.copy()
+    del(option_spec['target'])
+    option_spec['flickr_key'] = directives.unchanged
+    # option_spec['secret'] = directives.unchanged #not implemented
+    
     def run(self):
-        flickr_id = self.arguments[0]
-        self.options['target'] = flickr_id
+        # we give ourselves a chance to get the flickr key from options if not
+        # set manually on the class
+        if self.FLICKR_KEY is None: 
+            self.FLICKR_KEY = self.options['flickr_key']
+        if self.flickr is None:
+            self.flickr = flickrapi.FlickrAPI(self.FLICKR_KEY, cache=True)
+        photo_id = self.arguments[0]
+        resp = self.flickr.photos_getInfo(photo_id = photo_id)
+        photo = resp.getchildren()[0]
+        secret = photo.attrib['secret']
+        farm_id = photo.attrib['farm']
+        server_id = photo.attrib['server']
+        user_id = photo.find('owner').attrib['nsid']
+        photo_uri = _photo_uri_t.substitute(
+            secret = secret,
+            farm_id = farm_id,
+            server_id = server_id,
+            id = photo_id
+        )
+        self.arguments[0] = photo_uri
+        self.options['target'] = _photo_page_t.substitute(
+            user_id = user_id,
+            id = photo_id
+        )
         return super(FlickrImage, self).run()

File tests/test_flickr.py

 
 totest['flickr'] = [
 ["""\
-.. flickrimage:: 123456789
+.. flickrimage:: 3181047210
+  :flickr_key: 649a509753ac936cfd9fb57679de4f46
 """,
 """\
 <document source="test data">
-    <substitution_definition names="date">
-        %s
-    <paragraph>
-        Today's date is \n\
-        <substitution_reference refname="date">
-            date
-        .
+    <reference refuri="http://www.flickr.com/photos/54903489@N00/3181047210">
+        <image flickr_key="649a509753ac936cfd9fb57679de4f46" uri="http://farm4.static.flickr.com/3125/3181047210_bfcc1271a6.jpg">
 """],
 ]