我正在编写连接到Youtube API的代码,我看到的示例使用Oauthlib2工具中的"run"来运行auth流程.似乎我的venv安装出了问题(我已经重新安装了4次)但它找不到运行...可能是版本问题?我可以导入库的其他部分,但不能导入.tools.
代码 :
import httplib2
import os
import logging
from oauth2client import run
from oauth2client.file import Storage
# from oauth2client.client import AccessTokenRefreshError
from googleapiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from googleapiclient.errors import HttpError
import json
CLIENT_SECRETS_FILE = "client_secrets.json"
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
MISSING_CLIENT_SECRETS_MESSAGE = "Missing client secrets file"
def authenticate():
httplib2.debuglevel = 4
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
scope=YOUTUBE_READ_WRITE_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json")
credentials = storage.get()
if credentials is None or credentials.invalid:
print('invalid credentials')
credentials …Run Code Online (Sandbox Code Playgroud) 我正在编写一个小脚本,从声音文件中获取元数据,并创建一个具有所需值的字符串.我知道我做错了但是我不确定为什么,但这可能是我迭代if的方式.当我运行代码时:
import os, mutagen
XPATH= "/home/xavier/Code/autotube/tree/def"
DPATH="/home/xavier/Code/autotube/tree/down"
def get_meta():
for dirpath, directories,files in os.walk(XPATH):
for sound_file in files :
if sound_file.endswith('.flac'):
from mutagen.flac import FLAC
metadata = mutagen.flac.Open(os.path.join(dirpath,sound_file))
for (key, value) in metadata.items():
#print (key,value)
if key.startswith('date'):
date = value
print(date[0])
if key.startswith('artist'):
artist = value
#print(artist[0])
if key.startswith('album'):
album = value
#print(album[0])
if key.startswith('title'):
title = value
#print(title[0])
build_name(artist,album,title) # UnboundLocalError gets raised here
def build_name(artist,album,title):
print(artist[0],album[0],title[0])
Run Code Online (Sandbox Code Playgroud)
我随机得到了想要的结果或错误:
结果:
1967 Ravi Shankar & Yehudi Menuhin West Meets East Raga: …Run Code Online (Sandbox Code Playgroud) 我无法让Jinja2读取我的模板文件.
jinja2.exceptions.TemplateNotFound: template.html
配置Jinja2为应用程序加载模板的最简单方法大致如下:
来自jinja2 import环境,PackageLoader env = Environment(loader = PackageLoader('yourapplication','templates'))这将创建一个模板环境,其中包含默认设置和一个加载器,用于在yourapplication python包中的templates文件夹中查找模板.可以使用不同的加载器,如果要从数据库或其他资源加载模板,也可以编写自己的加载器.
要从此环境加载模板,您只需调用get_template()方法,然后返回加载的模板:
template = env.get_template('mytemplate.html')
env = Environment(loader=FileSystemLoader('frontdesk', 'templates'))
template = env.get_template('template.html')
Run Code Online (Sandbox Code Playgroud)
我的树(我已经激活了venv @frontdesk)
.
??? classes.py
??? labels.txt
??? payments.py
??? templates
??? test.py
??? venv
Run Code Online (Sandbox Code Playgroud) Google 上没有针对此错误或 StackOverflow 的结果。
我读到这pathlib是处理路径的新 Python 方式。
所以我这样做:
with open(pic_name, 'wb') as image:
image.write(download.content)
image_path = Path(pic_name).resolve()
return image_path
Run Code Online (Sandbox Code Playgroud)
当我打印时,image_path我得到了图像的完整路径,但是当我尝试将它传递给使用 ffmpeg 创建视频文件的函数时,我得到:
TypeError: Can't convert 'PosixPath' object to str implicitly
我怀疑这是因为对象是 Posix 并且 ffmpeg shell 命令需要一个字符串,但是如何将 Posix 路径转换为字符串?
因此,我正在测试这段代码:
import requests
import json
searchTerm = 'parrot'
startIndex = '0'
searchUrl = "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + \
searchTerm + "&start=" + startIndex
r = requests.get(searchUrl)
response = r.content.decode('utf-8')
result = json.loads(response)
print(r)
print(result)
Run Code Online (Sandbox Code Playgroud)
响应是:
<Response [200]>
{'responseData': None, 'responseStatus': 403, 'responseDetails': 'This API is no longer available.'}
Run Code Online (Sandbox Code Playgroud)
似乎我正在尝试使用旧的API,并且现在不推荐使用。当我检查Google自定义搜索API时,看不到任何直接在Google图片上进行搜索的方法,使用新的API甚至可以实现吗?