Skip to content

Instantly share code, notes, and snippets.

@catatsuy
Created December 2, 2017 08:15
Show Gist options
  • Save catatsuy/a40139c5e38b6c1c0ab6c54447a04659 to your computer and use it in GitHub Desktop.
Save catatsuy/a40139c5e38b6c1c0ab6c54447a04659 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"
)
func main() {
prefix := "/home/catatsuy/hatebu/"
content, err := ioutil.ReadFile(prefix + "dump.rss")
if err != nil {
log.Fatal(err)
}
idByte, err := ioutil.ReadFile(prefix + "id")
if err != nil {
log.Fatal(err)
}
id, err := strconv.Atoi(string(idByte))
if err != nil {
log.Fatal(err)
}
if id == 0 {
return
}
type XML struct {
Bookmarks []struct {
Title string `xml:"title"`
Link string `xml:"link"`
Subjects []string `xml:"subject"`
} `xml:"item"`
}
result := XML{}
err = xml.Unmarshal(content, &result)
if err != nil {
log.Fatal(err)
}
type pocket struct {
URL string `json:"url"`
Title string `json:"title"`
Tags string `json:"tags"`
ConsumerKey string `json:"consumer_key"`
AccessToken string `json:"access_token"`
}
po := pocket{
ConsumerKey: "dcba4321-dcba-4321-dcba-4321dc",
AccessToken: "5678defg-5678-defg-5678-defg56",
}
lastID := id
count := 300
for i := id; i >= 0; i-- {
count--
if count == 0 {
break
}
b := result.Bookmarks[i]
fmt.Println(i, b.Link, b.Title)
po.URL = b.Link
po.Title = b.Title
po.Tags = strings.Join(b.Subjects, ",")
by, _ := json.Marshal(po)
res, err := http.Post("https://getpocket.com/v3/add", "application/json;charset=utf-8", bytes.NewBuffer(by))
if err != nil {
log.Println(err)
break
}
if res.StatusCode != http.StatusOK {
log.Println(res.Status)
break
}
lastID = i
time.Sleep(time.Second)
}
ioutil.WriteFile(prefix+"id", []byte(strconv.Itoa(lastID)), 0666)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment