2011-04-19 22 views
1

這裏的情景: - 我使用embed.ly真實透過oEmbed服務,瞭解有關用戶提交的鏈接(有關該服務的詳細信息:http://api.embed.ly/docs/oembed)元數據 -I使用此內容來生成我的網站上 - 當內容預覽我提交的URL embed.ly的服務能讓我回包含元數據 -I想寫這樣一個數據庫,因爲用戶會被重複訪問上使用Django將JSON文件寫入數據庫的更好方法?

我的網站 - 我該信息的JSON文件

我有scritp的工作。以下是我的代碼。我不喜歡的是它對JSON文件中找到的鍵進行了硬編碼。如果鍵改變了,或者沒有在給定的查詢中提供,那麼事情就會中斷。我可以修復後面的問題,但很好奇,如果任何人有一個不同的方法,可以容忍丟失的數據或更改密鑰。

這裏是產生JSON文件(從embed.ly得到這個)的Python代碼:

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()) 

這裏是我的代碼,這種寫入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 

回答

0

鑑於在embedly's repsonse documentation中定義了有效密鑰,您可以通過在一個位置指定支持的響應密鑰列表和您的翻譯來減少冗餘代碼的數量,從而使代碼更易於維護。

例如:

# 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] 
+0

謝謝!正如你可能知道的,我是編程新手。這很好。我甚至能夠將它插入到CouchDB中。 – tabdon 2011-04-24 00:18:26