Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter way to write a JSON file to a DB?
    primarykey
    data
    text
    <p>Here's the scenario: -I'm using embed.ly's oembed service to pull metadata about user submitted links (more info about that service: <a href="http://api.embed.ly/docs/oembed" rel="nofollow">http://api.embed.ly/docs/oembed</a>) -I use this content to generate previews of content on my site -When I submit a URL to embed.ly the service gives me back a JSON file containing the metadata -I want to write this to a database since users will be accessing this information repeatedly on my website -I'm using Django</p> <p>I have the scritp working. Below is my code. What I don't like about it is that it hard codes the keys that are found in the JSON file. If the keys change, or are not provided on a given query, then things break. I can fix the later issue, but was curious if anyone had a different approach that would tolerate missing data or changing keys. </p> <p>Here is the Python code that generates the JSON file (got this from embed.ly):</p> <pre><code>def submit_content(request): import urllib import urllib2 try: import json except ImportError: try: import simplejson as json except ImportError: raise ImportError("Need a json decoder") ACCEPTED_ARGS = ['maxwidth', 'maxheight', 'format'] def get_oembed(url, **kwargs): """ Example Embedly oEmbed Function """ api_url = 'http://api.embed.ly/1/oembed?' params = {'url':url } for key, value in kwargs.items(): if key not in ACCEPTED_ARGS: raise ValueError("Invalid Argument %s" % key) params[key] = value oembed_call = "%s%s" % (api_url, urllib.urlencode(params)) return json.loads(urllib2.urlopen(oembed_call).read()) </code></pre> <p>And here is my code that writes this to the DB:</p> <pre><code>if request.method == 'POST': form = SubmitContent(request.POST) if form.is_valid(): user = request.user content_url = form.cleaned_data['content_url'] url_return = get_oembed(content_url) recordSave = ContentQueue(submitted_url=content_url) for key in url_return: if key == 'provider_url': recordSave.provider_url = url_return[key] if key == 'description': recordSave.description = url_return[key] if key == 'title': recordSave.title = url_return[key] if key == 'url': recordSave.content_url = url_return[key] if key == 'author_name': recordSave.author_name = url_return[key] if key == 'height': recordSave.height_px = url_return[key] if key == 'width': recordSave.width_px = url_return[key] if key == 'thumbnail_url': recordSave.thumbnail_url = url_return[key] if key == 'thumbnail_width': recordSave.thumbnail_width = url_return[key] if key == 'version': recordSave.version = 1 if key == 'provider_name': recordSave.provider_name = url_return[key] if key == 'cache_age': recordSave.cache_age = url_return[key] if key == 'type': recordSave.url_type = url_return[key] if key == 'thumbnail_height': recordSave.thumbnail_height = url_return[key] if key == 'author_url': recordSave.author_url = url_return[key] recordSave.user = user </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload