panda

package module
v0.0.0-...-c6ff308 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: MIT Imports: 26 Imported by: 0

README

Go Panda

Go package that scrapes the interwebs for images of pandas and emails them and stores in a database for future fetching. If you're here to see today's Panda, the website is https://daily-panda.herokuapp.com.

The other repos are:

How to use

  • Run go get github.com/opeonikute/panda

  • To fetch a panda image, save it to your cloudinary and mongo database:

    import "github.com/opeonikute/panda"

	goPanda := panda.GoPanda{
		Config: panda.Settings{
			MgDomain:       os.Getenv("MG_DOMAIN"),
			MgKey:          os.Getenv("MG_API_KEY"),
			MailRecipients: os.Getenv("MAIL_RECIPIENT"),
			CdCloudName:    os.Getenv("CD_CLOUD_NAME"),
			CdUploadPreset: os.Getenv("CD_UPLOAD_PRESET"),
			MongoURL:       os.Getenv("MONGO_URL"),
			MongoDB:        os.Getenv("MONGO_DATABASE"),
		},
	}
	res, err = goPanda.Run(0)

    if err != nil {
		log.Fatal(err)
	}

    fmt.Println(res)
  • To get today's panda (from your Mongo Database0)
    import "github.com/opeonikute/panda"
    
    goPanda := panda.GoPanda{
		Config: panda.Settings{
			MongoURL: os.Getenv("MONGO_URL"),
			MongoDB:  os.Getenv("MONGO_DATABASE"),
		},
	}

	tm := time.Now()
	res, err := goPanda.GetPOD(tm)

	if err != nil {
		log.Fatal(err)
	}

    fmt.Println(res)
Using Docker Compose

The compose file is used to start a local Mongo container.

  • Run docker-compose up -d
Environment Variables
  • MG_DOMAIN - Your Mailgun domain.
  • MG_API_KEY - Your Mailgun private API key. Do not commit this to source control.
  • MAIL_RECIPIENT - The email you want the pictures sent to.
  • CD_CLOUD_NAME - Cloudflare cloud name
  • CD_UPLOAD_PRESET - Cloudflare upload preset (unsigned uploads)
  • MONGO_URL - Mongo connection string
  • MONGO_DATABASE - Mongo database name

Tests

MG_DOMAIN=xxxxxx.opeonikute.dev MG_API_KEY=xxxxxxxxxx MAIL_RECIPIENT=test@yahoo.com CD_UPLOAD_PRESET=panda_images  CD_CLOUD_NAME=xxxxxxxxx MONGO_URL=mongodb://opeo:root@localhost SEND_EMAIL=false MONGO_DATABASE=go-panda bash -c "go test"

TODO

  • Config to turn off sending email etc
  • Create IAM Role, lambda function, event service rule with terraform.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPandaNotFound = errors.New("no panda was found")

ErrPandaNotFound is thrown when we don't get a panda of the day for a specific date

Functions

func GetDate

func GetDate(tm time.Time) time.Time

GetDate converts a time to 12am so it becomes a date

Types

type Entry

type Entry struct {
	ID           primitive.ObjectID `json:"_id" bson:"_id"`
	URL          string             `json:"url" bson:"url"`
	Source       string             `json:"source" bson:"source"`
	FileName     string             `json:"filename" bson:"filename"`
	WordOfTheDay string             `json:"wordOfTheDay" bson:"wordOfTheDay"`
	Date         time.Time          `json:"date" bson:"date"`
	Created      time.Time          `json:"created" bson:"created"`
	Updated      time.Time          `json:"updated" bson:"updated"`
}

Entry ...

type GoPanda

type GoPanda struct {
	Config     Settings
	DB         db
	SourceSite string
}

GoPanda ...

func (*GoPanda) CreateGif

func (g *GoPanda) CreateGif() (string, error)

CreateGif makes a gif on the Cloudinary account with all images with the tag 'panda_image'

func (*GoPanda) GetPOD

func (g *GoPanda) GetPOD(tm time.Time) (Entry, error)

GetPOD fetches the panda of a specific day. tm - allows you specify the specific date so you can call time.Now() and pass that in.

func (*GoPanda) Run

func (g *GoPanda) Run(retryCount int) bool

Run exposes the main functionality of the package

type Settings

type Settings struct {
	MgKey          string
	MgDomain       string
	MailRecipients string
	EmailSender    string
	CdCloudName    string
	CdUploadPreset string
	CdAPIKey       string
	CdAPISecret    string
	MongoURL       string
	MongoDB        string
	SendMail       bool
}

Settings exposes all package settings

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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