Created
September 4, 2014 14:38
-
-
Save jahfer/8993cee35b74edfc27b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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