''' import requests import json import datetime import streamlit as st from itertools…
- User
'''
import requests
import json
import datetime
import streamlit as st
from itertools import zip_longestdef basic_info():
config = dict()
config["access_token"] = ''
config['instagram_account_id'] = ""
config["version"] = 'v16.0'
config["graph_domain"] = 'https://graph.facebook.com/'
config["endpoint_base"] = config["graph_domain"]+config["version"] + '/'
return configdef InstaApiCall(url, params, request_type):
if request_type == 'POST':
req = requests.post(url,params)
else:
req = requests.get(url,params)
res = dict()
res["url"] = url
res["endpoint_params"] = params
res["endpoint_params_pretty"] = json.dumps(params, indent=4)
res["json_data"] = json.loads(req.content)
res["json_data_pretty"] = json.dumps(res["json_data"], indent=4)
return resdef getUserMedia(params,pagingUrl=''):
Params = dict()
Params['fields'] = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username,like_count,comments_count'
Params['access_token'] = params['access_token']if pagingUrl=='': url = params['endpoint_base'] + params['instagram_account_id'] + '/media' else : url = pagingUrl return InstaApiCall(url, Params, 'GET')st.set_page_config(layout="wide")
params = basic_info()
response = getUserMedia(params)
posts = response['json_data']['data'][::-1]NUM_COLUMNS = 6 MAX_WIDTH = 1000BOX_WIDTH = int(MAX_WIDTH / NUM_COLUMNS)
BOX_HEIGHT = 400show_description = st.checkbox("説明文を表示")
posts.reverse() post_groups = [list(filter(None, group)) for group in zip_longest(*[iter(posts)]*NUM_COLUMNS)] for post_group in post_groups: with st.container(): columns = st.columns(NUM_COLUMNS) for i, post in enumerate(post_group): with columns[i]: if post['media_type'] != 'CAROUSEL_ALBUM': image = post['media_url'] st.image(image, width=BOX_WIDTH, use_column_width=True, caption=post['caption']) else: carousel_id = post['id'] url = f"{params['endpoint_base']}{carousel_id}/children" images = [post['media_url']] while True: res = InstaApiCall(url, {'access_token':params['access_token']}, 'GET') if 'paging' in res['json_data'] and 'next' in res['json_data']['paging']: url = res['json_data']['paging']['next'] else: break children = res['json_data']['data'] images += [child['media_url'] for child in children] for image in images: st.image(image, width=BOX_WIDTH, use_column_width=True, caption=post['caption']) st.write(f"{datetime.datetime.strptime(post['timestamp'], '%Y-%m-%dT%H:%M:%S%z').astimezone(datetime.timezone(datetime.timedelta(hours=9))).strftime('%Y-%m-%d %H:%M:%S')}") st.write(f"👍: {post['like_count']}\n💬: {post['comments_count']}\n") caption = post['caption'] if caption is not None: caption = caption.strip() if "[Description]" in caption: caption = caption.split("[Description]")[1].lstrip() if "[Tags]" in caption: caption = caption.split("[Tags]")[0].rstrip() caption = caption.replace("#", "") caption = caption.replace("[Equip]", "📷")caption = caption.replace("[Develop]", "🖨")
if show_description:
st.write(caption or "No caption provided")
else:
st.write(caption[:0] if caption is not None and len(caption) > 50 else caption or "No caption provided")'''
上記のコードのインデントを修正し、コード全体を省略せずに表示してください。
- Assistant
こちらが修正されたコードです。
import requests import json import datetime import streamlit as st from itertools import zip_longest def basic_info(): config = dict() config["access_token"] = '' config['instagram_account_id'] = "" config["version"] = 'v16.0' config["graph_domain"] = 'https://graph.facebook.com/' config["endpoint_base"] = config["graph_domain"]+config["version"] + '/' return config def InstaApiCall(url, params, request_type): if request_type == 'POST': req = requests.post(url,params) else: req = requests.get(url,params) res = dict() res["url"] = url res["endpoint_params"] = params res["endpoint_params_pretty"] = json.dumps(params, indent=4) res["json_data"] = json.loads(req.content) res["json_data_pretty"] = json.dumps(res["json_data"], indent=4) return res def getUserMedia(params,pagingUrl=''): Params = dict() Params['fields'] = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username,like_count,comments_count' Params['access_token'] = params['access_token'] if pagingUrl=='': url = params['endpoint_base'] + params['instagram_account_id'] + '/media' else : url = pagingUrl return InstaApiCall(url, Params, 'GET') st.set_page_config(layout="wide") params = basic_info() response = getUserMedia(params) posts = response['json_data']['data'][::-1] NUM_COLUMNS = 6 MAX_WIDTH = 1000 BOX_WIDTH = int(MAX_WIDTH / NUM_COLUMNS) BOX_HEIGHT = 400 show_description = st.checkbox("説明文を表示") posts.reverse() post_groups = [list(filter(None, group)) for group in zip_longest(*[iter(posts)]*NUM_COLUMNS)] for post_group in post_groups: with st.container(): columns = st.columns(NUM_COLUMNS) for i, post in enumerate(post_group): with columns[i]: if post['media_type'] != 'CAROUSEL_ALBUM': image = post['media_url'] st.image(image, width=BOX_WIDTH, use_column_width=True, caption=post['caption']) else: carousel_id = post['id'] url = f"{params['endpoint_base']}{carousel_id}/children" images = [post['media_url']] while True: res = InstaApiCall(url, {'access_token':params['access_token']}, 'GET') if 'paging' in res['json_data'] and 'next' in res['json_data']['paging']: url = res['json_data']['paging']['next'] else: break children = res['json_data']['data'] images += [child['media_url'] for child in children] for image in images: st.image(image, width=BOX_WIDTH, use_column_width=True, caption=post['caption']) st.write(f"{datetime.datetime.strptime(post['timestamp'], '%Y-%m-%dT%H:%M:%S%z').astimezone(datetime.timezone(datetime.timedelta(hours=9))).strftime('%Y-%m-%d %H:%M:%S')}") st.write(f"👍: {post['like_count']}\n💬: {post['comments_count']}\n") caption = post['caption'] if caption is not None: caption = caption.strip() if "[Description]" in caption: caption = caption.split("[Description]")[1].lstrip() if "[Tags]" in caption: caption = caption.split("[Tags]")[0].rstrip() caption = caption.replace("#", "") caption = caption.replace("[Equip]", "📷") caption = caption.replace("[Develop]", "🖨") if show_description: st.write(caption or "No caption provided") else: st.write(caption[:0] if caption is not None and len(caption) > 50 else caption or "No caption provided")コード全体を表示しています。ご確認ください。