gphoto

package module
v0.0.0-...-0f769be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2021 License: GPL-3.0 Imports: 19 Imported by: 0

README

GPHOTO

A small lib to upload files to the google photo

Build Status GoDoc

Features

  • Uploads file to google photo account via user's cookies, via user's credential (user, pass).
  • Update upload's progress while a file is uploading.

Getting Started

If you want to login to google automaticaly, you have to install ChromeDriver firstly. See more about ChromeDrive at https://sites.google.com/a/chromium.org/chromedriver/

I'm not ensure the code can work smoothy. Use at your own risk. July 14 2018, This code still can work well.

You can take a look at the test code to get some example.

Install

go get -u github.com/canhlinh/gphoto

Quick Start

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"

	"github.com/canhlinh/gphoto"
)

func main() {

	cookies := GetCookiesFromJSON("./json.")
	client := gphoto.NewClient(cookies...)

	photo, err := client.Upload("../sample_data/sample.mp4", "sample.mp4", "AnyAlbumName", progressHandler)
	if err != nil {
		panic(err)
	}
	fmt.Println(photo)
}

// GetCookiesFromJSON parse cookies from a JSON file
// The JSON file can be exported by this extension https://chrome.google.com/webstore/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg
func GetCookiesFromJSON(path string) []*http.Cookie {
	file, err := os.Open(path)
	if err != nil {
		panic(err)
	}

	var cookies []*http.Cookie
	json.NewDecoder(file).Decode(&cookies)
	return cookies
}

func progressHandler(current int64, total int64) {
	fmt.Printf("current %d , total %d ", current, total)
}

Run test

Exports your google photo into an variable GPHOTO_COOKIES_BASE64. Run test:

go test -v -race

Documentation

Index

Constants

View Source
const (
	// ChromeUserAgent user-agent of chrome browser
	ChromeUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"

	// GooglePhotoURL the google photo homepage
	GooglePhotoURL = "https://photos.google.com"

	// GooglePhotoRequestUploadURL url to request create a new upload session
	GooglePhotoRequestUploadURL = "https://photos.google.com/_/upload/uploadmedia/rupio/interactive?authuser=0"

	// GooglePhotoMutateQueryURL url to execute a specific command
	GooglePhotoMutateQueryURL = "https://photos.google.com/_/PhotosUi/mutate"

	// GooglePhotoDataQueryURL url do something
	GooglePhotoDataQueryURL = "https://photos.google.com/_/PhotosUi/data"

	// GoogleCommandDataURL
	GoogleCommandDataURL = "" /* 164-byte string literal not displayed */

	// GoogleLoginSite the url to login
	GoogleLoginSite = "https://accounts.google.com/ServiceLogin"
	// DefaultAlbum a required album need to do a magic thing
	DefaultAlbum = "DefaultAlbum"
)
View Source
const (
	QueryNumberEnableImage           = 137530650
	QueryNumberGetAlbum              = 72930366
	QueryNumberCreateAlbum           = 79956622
	QueryNumberAddPhotoToAlbum       = 79956622
	QueryNumberAddPhotoToSharedAlbum = 99484733
	QueryNumberRemovePhotoFromAlbum  = 85381832
	QueryStringAddPhotoToAlbum       = "C2V01c"
)

Variables

View Source
var (
	//ErrorUnknow For unexpected error
	ErrorUnknow = errors.New("Unknow Error")

	// ErrorAlbumNotCreatedYet In case no album was created just return it
	ErrorAlbumNotCreatedYet = errors.New("There is no album was created")
)
View Source
var (
	HomePageURL, _ = url.Parse(GooglePhotoURL)
)

Functions

func BodyToBytes

func BodyToBytes(body io.Reader) []byte

func BodyToString

func BodyToString(body io.Reader) string

func DumpRequest

func DumpRequest(request *http.Request)

func DumpResponse

func DumpResponse(response *http.Response)

func JsonBodyByScanLine

func JsonBodyByScanLine(s string, start, end int) string

func NewDataQuery

func NewDataQuery(queryNumber int, query interface{}) string

func NewJSONBody

func NewJSONBody(model interface{}) io.Reader

NewJSONBody create a new json request body from an interface

func NewJSONString

func NewJSONString(model interface{}) string

func NewMutateQuery

func NewMutateQuery(queryNumber int, query interface{}) string

func SpritMagicToken

func SpritMagicToken(t string) []string

func UnixMiliSeconds

func UnixMiliSeconds() int64

func WriteStringToFile

func WriteStringToFile(s string)

Types

type Album

type Album struct {
	ID   string
	Name string
}

type AlbumlResponse

type AlbumlResponse struct {
	// contains filtered or unexported fields
}

func NewAlbumlResponse

func NewAlbumlResponse(s string) *AlbumlResponse

func (*AlbumlResponse) Albums

func (al *AlbumlResponse) Albums() (albums []*Album, err error)

type Albums

type Albums []*Album

func (Albums) Get

func (albums Albums) Get(name string) *Album

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client present a upload client

func NewClient

func NewClient(cookies ...*http.Cookie) *Client

NewClient init a Client by existing cookies.

func (*Client) AddPhotoToAlbum

func (c *Client) AddPhotoToAlbum(albumID, photoID string) error

AddPhotoToAlbum adds a photo to an album

func (*Client) CreateAlbum

func (c *Client) CreateAlbum(albumName string) (*Album, error)

CreateAlbum creates a new album

func (*Client) DoQuery

func (client *Client) DoQuery(endpoint string, query string) (io.ReadCloser, error)

DoQuery executes http request

func (*Client) ExportCookies

func (c *Client) ExportCookies() string

func (*Client) GetAlbums

func (client *Client) GetAlbums() (Albums, error)

GetAlbums gets all google photo albums

func (*Client) GetSharedAlbumKey

func (c *Client) GetSharedAlbumKey(albumID string) string

GetSharedAlbumKey gets an album's share key

func (*Client) Login

func (c *Client) Login(user, pass string) error

Login login to google photo with your authentication info.

func (*Client) RemoveFromAlbum

func (c *Client) RemoveFromAlbum(photoID string) error

RemoveFromAlbum Remove a photo from an album

func (*Client) SearchOrCreteaAlbum

func (c *Client) SearchOrCreteaAlbum(name string) (*Album, error)

SearchOrCreteaAlbum creates an album if the album name doesn't exist

func (*Client) SetCookies

func (c *Client) SetCookies(cookies ...*http.Cookie) *Client

SetCookies attach google's cookies to the upload client

func (*Client) SetHTTPClient

func (c *Client) SetHTTPClient(hClient *http.Client) *Client

SetHTTPClient specific the http client to the upload client.

func (*Client) Upload

func (c *Client) Upload(filePath string, filename string, album string, progressHandler ProgressHandler) (*Photo, error)

Upload uploads the file to the google photo. We will recive an url that people can access to the uploaded file directly.

type CopyBufferResult

type CopyBufferResult struct {
	Err     error
	Written int64
}

type CreateSessionRequest

type CreateSessionRequest struct {
	Fields []interface{} `json:"fields"`
}

type EnableImageResponse

type EnableImageResponse []interface{}

type ExternalField

type ExternalField struct {
	Field interface{} `json:"external"`
}

type ExternalFieldNewUpload

type ExternalFieldNewUpload struct {
	Name     string   `json:"name"`
	FileName string   `json:"filename"`
	Put      struct{} `json:"put"`
	Size     int64    `json:"size"`
}

type ExternalFieldTransfer

type ExternalFieldTransfer struct {
	Name    string `json:"name"`
	Status  string `json:"status"`
	PutInfo struct {
		URL string `json:"url"`
	} `json:"putInfo"`
}

type GoogleRupioAdditionalInfo

type GoogleRupioAdditionalInfo struct {
	CompletionInfo struct {
		CustomerSpecificInfo struct {
			UploadToken string `json:"upload_token_base64"`
		} `json:"customerSpecificInfo"`
	} `json:"completionInfo"`
}

type InlinedField

type InlinedField struct {
	Inlined InlinedFieldObject `json:"inlined"`
}

type InlinedFieldObject

type InlinedFieldObject struct {
	Name        string `json:"name"`
	Content     string `json:"contentType"`
	ContentType string `json:"contentType"`
}

type MagicToken

type MagicToken struct {
	Token string `json:"SNlM0e"`
}

type Photo

type Photo struct {
	ID      string
	AlbumID string
	Name    string
	URL     string
}

type ProgressHandler

type ProgressHandler func(current int64, total int64)

type SessionRequest

type SessionRequest struct {
	ProtocolVersion      string               `json:"protocolVersion"`
	CreateSessionRequest CreateSessionRequest `json:"createSessionRequest"`
}

func NewUploadSessionRequest

func NewUploadSessionRequest(fileName string, fileSize int64) *SessionRequest

type SessionStatus

type SessionStatus struct {
	State                  string                   `json:"state"`
	ExternalFieldTransfers []*ExternalFieldTransfer `json:"externalFieldTransfers"`
	UploadID               string                   `json:"upload_id"`
	DropZoneLabel          string                   `json:"drop_zone_label"`
	AdditionalInfo         struct {
		GoogleRupioAdditionalInfo GoogleRupioAdditionalInfo `json:"uploader_service.GoogleRupioAdditionalInfo"`
	} `json:"additionalInfo"`
}

type SessionUpload

type SessionUpload struct {
	SessionStatus SessionStatus `json:"sessionStatus"`
}

func NewSessionUploadFromJson

func NewSessionUploadFromJson(body string) *SessionUpload

type UploadResult

type UploadResult struct {
	Err  error
	Resp *http.Response
}

type Uploader

type Uploader struct {
	// contains filtered or unexported fields
}

func NewUploader

func NewUploader(c *http.Client) *Uploader

func (*Uploader) Do

func (u *Uploader) Do(url string, file io.Reader, fileSize int64, progressHanlder ProgressHandler) (*http.Response, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL