将 JSON 文件写入数据库的更好方法?
这是场景: -我正在使用 embed.ly 的 oembed 服务来提取有关用户提交的链接的元数据(有关该服务的更多信息:http ://api.embed.ly/docs/oembed) -我使用此内容在我的网站上生成内容预览 - 当我向 embed.ly 提交 URL 时,该服务会返回一个包含元数据的 JSON 文件 -我想将其写入数据库,因为用户将在我的网站上重复访问此信息 -我正在使用 Django,
我的脚本可以工作。下面是我的代码。我不喜欢它的是它对 JSON 文件中找到的键进行硬编码。如果键发生变化,或者在给定的查询中未提供键,那么事情就会崩溃。我可以解决后面的问题,但很好奇是否有人有不同的方法来容忍丢失数据或更改密钥。
这是生成 JSON 文件的 Python 代码(从 embed.ly 获取):
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())
这是我将其写入数据库的代码:
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
Here's the scenario:
-I'm using embed.ly's oembed service to pull metadata about user submitted links (more info about that service: http://api.embed.ly/docs/oembed)
-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
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.
Here is the Python code that generates the JSON file (got this from embed.ly):
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())
And here is my code that writes this to the DB:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于有效的密钥是在 embedly 的响应文档中定义的,您可以使代码更丰富一些通过在一个地方指定支持的响应键列表和您的翻译来维护,从而减少冗余代码量。
例如:
Given that the valid keys are defined in embedly's repsonse documentation you can make your code a bit more maintaininable by specifying the list of supported response keys and your translations in one place, reducing the amount of redundant code.
For example: