使用 Python 将照片上传至 Photobucket

如何在 Python 中编写一个脚本,将照片上传至 Photobucket,并获取该照片的 URL?
在这里插入图片描述

解决方案

  1. 安装 Photobucket API 包装器

    • 下载 Photobucket API 包装器并将其放到 Python 路径中。
    • 安装 httplib2 库。
  2. 请求 Photobucket API 密钥

    • 访问 Photobucket 网站并申请 API 密钥。
  3. 编写 Python 脚本

    • 导入必要的库。
    • 设置 Photobucket API 密钥和用户名。
    • 创建一个图像记录,其中包含要上传的图像路径、标题和描述。
    • 将图像上传至 Photobucket。
    • 获取上传文件的 URL。
import pbapi

import webbrowser
import cPickle
import os
import re
import sys
from xml.etree import ElementTree

__author__ = "leoluk"

###############################################
##               CONFIGURATION               ##
###############################################

# File in which the oAuth token will be stored
TOKEN_FILE = "token.txt"

IMAGE_PATH = r"D:\Eigene Dateien\Bilder\SC\foo.png"

IMAGE_RECORD = {
    "type": 'image',
    "uploadfile": '@'+IMAGE_PATH,

    "title": "My title", # <---
    "description": "My description", # <---
}

ALBUM_NAME = None # default album if None


API_KEY = "149[..]"
API_SECRET = "528[...]"


###############################################
##                   SCRIPT                  ##
###############################################

api = pbapi.PbApi(API_KEY, API_SECRET)

api.pb_request.connection.cache = None

# Test if service online
api.reset().ping().post()

result = api.reset().ping().post().response_string

ET = ElementTree.fromstring(result)

if ET.find('status').text != 'OK':
    sys.stderr.write("error: Ping failed \n"+result)
    sys.exit(-1)

try:
    # If there is already a saved oAuth token, no need for a new one
    api.username, api.pb_request.oauth_token = cPickle.load(open(TOKEN_FILE))
except (ValueError, KeyError, IOError, TypeError):
    # If error, there's no valid oAuth token

    # Getting request token
    api.reset().login().request().post().load_token_from_response()

    # Requesting user permission (you have to login with your account)
    webbrowser.open_new_tab(api.login_url)

    raw_input("Press Enter when you finished access permission. ")

    #Getting oAuth token
    api.reset().login().access().post().load_token_from_response()


# This is needed for getting the right subdomain
infos = api.reset().album(api.username).url().get().response_string

ET = ElementTree.fromstring(infos)

if ET.find('status').text != 'OK':
    # Remove the invalid oAuth
    os.remove(TOKEN_FILE)
    # This happend is user deletes the oAuth permission online
    sys.stderr.write("error: Permission deleted. Please re-run.")
    sys.exit(-1)

# Fresh values for username and subdomain
api.username = ET.find('content/username').text
api.set_subdomain(ET.find('content/subdomain/api').text)

# Default album name
if not ALBUM_NAME:
    ALBUM_NAME = api.username

# Debug :-)
print "User: %s" % api.username

# Save the new, valid oAuth token
cPickle.dump((api.username, api.oauth_token), open(TOKEN_FILE, 'w'))

# Posting the image
result = (api.reset().album(ALBUM_NAME).
          upload(IMAGE_RECORD).post().response_string)

ET = ElementTree.fromstring(result)

if ET.find('status').text != 'OK':
    sys.stderr.write("error: File upload failed \n"+result)
    sys.exit(-1)


# Now, as an example what you could do now, open the image in the browser

webbrowser.open_new_tab(ET.find('content/browseurl').text)
  1. 运行脚本

    • 保存脚本并运行它。
  2. 查看上传的图像

    • 访问 Photobucket 网站并查看上传的图像。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值