def send(recipient, subject, txt, html=None, dry_run=False): """Send an e-mail through the Postmark API.""" if not is_valid(recipient): raise ValueError('recipient address is invalid') config = uutils.get_config() data = { 'From': config['email']['from'], 'To': recipient, 'Subject': subject, 'TextBody': txt, } if html is not None: data['HtmlBody'] = html json_data = json.dumps(data) # Talk to Postmark. req = urllib2.Request(API_ENDPOINT) req.add_header('Accept', 'application/json') req.add_header('Content-Type', 'application/json') if dry_run: req.add_header('X-Postmark-Server-Token', 'POSTMARK_API_TEST') else: req.add_header('X-Postmark-Server-Token', config['email']['key']) req.add_data(json_data) url = urllib2.urlopen(req) # Postmark returns some info in a JSON struct. return json.loads(url.read())
#!/usr/bin/env python import argparse import json import libunison.utils as uutils import liblfm import sqlite3 import sys import urllib import urllib2 import time CONFIG = uutils.get_config() DEFAULT_IN_DATABASE = 'gen/userdata.db' DEFAULT_OUT_DATABASE = 'gen/trackdata.db' API_ROOT = 'http://ws.audioscrobbler.com/2.0/' DB_SCHEMA = """ CREATE TABLE IF NOT EXISTS tracks( artist TEXT, title TEXT, tags TEXT, features TEXT ); CREATE INDEX IF NOT EXISTS tracks_idx ON tracks(artist, title); """ QUERY_TRACK_EXISTS = 'SELECT 1 FROM tracks WHERE artist = ? AND title = ?' QUERY_INSERT_TRACK = 'INSERT INTO tracks (artist, title, tags) VALUES (?, ?, ?)'
#!/usr/bin/env python import argparse import datetime import json import libunison.utils as uutils import pika import time from libunison.models import Track from storm.locals import * CONFIG = uutils.get_config() def _parse_args(): parser = argparse.ArgumentParser() parser.add_argument('interval', type=int) return parser.parse_args() def init_track(track): """Initialize a new track. To be used when creating a new track. In concrete terms, this function generates and sends the jobs that will fetch the track's tags and other information. """ meta = {'artist': track.artist, 'title': track.title} tags_msg = json.dumps({ 'action': 'track-tags',
#!/usr/bin/env python import argparse import json import liblfm import mutagen import os.path import urllib import urllib2 from libunison.utils import GEN_ROOT, get_config CONFIG = get_config() DEFAULT_FOLDER = '%s/metadata' % GEN_ROOT API_ROOT = 'http://ws.audioscrobbler.com/2.0/' def process(file_path): # Get the ID3 tags, fail if not present. meta = mutagen.File(file_path, easy=True) try: artist = meta.get('artist', []).pop(0) except IndexError: raise LookupError("file has no ID3 'artist' tag") try: title = meta.get('title', []).pop(0) except IndexError: raise LookupError("file has no ID3 'title' tag") if len(artist) == 0 or len(title) == 0: raise LookupError("'title' or 'artist' tag is empty")
def sign(*values): """Generate a MAC for a list of values (e.g. to sign URL params).""" config = uutils.get_config() data = "".join(str(val) for val in values) mac = hmac.new(config['email']['salt'], data, hashlib.sha256) return uutils.b64enc(mac.digest()[:16])