Skip to content

Instantly share code, notes, and snippets.

View johandahlberg's full-sized avatar

Johan Dahlberg johandahlberg

View GitHub Profile
@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
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)
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)
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
def rawbuffer2JsValue(rawBuffer: RawBuffer): Option[JsValue] = {
for {
bytes <- rawBuffer.asBytes()
} yield {
Json.parse(bytes.utf8String)
}
}
def receiveFacebookMessages = withVerifiedPayload {
Action (parse.raw) {
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
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