Skip to content

Instantly share code, notes, and snippets.

View johandahlberg's full-sized avatar

Johan Dahlberg johandahlberg

View GitHub Profile
from pathlib import Path
from queue import Queue
def bf_search(search_for, root, max_depth):
class PathLevel():
def __init__(self, path, level):
self.path = path
self.level = level
package controllers
import java.nio.charset.StandardCharsets
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import models.RegisterFacebook
import org.apache.commons.codec.binary.Hex
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits.defaultContext
def rawbuffer2JsValue(rawBuffer: RawBuffer): Option[JsValue] = {
for {
bytes <- rawBuffer.asBytes()
} yield {
Json.parse(bytes.utf8String)
}
}
def receiveFacebookMessages = withVerifiedPayload {
Action (parse.raw) {
val verificationService = new SHA1VerificationService()
def withVerifiedPayload[A](action: Action[RawBuffer])= Action.async(parse.raw) { request =>
val xHubSignatureOption = request.headers.get("X-Hub-Signature")
Logger.debug("Attempting to verify payload signature.")
val verified =
for {
signature <- xHubSignatureOption
class SHA1VerificationService {
private val secret = "<your key>"
private def computeSHA1Hash(payloadBytes: Array[Byte], secret: String): String = {
val HMAC_SHA1_ALGORITHM = "HmacSHA1"
val secretKeySpec = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), HMAC_SHA1_ALGORITHM)
val mac = Mac.getInstance(HMAC_SHA1_ALGORITHM)
mac.init(secretKeySpec)
case class FacebookPostingMessages(`object`: String, entry: Seq[MessagePostEntry]) {
def messages(): Seq[Message] = {
entry.flatMap(_.messaging).map(_.message)
}
}
case class Correspondent(id: String)
case class MessagePostEntry(id: String, time: Long, messaging: Seq[Messaging])
case class Messaging(sender: Correspondent, recipient: Correspondent, timestamp: Long, message: Message)
case class Message(mid: String, seq: Long, text: String)
@johandahlberg
johandahlberg / lunchbot.py
Created April 29, 2016 16:04
Small bot that posts the Bikupan menu of the day to
# CC BY-SA 4.0
# http://creativecommons.org/licenses/by-sa/4.0/
import json
import time
import datetime
import requests
from bs4 import BeautifulSoup
@johandahlberg
johandahlberg / performance_test.sh
Created February 11, 2016 14:04
Quick and dirty performance test of proot at Uppmax
echo "Time in container"
for i in $(seq 1 5)
do
/usr/bin/time -f "time: %e" proot -S debian-sid --bind=/proj/a2009002/webexport/opendata/HiSeqX_CEPH/CEP-13-3/03-BAM/ samtools mpileup -r 22:1-23096112 /proj/a2009002/webexport/opendata/HiSeqX_CEPH/CEP-13-3/03-BAM/CEP-13-3.clean.dedup.recal.bam 2>&1 > test.pileup | grep time
done
echo "Time outside container"
for i in $(seq 1 5)
do
/usr/bin/time -f "time: %e" samtools mpileup -r 22:1-23096112 /proj/a2009002/webexport/opendata/HiSeqX_CEPH/CEP-13-3/03-BAM/CEP-13-3.clean.dedup.recal.bam 2>&1 > test.pileup | grep time
@johandahlberg
johandahlberg / income_difference.R
Last active December 26, 2015 13:20
Quick plot of income differences between men and women in the period 1991 to 2013 in Sweden.
# Licence:
# https://creativecommons.org/licenses/by/3.0/
library(ggplot2)
library(reshape2)
library(dplyr)
# Data:
# Downloaded at: http://www.statistikdatabasen.scb.se/pxweb/sv/ssd/START__HE__HE0110__HE0110A/SamForvInk2/?rxid=c58583e1-7fc3-418a-9395-0300e138fe7f
# Sammanräknad förvärvsinkomst, medianinkomst för boende i Sverige den 31/12, tkr efter region, kön, ålder och år
---
title: Understanding the Rwandan genocide of 1994 through data from the Uppsala Conflict
Data Program
author: "Johan Dahlberg"
date: "October 27, 2015"
output: html_document
licence: http://creativecommons.org/licenses/by-sa/3.0/
---
``` {r, echo=FALSE, message=FALSE}