aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Dodrill <xena@yolo-swag.com>2015-06-06 09:47:24 -0700
committerChristine Dodrill <xena@yolo-swag.com>2015-06-06 09:47:41 -0700
commit06c09db5f3109ac8fc84ddaeca317c55dea826d1 (patch)
treeb501315b0abf69486bd2f85bb72eae52ffccc313
parentb014abfa03cf1e50db8e0ebee1ed8cd5a7b9ecde (diff)
downloadx-06c09db5f3109ac8fc84ddaeca317c55dea826d1.tar.xz
x-06c09db5f3109ac8fc84ddaeca317c55dea826d1.zip
Add first pass of DB upload tool
-rw-r--r--dbupload/.gitignore1
-rw-r--r--dbupload/db.go49
-rw-r--r--dbupload/main.go122
3 files changed, 172 insertions, 0 deletions
diff --git a/dbupload/.gitignore b/dbupload/.gitignore
new file mode 100644
index 0000000..fa5221a
--- /dev/null
+++ b/dbupload/.gitignore
@@ -0,0 +1 @@
+dbupload
diff --git a/dbupload/db.go b/dbupload/db.go
new file mode 100644
index 0000000..5307d04
--- /dev/null
+++ b/dbupload/db.go
@@ -0,0 +1,49 @@
+package main
+
+type UploadImage struct {
+ Image struct {
+ SourceURL string `json:"source_url"`
+ Tags string `json:"tag_list"`
+ ImageURL string `json:"image_url"`
+ } `json:"image"`
+}
+
+type Image struct {
+ ID string `json:"id"`
+ IDNumber int `json:"id_number"`
+ CreatedAt string `json:"created_at"`
+ UpdatedAt string `json:"updated_at"`
+ DuplicateReports []interface{} `json:"duplicate_reports"`
+ FileName string `json:"file_name"`
+ Description string `json:"description"`
+ Uploader string `json:"uploader"`
+ Image string `json:"image"`
+ Score int `json:"score"`
+ Upvotes int `json:"upvotes"`
+ Downvotes int `json:"downvotes"`
+ Faves int `json:"faves"`
+ CommentCount int `json:"comment_count"`
+ Tags string `json:"tags"`
+ TagIds []string `json:"tag_ids"`
+ Width int `json:"width"`
+ Height int `json:"height"`
+ AspectRatio float64 `json:"aspect_ratio"`
+ OriginalFormat string `json:"original_format"`
+ MimeType string `json:"mime_type"`
+ Sha512Hash string `json:"sha512_hash"`
+ OrigSha512Hash string `json:"orig_sha512_hash"`
+ SourceURL string `json:"source_url"`
+ License string `json:"license"`
+ Representations struct {
+ ThumbTiny string `json:"thumb_tiny"`
+ ThumbSmall string `json:"thumb_small"`
+ Thumb string `json:"thumb"`
+ Small string `json:"small"`
+ Medium string `json:"medium"`
+ Large string `json:"large"`
+ Tall string `json:"tall"`
+ Full string `json:"full"`
+ } `json:"representations"`
+ IsRendered bool `json:"is_rendered"`
+ IsOptimized bool `json:"is_optimized"`
+}
diff --git a/dbupload/main.go b/dbupload/main.go
new file mode 100644
index 0000000..7a6f5b1
--- /dev/null
+++ b/dbupload/main.go
@@ -0,0 +1,122 @@
+package main
+
+import (
+ "bytes"
+ "encoding/json"
+ "flag"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+)
+
+var (
+ apiKeyLocation = flag.String("apikeyloc", "/home/xena/.local/share/within/db.key", "Derpibooru API key location")
+ cookieLocation = flag.String("cookieloc", "/home/xena/.db.cookie", "Location for magic cookie")
+)
+
+func main() {
+ flag.Parse()
+
+ if flag.NArg() == 0 {
+ fmt.Printf("%s: <glob of files to upload>\n", os.Args[0])
+ fmt.Printf("All files must have a manifest json file.\n")
+ flag.Usage()
+ }
+
+ dbkey, err := ioutil.ReadFile(*apiKeyLocation)
+ if err != nil {
+ panic(err)
+ }
+
+ mydbkey := strings.Split(string(dbkey), "\n")[0]
+
+ cookie, err := ioutil.ReadFile(*cookieLocation)
+ if err != nil {
+ panic(err)
+ }
+
+ images, err := filepath.Glob(flag.Arg(0))
+ if err != nil {
+ panic(err)
+ }
+
+ for _, image := range images {
+ if strings.HasSuffix(image, ".json") {
+ log.Printf("Skipped %s...", image)
+ continue
+ }
+
+ metafin, err := os.Open(image + ".json")
+ if err != nil {
+ log.Fatal("image " + image + " MUST have description manifest for derpibooru")
+ }
+ defer metafin.Close()
+
+ metabytes, err := ioutil.ReadAll(metafin)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var meta UploadImage
+ err = json.Unmarshal(metabytes, &meta)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ imfin, err := os.Open(image)
+ if err != nil {
+ log.Fatal("cannot open " + image)
+ }
+ defer imfin.Close()
+
+ if meta.Image.ImageURL == "" {
+ panic("need file uploaded somewhere?")
+ }
+
+ outmetabytes, err := json.Marshal(&meta)
+
+ req, err := http.NewRequest("POST", "https://derpibooru.org/images.json?key="+mydbkey, bytes.NewBuffer(outmetabytes))
+ if err != nil {
+ panic(err)
+ }
+
+ c := &http.Client{}
+
+ req.Header = http.Header{
+ "User-Agent": {"Xena's crappy upload tool"},
+ "Cookie": {string(cookie)},
+ "Content-Type": {"application/json"},
+ }
+
+ resp, err := c.Do(req)
+ if err != nil {
+ log.Printf("%#v", err)
+ fmt.Printf("Request ID: %s\n", resp.Header.Get("X-Request-Id"))
+ continue
+ }
+
+ if resp.StatusCode != 201 {
+ io.Copy(os.Stdout, resp.Body)
+ continue
+ }
+
+ respbytes, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ log.Println(err)
+ continue
+ }
+
+ var i Image
+ json.Unmarshal(respbytes, &i)
+
+ fmt.Printf("Uploaded as https://derpibooru.org/%d\n", i.IDNumber)
+
+ time.Sleep(20 * time.Second)
+ }
+}