Skip to content

Instantly share code, notes, and snippets.

@Mm2PL
Last active August 7, 2023 13:19
Show Gist options
  • Save Mm2PL/06dcb239f722195a1c6796b5e412a83f to your computer and use it in GitHub Desktop.
Save Mm2PL/06dcb239f722195a1c6796b5e412a83f to your computer and use it in GitHub Desktop.
Chatter list fetcher eShrug
  • fetcher.py python version. More cross platform. Relies on requests
  • fetcher.sh smaller version but relies on bash, diff, curl
import datetime
import difflib
import json
import argparse
import os
import logging
import requests
class Args:
channel: str
base_file: str
diff_prefix: str
user_agent: str
log_level: str
def take_snapshot(channel: str, user_agent: str) -> str:
"""Takes a snapshot of the chatter list"""
url = f'https://tmi.twitch.tv/group/user/{channel}/chatters'
logging.info(f'Fetching {url!r} with UA {user_agent!r}')
r = requests.get(url, headers={
'User-Agent': user_agent
})
if r.status_code != 200:
logging.error(f'Fetched {url!r} with status {r.status_code} (UA: {user_agent!r})')
else:
logging.info(f'Fetched with status {r.status_code}')
r.raise_for_status()
data = r.json()
return json.dumps(data, sort_keys=True, indent=4) # make sure diff doesn't get a one line JSON
def create_diff(base_data: str, current_data: str, filename: str) -> str:
"""Diffs two strings"""
return '\n'.join(list(difflib.unified_diff(
base_data.split('\n'), current_data.split('\n'), n=0, fromfile=filename, tofile=filename
)))
def main(args: Args) -> int:
if os.path.isfile(args.base_file):
with open(args.base_file, 'r') as f:
base_data = ''.join(f.readlines())
else:
base_data = ''
snapshot: str = take_snapshot(args.channel, args.user_agent)
diff = create_diff(base_data, snapshot, args.base_file)
diff_fname = args.diff_prefix + datetime.datetime.now().isoformat()
logging.info(f'Writing {diff_fname!r}.')
with open(diff_fname, 'w') as f:
f.write(diff)
logging.info(f'Writing {args.base_file!r}.')
with open(args.base_file, 'w') as f:
f.write(snapshot)
return 0
if __name__ == '__main__':
p = argparse.ArgumentParser()
p.add_argument('--channel', required=True, dest='channel')
p.add_argument('--base', required=True, dest='base_file')
p.add_argument('--diff-prefix', required=True, dest='diff_prefix')
p.add_argument('--user-agent', required=False, dest='user_agent', default='Chatter snapshot script v1.0')
p.add_argument('--log', required=False, dest='log_level', default='INFO')
arguments = p.parse_args(namespace=Args())
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=arguments.log_level)
exit(main(arguments))
#!/bin/bash
if [ $# -ne 3 ]; then
echo "Wrong arguments provided" 1>&2
echo "$0 <channel> <base> <diffprefix>"
echo " channel -- Channel to take a snapshot of."
echo " base -- Base chatters revision file. \"current.json\" is a good value"
echo " diffprefix -- Prefix for diff files. Filename will be PREFIX[ISO 8601 date time]"
exit 1
fi
channel=$1
base=$2
diffprefix=$3
temp=$(mktemp)
url="https://tmi.twitch.tv/group/user/$channel/chatters"
echo "Fetching \"$url\"."
curl "$url" -s > $temp || exit
diffname=$diffprefix$(date --iso-8601=seconds)
[ -f "$base" ] || touch "$base"
diff --unified "$base" "$temp" > "$diffname"
cat "$temp" > "$base"
rm "$temp" # dispose of $temp created earlier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment