Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python3
import sys, os
def fix_eol(filename):
with open(filename) as f:
contents = f.read()
if not contents.endswith("\n"):
contents += "\n"
@Fedjmike
Fedjmike / README.md
Last active October 30, 2018 18:01 — forked from cjrd/README.md
Interactive tool for creating directed graphs using d3.js.

directed-graph-creator

Interactive tool for creating directed graphs, created using d3.js.

Demo: http://bl.ocks.org/cjrd/6863459

Operation:

  • drag/scroll to translate/zoom the graph
@Fedjmike
Fedjmike / relative_datetime.py
Created January 1, 2017 22:31
Concise and precise relative datetime
def relative_datetime(then):
delta = arrow.now() - then
years, days_of_year = delta.days // 365, delta.days % 365
months_of_year, days_of_month = days_of_year // 30, days_of_year % 30
hours_of_day, seconds_of_hour = delta.seconds // (60*60), delta.seconds % (60*60)
minutes_of_hour, seconds_of_minute = seconds_of_hour // 60, seconds_of_hour % 60
if years != 0:
@Fedjmike
Fedjmike / with_request_values_positionally.py
Last active November 30, 2016 17:11
with_request_values_positionally
@decorator_with_args
def with_request_values_positionally(view, keys=[], optional_keys=[],
error_view=request_by_json_missing_value):
try:
values = [request.values[key] for key in keys]
#Only catch a KeyError from that particular lookup, not the whole view function
except KeyError:
return error_view()
import sys
from model import Model, NotFound
from tools import get_wikipedia_summary
def safe_print(*args):
try:
print(*args)
except:
pass
@Fedjmike
Fedjmike / fix_release_art.py
Created July 26, 2016 23:47
Fix release cover arts whose canonical URL has changed
import requests
from multiprocessing import Pool
from model import Model, NotFound
from mb_api_import import get_album_art_urls
def safe_print(*args):
try:
print(*args)
except:
pass
@Fedjmike
Fedjmike / convert_datetimes.py
Last active April 24, 2016 14:43
Convert user and action creation datetimes from ISO 8601 (EDT) to Unix timestamps
import arrow
from model import Model
def convert_actions():
with Model() as model:
model.db.executescript(
"""drop table if exists actions_2;
create table actions_2 (
id integer primary key,
user_id integer not null,
@Fedjmike
Fedjmike / assign_artist_image.py
Last active April 24, 2016 12:24
Scrape wikipedia images
import sys
from model import Model, NotFound
from tools import get_wikipedia_image
def safe_print(*args):
try:
print(*args)
except:
pass
@Fedjmike
Fedjmike / assign_artist_mbids.py
Created April 5, 2016 10:33
Work out artist mbids using release mbids, incomplete field
import sys
from model import Model, NotFound
import musicbrainzngs as mb
def mb_get_author_ids(release_mbid):
mb.set_useragent("Skiller", "0.0.0", "mb@satyarth.me")
response = mb.get_release_by_id(release_mbid, includes=["artists"])["release"]
return [artist["artist"]["id"] for artist in response["artist-credit"] if isinstance(artist, dict)]
@Fedjmike
Fedjmike / model.py
Last active April 4, 2016 03:27
merging releases
def merge_into_release(self, dest_release, src_release):
"""Transfers all data from a release to another and removes the source.
Overwrites the destination where the release info differs.
There is never loss of user information."""
#merge = replace in dest + remove in src
#move = insert in dest + remove in src
def move_actions(model, dest_id, src_id):
model.execute("update actions set object_id=? where object_id=?", src_id, dest_id)