~rek2/goTootRamdonVideo

8e34443c2a365aac446e200b128764c646a36688 — rek2 3 years ago ed76bd2
commented funtionality and minor cleaning
1 files changed, 27 insertions(+), 2 deletions(-)

M main.go
M main.go => main.go +27 -2
@@ 1,3 1,7 @@
// ReK2 - Hispagatos
// GPLv3 read LICENSE file
// gemini://rek2.hispagatos.org

package main

import (


@@ 26,8 30,10 @@ type theFeed struct {

func main() {

	// get config parameters
	server, clientId, clientSecret, username, password, feedURL := readConfig()

	// daemonize
	cntxt := &daemon.Context{
		PidFileName: "goTootRamdonVideo.pid",
		PidFilePerm: 0644,


@@ 35,7 41,7 @@ func main() {
		LogFilePerm: 0640,
		WorkDir:     "./",
		Umask:       027,
		Args:        []string{"[goTootRamdonVideo]"},
		Args:        []string{"goTootRamdonVideo"},
	}
	d, err := cntxt.Reborn()
	if err != nil {


@@ 49,6 55,7 @@ func main() {
	log.Print("- - - - - - - - - - - - - - -")
	log.Print("daemon started")

	// log into mastodon
	c := mastodon.NewClient(&mastodon.Config{
		Server:       server,
		ClientID:     clientId,


@@ 59,6 66,8 @@ func main() {
	if error != nil {
		log.Fatal(error)
	}

	// run every x times and blocks the program to keep it running
	cron := gocron.NewScheduler(time.UTC)
	cron.Every(5).Minutes().Do(mastodonTask, c, feedURL)
	cron.StartBlocking()


@@ 66,12 75,18 @@ func main() {

func mastodonTask(c *mastodon.Client, feedURL string) {

	// here we do  the heavy orchestration of the app
	// gets feeds
	feeds := parseFeed(feedURL)

	// selects a ramdon item from the feed
	rand.Seed(time.Now().UnixNano())
	randToot := feeds[rand.Intn(len(feeds))]

	// creates the formating of the toot
	theToot := createTheToot(randToot)

	// post the toot
	postToot(c, &theToot)

	log.Printf("Posted video at: %s", time.Now().String())


@@ 80,6 95,7 @@ func mastodonTask(c *mastodon.Client, feedURL string) {

func postToot(c *mastodon.Client, theToot *mastodon.Toot) {

	// PostStatus mastodon api for post a toot
	_, err := c.PostStatus(context.Background(), theToot)
	if err != nil {



@@ 91,8 107,9 @@ func postToot(c *mastodon.Client, theToot *mastodon.Toot) {

func parseFeed(feedURL string) []theFeed {

	// parse a grab all items from the feed
	fp := gofeed.NewParser()
	fp.UserAgent = "ReK2 videoposter 0.1"
	fp.UserAgent = "ReK2 videoposter 1.0"
	feed, _ := fp.ParseURL(feedURL)
	feeds := make([]theFeed, len(feed.Items))



@@ 109,6 126,7 @@ func parseFeed(feedURL string) []theFeed {

func createTheToot(toot theFeed) mastodon.Toot {

	// here we customize and format the toot
	theToot := mastodon.Toot{
		Status: fmt.Sprintf("%s\n%s\n", toot.Title, toot.Link),
	}


@@ 117,6 135,9 @@ func createTheToot(toot theFeed) mastodon.Toot {

func parseCredentials(command string) (string, error) {

	// function to run the external pass/gopass program
	// to get secrets

	if command == "" {
		return "", nil
	}


@@ 132,6 153,10 @@ func parseCredentials(command string) (string, error) {
}

func readConfig() (string, string, string, string, string, string) {

	// read config.ini
	// get all parameters

	cfg, err := ini.Load("config.ini")
	if err != nil {
		fmt.Printf("Fail to read file: %v", err)