Last active
November 13, 2019 18:40
-
-
Save hasandiwan/391e9cc31c73b1f580992e8531ef2d89 to your computer and use it in GitHub Desktop.
Graph a user's participated subreddits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!.virtualenvs/data/bin/python | |
import argparse | |
import base64 | |
from datetime import date | |
from collections import Counter | |
from io import StringIO | |
import os | |
import logging | |
import string | |
import sys | |
import tempfile | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import requests | |
if __name__ == '__main__': | |
logging.basicConfig(level=logging.DEBUG) | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-u', '--user', type=str) | |
parsed = parser.parse_args() | |
URL = '''https://www.reddit.com/user/{0}/overview.json'''.format(parsed.user) | |
user_record = requests.get(URL, params={'limit': 100}, headers={'User-Agent': "u/THVAQLJZawkw8iCKEZAE, u/cruyff8's user graph"}).json()['data']['children'] | |
subreddit_counter = Counter() | |
for s in user_record: | |
subreddit_counter[s['data']['subreddit']] += 1 | |
df = pd.DataFrame({'Name': subreddit_counter.keys(), 'Count': subreddit_counter.values()}) | |
n_bins = len(subreddit_counter.keys()) | |
x = n_bins | |
y = subreddit_counter.values() | |
fig, axs = plt.subplots(1, 1, sharey=True, tight_layout=True) | |
axs.tick_params(axis='x', rotation=90) | |
axs.hist(y, bins=x, density=False) | |
axs.set_xticklabels(subreddit_counter.keys()) | |
plt.title("{0}'s subreddits, as of {1}".format(parsed.user, date.today().strftime('%b %d, %Y'))) | |
output = tempfile.NamedTemporaryFile(suffix='.png') | |
plt.savefig(output.name) | |
img_data = base64.encodestring(open(output.name, 'rb').read()) | |
resp = requests.post('https://api.imgur.com/3/image', data={'image': img_data}, headers={'Authorization': 'Client-ID e253ce5428d6eb1'}) | |
print('Image stored on '+resp.json().get('data').get('link')) | |
plt.show() |
Author
hasandiwan
commented
Nov 13, 2019
via email
Feel free to steal it if you need to :)
…On Wed, 13 Nov 2019, 02:35 Ailson Rocha Freire, ***@***.***> wrote:
[image: Boxbe] <https://www.boxbe.com/overview> This message is eligible
for Automatic Cleanup! ***@***.***) Add cleanup rule
<https://www.boxbe.com/popup?url=https%3A%2F%2Fwww.boxbe.com%2Fcleanup%3Fkey%3Dz5jlBDKQ9IGkrtXcultFi%252FP86ci%252B%252F%252Bu2TL0gYadjb8k%253D%26token%3DoCDKLqlAgT%252BxmrUwXSIv%252BF1N4tCH4Q7UaaaVgSZT9MWmZ6KtCl5F44EPDalFyyxRbyqMFa4%252FJ4%252Br5vdifo5L52%252B7B4t4W%252BwA47DvB3KMwIBXxffeXJDcC%252BsSHbUORnwRPkMQdrn4uOGGn6E61UNIIg%253D%253D&tc_serial=51175179507&tc_rand=1236985246&utm_source=stf&utm_medium=email&utm_campaign=ANNO_CLEANUP_ADD&utm_content=001>
| More info
<http://blog.boxbe.com/general/boxbe-automatic-cleanup?tc_serial=51175179507&tc_rand=1236985246&utm_source=stf&utm_medium=email&utm_campaign=ANNO_CLEANUP_ADD&utm_content=001>
Very nice Gists!
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/391e9cc31c73b1f580992e8531ef2d89?email_source=notifications&email_token=AKSFUTNQP5LH3PYFFBR3LS3QTPJ5ZA5CNFSM4JMYN3VKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF4DQE#gistcomment-3081986>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKSFUTLIKPDEPKPVHFJQTNLQTPJ5ZANCNFSM4JMYN3VA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment