在 yampy python yammer api 中只有登录后才能查看图像
in yampy python yammer api images are only viewable if logged in
python 的 yampy yammer 包允许您获取 url 消息中的图像。但是,这些图像只有在您登录后才能查看。
我可以使用访问令牌通过 api 访问消息内容,但是如何下载图像?
import yampy
import pprint
from werkzeug.wrappers import Request, Response
from datetime import datetime
import textwrap
import os,webbrowser
from flask import Flask
from flask import jsonify
pp = pprint.PrettyPrinter(indent=4)
authenticator = yampy.Authenticator(client_id="asdfadsfasdfsdf",
client_secret="asdfasdfasdf")
app = Flask(__name__)
redirect_uri = "https://localhost:8000/yammerasdf/api/v1/redirect"
# auth_url = authenticator.authorization_url(redirect_uri=redirect_uri)
# access_token = authenticator.fetch_access_token(code)
# access_data = authenticator.fetch_access_data(code)
# access_token = access_data.access_token.token
# user_info = access_data.user
# network_info = access_data.network
yammer = yampy.Yammer(access_token="asdfasdfasdfsd-asdfasdfadsf")
@app.route("/")
def hello():
messages = yammer.messages.from_user(2323423432, older_than=None, newer_than=None, limit=3, threaded=1)
# asdfmessages = yammer.messages.from_user(1663406272)
allposts = ''
messagesdata= []
for message in messages.messages:
post= '<div class="chat-body clearfix"><div class="header"><strong class="primary-font">asdfasdfdsf</strong>'
d1= message.created_at.split()[0]
d = datetime.strptime(d1, '%Y/%m/%d')
day_string = d.strftime('%A, %B %d')
post+= '<small class="pull-right text-muted">' + day_string + '</small></div>'
post+='<p style="margin-top:10px"><a href="' + message.web_url + '" target="_blank" style="font-size:13px;padding:15px" class="thumbnail">'
messageimages = []
for image in message.attachments:
webbrowser.open(image.image.url)
# urllib.request.urlretrieve(image.image.url)
# urllib.urlretrieve(image.image.url, "local-filename.jpg")
post+='<img src="' + image.image.url + '">'
messageimages.append(image.image.url)
post+= textwrap.wrap(message.content_excerpt, 120)[0] + '...</a></p></div><hr>'
allposts += post
messagedict = {'date':day_string, 'url':message.web_url, 'images':messageimages, 'content':textwrap.wrap(message.content_excerpt, 120)[0]}
messagesdata.append(messagedict.copy())
# messagesdata=['html',allposts]
return jsonify(messagesdata)
if __name__ == '__main__':
from werkzeug.serving import run_simple
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
# run_simple('localhost', port, app)
Yampy 只返回 json 格式的数据。要下载图像,您需要原始响应和流。为此,您可以改用请求和 shutil:
import requests
import shutil
headers = {"Authorization": "Bearer " + access_token}
response = requests.get("yammer_image_url", headers=headers, stream=True)
with open("path_to_new_image.jpg", "wb") as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
python 的 yampy yammer 包允许您获取 url 消息中的图像。但是,这些图像只有在您登录后才能查看。
我可以使用访问令牌通过 api 访问消息内容,但是如何下载图像?
import yampy
import pprint
from werkzeug.wrappers import Request, Response
from datetime import datetime
import textwrap
import os,webbrowser
from flask import Flask
from flask import jsonify
pp = pprint.PrettyPrinter(indent=4)
authenticator = yampy.Authenticator(client_id="asdfadsfasdfsdf",
client_secret="asdfasdfasdf")
app = Flask(__name__)
redirect_uri = "https://localhost:8000/yammerasdf/api/v1/redirect"
# auth_url = authenticator.authorization_url(redirect_uri=redirect_uri)
# access_token = authenticator.fetch_access_token(code)
# access_data = authenticator.fetch_access_data(code)
# access_token = access_data.access_token.token
# user_info = access_data.user
# network_info = access_data.network
yammer = yampy.Yammer(access_token="asdfasdfasdfsd-asdfasdfadsf")
@app.route("/")
def hello():
messages = yammer.messages.from_user(2323423432, older_than=None, newer_than=None, limit=3, threaded=1)
# asdfmessages = yammer.messages.from_user(1663406272)
allposts = ''
messagesdata= []
for message in messages.messages:
post= '<div class="chat-body clearfix"><div class="header"><strong class="primary-font">asdfasdfdsf</strong>'
d1= message.created_at.split()[0]
d = datetime.strptime(d1, '%Y/%m/%d')
day_string = d.strftime('%A, %B %d')
post+= '<small class="pull-right text-muted">' + day_string + '</small></div>'
post+='<p style="margin-top:10px"><a href="' + message.web_url + '" target="_blank" style="font-size:13px;padding:15px" class="thumbnail">'
messageimages = []
for image in message.attachments:
webbrowser.open(image.image.url)
# urllib.request.urlretrieve(image.image.url)
# urllib.urlretrieve(image.image.url, "local-filename.jpg")
post+='<img src="' + image.image.url + '">'
messageimages.append(image.image.url)
post+= textwrap.wrap(message.content_excerpt, 120)[0] + '...</a></p></div><hr>'
allposts += post
messagedict = {'date':day_string, 'url':message.web_url, 'images':messageimages, 'content':textwrap.wrap(message.content_excerpt, 120)[0]}
messagesdata.append(messagedict.copy())
# messagesdata=['html',allposts]
return jsonify(messagesdata)
if __name__ == '__main__':
from werkzeug.serving import run_simple
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
# run_simple('localhost', port, app)
Yampy 只返回 json 格式的数据。要下载图像,您需要原始响应和流。为此,您可以改用请求和 shutil:
import requests
import shutil
headers = {"Authorization": "Bearer " + access_token}
response = requests.get("yammer_image_url", headers=headers, stream=True)
with open("path_to_new_image.jpg", "wb") as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)