Skip to content

Instantly share code, notes, and snippets.

@jarhill0
jarhill0 / speeds.py
Created May 28, 2020 00:03
Test Reddit ratelimit usage rate — runs for 60 seconds without output and then exits
from time import monotonic
import praw
reddit = praw.Reddit(...) # authenticate
print(f'Authenticated as {reddit.user.me()}')
start_time = monotonic()
used_at_start = reddit._core._rate_limiter.used
@jarhill0
jarhill0 / bottles.rb
Last active May 17, 2020 18:57
My first time writing Ruby — worked for a half hour on the introductory exercise from "99 Bottles of OOP." All tests pass!
class Bottles
def song
verses 99, 0
end
def verses(start, stop)
start.downto(stop).map {|v| self.verse v}.join("\n")
end
def verse(num)
"#{self.bottle_text(num).capitalize} of beer on the wall, #{self.bottle_text num} of beer.
#{self.next_action num}
@jarhill0
jarhill0 / repro.py
Created February 12, 2020 21:28
Checking for multiple exceptions.
import praw
SUB_NAME = "###"
# ^ This name should be a subreddit that only allows link posts and not
# self posts. Preferably you should mod it (but not with the account
# that you run this script as), because it is going to get spammed.
reddit = praw.Reddit() # fill in
subreddit = reddit.subreddit(SUB_NAME)
@jarhill0
jarhill0 / hook_demo.py
Last active January 13, 2020 01:08
Demonstrating use of new cassette load/eject hooks in Betamax
from betamax import Betamax
from requests import Session
def before(cassette): print('cassette created.')
def after(cassette): print('cassette destroyed')
session = Session()
recorder = Betamax(session, cassette_library_dir='test cassettes')
@jarhill0
jarhill0 / kishi_bashi_shows.py
Created June 14, 2019 19:18
Get Kishi Bashi's shows and build a nice markdown table.
"""Get Kishi Bashi's shows and build a nice markdown table."""
from datetime import datetime
from urllib.parse import urlparse
import requests
def main():
shows = requests.get('https://rest.bandsintown.com/artists/Kishi Bashi/events',
@jarhill0
jarhill0 / prawbject_to_json.py
Created April 30, 2019 03:55
Sample code for converting a PRAW Comment to JSON
import praw
from json import dumps, JSONEncoder
reddit = praw.Reddit(username='',
password='',
client_id='',
client_secret='',
user_agent='')
comment = reddit.comment('em3rygg')
#include <stdio.h>
#define DIGITS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
void my_itoa(int num, char* str, int base) {
if (num < 0) {
num *= -1;
str[0] = '-';
str++;
}
@jarhill0
jarhill0 / print_argv.c
Created February 18, 2019 00:31
C code sample for printing all argument variables
#include <stdio.h>
int main(int argc, char* argv[]) {
for (int i = 0; i < argc; i++)
printf("%s\n", argv[i]);
return 0;
}
@jarhill0
jarhill0 / poly_contains.py
Created June 28, 2018 20:15
Determine whether a point lies within a polygon. I think the edge and corner cases work properly.
from random import random
def contains_point(polygon, point):
"""Determine whether point is contained within polygon.
:param polygon: An ordered sequence of length-2 tuples that represent the (x, y) coordinates of points that,
when connected, form a polygon that does not intersect itself. The last point will connect with the first
one.
:param point: A length-2 tuple that represents the (x, y) coordinates of a point.
@jarhill0
jarhill0 / bandcamp.py
Created May 31, 2018 03:23
Use regex to parse HTML and make a Bandcamp embed.
"""Use regex to parse HTML and make a Bandcamp embed."""
import re
import requests
TEMPLATE = """
<iframe style="border: 0; width: 276px; height: 390px;"
src="https://bandcamp.com/EmbeddedPlayer/track={track_number}/size=large/bgcol=ffffff/linkcol=17a2b8/tracklist=false/"
seamless>