示例#1
0
func main() {
	godotenv.Load("go.env")

	doc, err := goquery.NewDocument(popular)
	if err != nil {
		panic(err)
	}

	var arr []string
	doc.Find("#a-index article").Each(func(_ int, s *goquery.Selection) {
		a, _ := s.Attr("data-file-url")
		arr = append(arr, a)
	})

	httpClient := http.DefaultClient
	svc := dropbox4go.New(httpClient, os.Getenv("DB_ACCESS_TOKEN"))
	now := time.Now().UTC()

	for _, img := range arr {
		resp, err := httpClient.Get(fqdn + img)
		defer resp.Body.Close()
		if err != nil {
			panic(err)
		}

		pos := strings.LastIndex(img, "/")
		fileName := img[pos+1:]

		req := dropbox4go.Request{
			File: resp.Body,
			Parameters: dropbox4go.Parameters{
				Path: fmt.Sprintf(
					"/danbooru/popular/%s/%s",
					now.Format("2006-01-02"),
					fileName,
				),
				Mode:           "overwrite",
				AutoRename:     false,
				ClientModified: now.Format(time.RFC3339),
				Mute:           true,
			},
		}
		a, err := svc.Upload(req)
		if err != nil {
			panic(err)
		} else {
			fmt.Println(fmt.Sprintf("file %s has uploaded.", a.Name))
		}
	}
}
示例#2
0
func DropboxUpload(url string) string {
	pos := strings.LastIndex(url, "/")
	fileName := url[pos+1:]
	ext := path.Ext(fileName)

	switch ext {
	case ".jpg", ".jpeg", ".png", ".gif":
		if !strings.Contains(url, "http") {
			return onError(errors.New("incorrect resource."))
		}
	default:
		return onError(errors.New("incorrect extension."))
	}

	token := viper.GetString("dropbox.access_token")

	httpClient := http.DefaultClient
	resp, err := httpClient.Get(url)
	defer resp.Body.Close()
	if err != nil {
		return onError(err)
	}

	svc := dropbox4go.New(httpClient, token)
	req := dropbox4go.Request{
		File: resp.Body,
		Parameters: dropbox4go.Parameters{
			Path:           "/twitter/" + fileName,
			Mode:           "overwrite",
			AutoRename:     false,
			ClientModified: time.Now().UTC().Format(time.RFC3339),
			Mute:           true,
		},
	}

	result, err := svc.Upload(req)

	if err != nil {
		return onError(err)
	} else {
		return fmt.Sprintf("file %s has uploaded. (size: %d bytes)", fileName, result.Size)
	}
}