كيف يمكنني تحديث محتوى المرفقات مع مواقع Google Python API؟
-
18-09-2019 - |
سؤال
أحاول كتابة برنامج نصي سيقوم تلقائيا بتحديث بعض المرفقات على موقع الويب الذي تم إنشاؤه وإدارته من خلال مواقع Google. يجب أن يكون هذا ممكنا كما أصدرت جوجل المواقع API. في سبتمبر و بيثون GDATA API. مطالبات لدعم المواقع. ومع ذلك، فإن أقرب طريقة يمكنني أن أجدها client.update
, ، والتي تتيح لي تحديث البيانات الوصفية المرفق، ولكن ليس المحتوى.
في جافا API في تحديث المرفق يتم من خلال إنشاء جديد MediaFileSource
ثم الاتصال entry.setMediaFileSource(source)
تليها entry.updateMedia()
. وبعد ومع ذلك، لا يمكنني العثور على أي شيء مماثل في بيثون API. هل أنا غبي وفقد شيء ما، أم أنه ليس من الممكن تحديث مرفق مواقع Google باستخدام Python API؟
المحلول
تم تحديث المواقع API إلى V1.1؛ ربما هذا هو إضافة جديدة
http://code.google.com/apis/sites/docs/1.0/developers_guide_python.html#updatingcontent.
نصائح أخرى
وثائق هنا يوفر مثالا على كيفية تحديث محتوى المرفق والبيانات التعريفية (استبدال القسم الفرعي لاستبدال محتوى المرفق والبيانات الوصفية)
الشيء الوحيد الذي تركه هو الحصول على existing_attachment
والتي يمكن القيام به بسهولة مع شيء مثل هذا:
existing_attachment = None
uri = '%s?kind=%s' % (client.MakeContentFeedUri(), 'attachment')
feed = client.GetContentFeed(uri=uri)
for entry in feed.entry:
if entry.title.text == title:
print '%s [%s]' % (entry.title.text, entry.Kind())
existing_attachment = entry
حسنا، API هناك غريب، والوثائق ليست واضحة جدا. هنا هو ما اعتبرته. في المرة الأولى التي تقوم فيها بتحميل مرفق، يمكنك القيام بذلك من خلال طريقة التحميل المتابعة، ولكن في محاولات المتابعة، تحتاج إلى الاتصال بالتحديث. إليك الرمز الذي يفعل ذلك:
class AttachmentUploader(object):
"""Uploads a given attachment to a given filecabinet in Google Sites."""
def __init__(self, site, username, password):
self.client = gdata.sites.client.SitesClient(
source="uploaderScript", site=site)
self.client.ssl = True
try:
self.client.ClientLogin(username, password, "some.key")
except:
traceback.print_exc()
raise
def FindAttachment(self, title):
uri = "%s?kind=%s" % (self.client.MakeContentFeedUri(), "attachment")
feed = self.client.GetContentFeed(uri=uri)
for entry in feed.entry:
if entry.title.text == title:
return entry
return None
def FindCabinet(self, title):
uri = "%s?kind=%s" % (self.client.MakeContentFeedUri(), "filecabinet")
feed = self.client.GetContentFeed(uri=uri)
for entry in feed.entry:
if entry.title.text == title:
return entry
return None
def Upload(self, cabinet_title, title, file_path, description):
"""Upload the given file as attachment."""
ms = gdata.data.MediaSource(file_path=file_path, content_type="text/ascii")
existing_attachment = self.FindAttachment(title)
if existing_attachment is not None:
existing_attachment.summary.text = description
updated = self.client.Update(existing_attachment, media_source=ms)
print "Updated: ", updated.GetAlternateLink().href
else:
cabinet = self.FindCabinet(cabinet_title)
if cabinet is None:
print "OUCH: cabinet %s does not exist" % cabinet_title
return
attachment = self.client.UploadAttachment(
ms, cabinet, title=title, description=description)
print "Uploaded: ", attachment.GetAlternateLink().href
هناك upload_attachment. الطريقة، التي يجب أن تعمل. قد ترغب أيضا في التحقق من عينة من الرموز ل API المواقع، فإنه يستخدم هذه الطريقة.