Skip to content

Instantly share code, notes, and snippets.

@techjanitor
techjanitor / sfs.go
Last active August 29, 2015 14:07
Query Stop Forum Spam
func CheckStopForumSpam(ip string) error {
sfs_endpoint, err := url.Parse("http://api.stopforumspam.org/api")
if err != nil {
return errors.New("error parsing sfs endpoint")
}
queryValues := url.Values{}
if ip == "" {
@techjanitor
techjanitor / md5_tee.go
Last active August 29, 2015 14:07
Get MD5 hash with TeeReader
// Make new md5
hasher := md5.New()
// Make a new file
image, err := os.Create(imagefile)
if err != nil {
err = errors.New("problem creating file")
return
}
@techjanitor
techjanitor / magic.go
Last active August 29, 2015 14:07
Check file signatures like libmagic
image, err := os.Open(imagefile)
if err != nil {
err = errors.New("problem opening file")
return
}
defer image.Close()
// Check file type
bytes := make([]byte, 4)
n, _ := image.ReadAt(bytes, 0)
@techjanitor
techjanitor / upload.go
Last active January 31, 2016 13:32
Upload to a multipart/form-data form
func UploadPosts(file, date, name, comment, url string) (err error) {
// Make a new multipart writer with a buffer
var b bytes.Buffer
w := multipart.NewWriter(&b)
// This is just a random form field
if err = w.WriteField("name", name); err != nil {
return
}
@techjanitor
techjanitor / scrape.go
Last active August 29, 2015 14:07
Scrape a website with GoQuery
func Scrape(address string) (b *bytes.Buffer) {
// Create a new goquery document from the address
doc, err := goquery.NewDocument(address)
if err != nil {
log.Fatal(err)
}
// A buffer for the CSV writer
b := &bytes.Buffer{}
@techjanitor
techjanitor / sitemap.go
Created October 12, 2014 09:16
Generate Google sitemap
package main
import (
"bufio"
"encoding/xml"
"fmt"
"os"
)
func main() {
@techjanitor
techjanitor / cloudflare.go
Last active August 29, 2015 14:12
Ban someone on cloudflare
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
)
@techjanitor
techjanitor / contains.go
Created January 1, 2015 22:26
Case insensitive string comparison
func contains(s []string, e string) bool {
for _, a := range s {
if strings.EqualFold(a, e) {
return true
}
}
return false
}
@techjanitor
techjanitor / extensions.go
Created January 13, 2015 05:21
a way to check file extensions
package main
import (
"fmt"
"strings"
)
func main() {
ext := ".WEBM"
@techjanitor
techjanitor / hmac.go
Created April 18, 2015 03:43
Example of web safe, hmac signed JSON messages
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"time"
)