API updates: default tip
authorKeyserSosa <chris@reddit.com>
Wed Aug 12 16:20:29 2009 -0700 (2 years ago)
changeset 401efac9f63b4b8
parent 400 fb69e8b57be8
API updates:
* make created_utc reference GMT
* add subreddit_id to comments
* generate useful media embed info
* fix to url validator

committer: KeyserSosa <chris@reddit.com>
r2/r2/lib/jsontemplates.py
r2/r2/lib/utils/utils.py
     1.1 --- a/r2/r2/lib/jsontemplates.py	Wed Aug 12 11:28:05 2009 -0700
     1.2 +++ b/r2/r2/lib/jsontemplates.py	Wed Aug 12 16:20:29 2009 -0700
     1.3 @@ -163,7 +163,8 @@
     1.4          elif attr == "created":
     1.5              return time.mktime(thing._date.timetuple())
     1.6          elif attr == "created_utc":
     1.7 -            return time.mktime(thing._date.astimezone(pytz.UTC).timetuple())
     1.8 +            return (time.mktime(thing._date.astimezone(pytz.UTC).timetuple())
     1.9 +                    - time.timezone)
    1.10          elif attr == "child":
    1.11              return CachedVariable("childlisting")
    1.12          return getattr(thing, attr, None)
    1.13 @@ -204,20 +205,32 @@
    1.14                                                  author       = "author", 
    1.15                                                  thumbnail    = "thumbnail",
    1.16                                                  media        = "media_object",
    1.17 +                                                media_embed  = "media_embed",
    1.18                                                  selftext     = "selftext",
    1.19                                                  num_comments = "num_comments",
    1.20                                                  subreddit    = "subreddit",
    1.21                                                  subreddit_id = "subreddit_id")
    1.22 -    
    1.23 +
    1.24      def thing_attr(self, thing, attr):
    1.25 -        if attr == 'subreddit':
    1.26 +        from r2.lib.scraper import scrapers
    1.27 +        if attr == "media_embed":
    1.28 +           if (thing.media_object and
    1.29 +               not isinstance(thing.media_object, basestring)):
    1.30 +               scraper = scrapers[thing.media_object['type']]
    1.31 +               media_embed = scraper.media_embed(**thing.media_object)
    1.32 +               return dict(scrolling = media_embed.scrolling,
    1.33 +                           width = media_embed.width,
    1.34 +                           height = media_embed.height,
    1.35 +                           content = media_embed.content)
    1.36 +           return dict()
    1.37 +        elif attr == 'subreddit':
    1.38              return thing.subreddit.name
    1.39          elif attr == 'subreddit_id':
    1.40              return thing.subreddit._fullname
    1.41          elif attr == 'selftext':
    1.42              return safemarkdown(thing.selftext)
    1.43          return ThingJsonTemplate.thing_attr(self, thing, attr)
    1.44 -                          
    1.45 +
    1.46      def rendered_data(self, thing):
    1.47          d = ThingJsonTemplate.rendered_data(self, thing)
    1.48          d['sr'] = thing.subreddit._fullname
    1.49 @@ -234,13 +247,18 @@
    1.50                                                  likes        = "likes",
    1.51                                                  author       = "author", 
    1.52                                                  link_id      = "link_id",
    1.53 +                                                sr_id        = "sr_id",
    1.54                                                  parent_id    = "parent_id",
    1.55                                                  )
    1.56  
    1.57      def thing_attr(self, thing, attr):
    1.58 -        from r2.models import Comment, Link
    1.59 +        from r2.models import Comment, Link, Subreddit
    1.60          if attr == 'link_id':
    1.61              return make_fullname(Link, thing.link_id)
    1.62 +        elif attr == 'sr_id':
    1.63 +            if hasattr(thing, attr):
    1.64 +                return make_fullname(Subreddit, thing.sr_id)
    1.65 +            return None
    1.66          elif attr == "parent_id":
    1.67              try:
    1.68                  return make_fullname(Comment, thing.parent_id)
     2.1 --- a/r2/r2/lib/utils/utils.py	Wed Aug 12 11:28:05 2009 -0700
     2.2 +++ b/r2/r2/lib/utils/utils.py	Wed Aug 12 16:20:29 2009 -0700
     2.3 @@ -329,6 +329,9 @@
     2.4          u = urlparse(url)
     2.5  
     2.6      if u.scheme and u.scheme in valid_schemes:
     2.7 +        # if there is a scheme and no hostname, it is a bad url.
     2.8 +        if not u.hostname:
     2.9 +            return
    2.10          labels = u.hostname.split('.')
    2.11          for label in labels:
    2.12              try: