将 JSON 文件写入数据库的更好方法?

发布于 2024-11-02 11:57:51 字数 2943 浏览 1 评论 0原文

这是场景: -我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-11-09 11:57:51

鉴于有效的密钥是在 embedly 的响应文档中定义的,您可以使代码更丰富一些通过在一个地方指定支持的响应键列表和您的翻译来维护,从而减少冗余代码量。

例如:

# embed.ly keys which map 1:1 with your database record keys
RESPONSE_KEYS = set([
    'provider_url', 'description', 'title', 'author_name', 'thumbnail_url',
    'thumbnail_width', 'thumbnail_height', 'author_url'
    ])

# mapping from embed.ly's key name to your database record key
KEY_MAP = {
    'url': 'content_url',
    'width': 'width_px',
    'height': 'height_px',
    'type': 'url_type'
    }

url_return = get_oembed(content_url)
record = ContentQueue(submitted_url=content_url)
record.version = 1

# iterate over the response keys and add them to the record
for key_name in url_return.iterkeys():
    key = key_name if key_name in RESPONSE_KEYS else KEY_MAP.get(key_name)
    if key:
        record[key] = url_return[key_name]

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:

# embed.ly keys which map 1:1 with your database record keys
RESPONSE_KEYS = set([
    'provider_url', 'description', 'title', 'author_name', 'thumbnail_url',
    'thumbnail_width', 'thumbnail_height', 'author_url'
    ])

# mapping from embed.ly's key name to your database record key
KEY_MAP = {
    'url': 'content_url',
    'width': 'width_px',
    'height': 'height_px',
    'type': 'url_type'
    }

url_return = get_oembed(content_url)
record = ContentQueue(submitted_url=content_url)
record.version = 1

# iterate over the response keys and add them to the record
for key_name in url_return.iterkeys():
    key = key_name if key_name in RESPONSE_KEYS else KEY_MAP.get(key_name)
    if key:
        record[key] = url_return[key_name]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文