I started to write programming & data related articles in Medium recently. Just as a new curious user to Medium, I frequently visit https://medium.com/me/stats to check how many readers has read my posts. Although those numbers rarely change, but these numbers are really what I care, at least for current several days. Since it means something for me, I want to keep trace of those numbers (especially the change trending)
But soon, I learned although Medium has API:
This repository contains the documentation for Medium 's API. Medium's API is a JSON-based OAuth2 API. All requests are…
But its API is quite limited, mainly used to publish articles to Medium. But there is no official Medium Stats API. After more research, I found an already Archived project:
An API endpoint for fetching Medium article stats. - CharlesStover/medium-stats-api
The most useful thing I learned is: when you use HTTP Clients to access https://medium.com/me/stats, if you set “SID” & “UID” inside cookie, then you can get data successfully (Although the return data is HTML, but it is very easy to use regex to extract the real stats data in JSON format), Great!
First, just visit https://medium.com/me/stats by web browser, like Chrome. Then, open Chrome’s developer tool, inside “Network” tab, find any request, and look up the “sid” & “uid” inside cookie HTTP Header. (keep those in secret), then I wrote the following Python Script to fetch my article data, and save them into Apache Parquet format.
import pandas as pd
from datetime import datetimeuid='__your_uid_from_cookie__'
sid='__your_sid_from_cookie__'r = requests.get('https://medium.com/me/stats?limit=9999',
cookies = dict(uid=uid, sid=sid)
)post_list = json.loads(re.compile('window\["obvInit"\]\((.*)\)').findall(r.text))['value']
df = pd.DataFrame(post_list)
df['fetchTime'] = datetime.now()
After python script run successfully, I have save those data into my local file. I can add some additional logic to merge those files together to keep all the data change histories.
Since data is ready, Let’s make some plots. I’d like to use open source https://streamlit.io/ tools to do this job. Streamlit is an amazing tool, by using it, I can turn small number of python lines into a fully workable web application! The Python script for my web app is as following:
import pandas as pd
import streamlit as st
import altair as altfull_df = pd.read_parquet('~/data/collections/medium_stats.parquet')
st.header('My Medium Posts\'s stats')
df = full_df[['title', 'views', 'reads','readingTime', 'claps', 'upvotes', 'fetchTime']]bars = alt.Chart(df).mark_bar().encode(
tooltip=['reads', 'claps', 'upvotes']
)text = bars.mark_text(dy=-9).encode(text='views:Q')st.altair_chart(bars + text, use_container_width=True)st.table(df)
I personally like to use altair (which wraps Vega-Lite) for visualization, but streamlit also support several alternatives. Finally, I can have my own dashboard!