Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"encoding/json"
"io/ioutil"
"flag"
"strings"
"fmt"
)
type Gif struct {
Url string `json:"url"`
Keywords string `json:"keywords"`
}
type GifwitLibrary struct {
Images []Gif `json:"images"`
}
func (lib *GifwitLibrary) AddGif(image Gif) {
lib.Images = append(lib.Images, image)
return
}
var rejectedTags []string = []string{"on", "at", "with", "is", "gif", "this", "so", "it"}
func main() {
var dir = flag.String("dir", "./", "the directory of your gifs")
var id = flag.String("id", "0", "the ID of your public Dropbox folder")
flag.Parse()
if *id == "0" {
fmt.Println("You must specify your Dropbox ID using the -id flag")
return
}
lib := GifwitLibrary{}
files, _ := ioutil.ReadDir(*dir)
for _, f := range files {
if strings.HasPrefix(f.Name(), ".") {
continue
}
name := strings.Split(f.Name(), ".")[0]
tags := strings.Split(name, "-")
url := strings.Join([]string{"http://dl.dropboxusercontent.com/u/", *id, "/memes/", f.Name()}, "")
cleanedTags := []string{}
for _, tag := range tags {
if !stringInSlice(tag, rejectedTags) {
cleanedTags = append(cleanedTags, tag)
}
}
image := Gif{url, strings.ToLower(strings.Join(cleanedTags, " "))}
lib.AddGif(image)
}
encoded, _ := json.Marshal(lib)
err := ioutil.WriteFile("library.gifwit", encoded, 0644)
if err != nil {
panic(err)
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.