import requests import json import datetime import streamlit as st from itertools import…
- User
import requests import json import datetime import streamlit as st from itertools import zip_longest import os import pandas as pd import seaborn as sns import matplotlib.pyplot as plt
def basic_info(): config = dict() config["access_token"] = st.secrets["access_token"] config['instagram_account_id'] = st.secrets.get("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']
codeif not params['endpoint_base']: return None if pagingUrl == '': url = params['endpoint_base'] + params['instagram_account_id'] + '/media' else: url = pagingUrl return InstaApiCall(url, Params, 'GET')def getUser(params): Params = dict() Params['fields'] = 'followers_count' Params['access_token'] = params['access_token']
codeif not params['endpoint_base']: return None url = params['endpoint_base'] + params['instagram_account_id'] return InstaApiCall(url, Params, 'GET')def saveCount(count, filename): with open(filename, 'w') as f: json.dump(count, f, indent=4)
def getCount(filename): try: with open(filename, 'r') as f: return json.load(f) except (FileNotFoundError, json.decoder.JSONDecodeError): return {}
st.set_page_config(layout="wide") params = basic_info()
count_filename = "count.json"
if not params['instagram_account_id']: st.write('.envファイルでinstagram_account_idを確認') else: response = getUserMedia(params) user_response = getUser(params) if not response or not user_response: st.write('.envファイルでaccess_tokenを確認') else: posts = response['json_data']['data'][::-1] user_data = user_response['json_data'] followers_count = user_data.get('followers_count', 0)
code - Assistant
This script fetches Instagram media posts and displays various insights using Streamlit. It shows the follower counts along with the difference in followers compared to the previous day. Furthermore, it displays summary charts for followers' count, like count sum, and comments count sum.
The script provides options to view graphs of individual posts with like count and comment count. Finally, users can also display captions for each post with specific tags and emojis.
To run this script, you need to have Streamlit, Requests, Pandas, Seaborn, and Matplotlib libraries installed. Also, you need to have an access token and Instagram account ID set up in a .env file.
To run the script, open your terminal, navigate to the directory containing the script, and enter:
codestreamlit run script_name.pyReplace "script_name.py" with the name of the script file. This will launch a Streamlit web app in your default web browser with the Instagram analytics dashboard.
- User
- Assistant
It seems there is an error when trying to reverse the
postsvariable as the error indicates that it's a dictionary instead of a list. To fix the error, we need to make sure to reverse the list instead. Here's the corrected version of the code:python# ... responses = getUserMedia(params) user_response = getUser(params) if not response or not user_response: st.write('.envファイルでaccess_tokenを確認') else: data = response['json_data']['data'] posts = [post for post in data[::-1]] # ... ax.set_xticklabels(map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').strftime('%m/%d'), dates), rotation=45) st.pyplot(fig_summary) post_groups = [list(filter(None, group)) for group in zip_longest(*[iter(posts)] * NUM_COLUMNS)] # ...In this version, I removed the line
posts.reverse(), created a new variable to store the response data, and then constructed the list using a list comprehension to reverse the order. This should resolve the AttributeError you were facing. Please try running this corrected version of the code.