Skip to content

Commit 4b81af5

Browse files
authored
Merge pull request #11 from reidjs/fix-turn-off-queue
Remove queue system, allow different date formats
2 parents 6ac5327 + afd3bd1 commit 4b81af5

File tree

6 files changed

+34
-46
lines changed

6 files changed

+34
-46
lines changed

README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# markdown-tweet-scheduler
2-
Schedule daily tweets from markdown files in your repo, posted to twitter via github actions.
2+
Schedule tweets from markdown files in your repo, posted to twitter via github actions.
3+
34

45
## Why
56
- View, edit, and post your tweets without logging into twitter
67
- Keep source controlled backups of your tweets
78
- Free and open source
89

10+
911
## Setup
10-
1. Fork this repo
11-
2. Get your twitter credentials by creating an app (https://developer.twitter.com/apps)
12+
1. Clone this repo and push it to your own private repo.
13+
2. Get your credentials by creating a twitter app (https://developer.twitter.com/apps)
1214
3. Add your twitter credentials to the repository's secrets (https://docs.github.com/en/actions/reference/encrypted-secrets)
1315
- API_KEY (known as consumer_key in twitter API)
1416
- API_SECRET_KEY (known as consumer_secret in twitter API)
1517
- ACCESS_TOKEN
1618
- ACCESS_TOKEN_SECRET
1719

18-
## Scheduling Tweets
19-
**By Date**.
20-
1. Create a markdown file in the `./tweets/` folder with a future date in `YYYY-Mon-DD` format, for example, `2021-Sep-05.md`, and write the content of your tweet in it.
20+
21+
## Scheduling Tweets By Date
22+
1. Create a markdown file in the `./tweets/` folder with a future date in either the `YYYY-Mon-DD` or `Month dd, YYYY` format, for example, `2021-Sep-05.md` or `September 5, 2021.md`, and write the content of your tweet in it.
2123
2. Commit the file(s) and push to the remote repo. When the daily action runs on the specified date, the tweet should be posted.
2224

2325

@@ -28,15 +30,16 @@ Schedule daily tweets from markdown files in your repo, posted to twitter via gi
2830
2. Changing the tweet directory:
2931
- Change the `FILE_PATH` environment variable in `.github/workflows/go.yml`
3032

33+
3134
## Running locally
3235
1. rename `.env-SAMPLE` to `.env` and fill in your twitter credentials
3336
- consumer_key == API_KEY
3437
- consumer_secret == API_SECRET_KEY
35-
2. In your terminal: `go run main.go`
38+
2. In your terminal, from the root directory: `go run .`
39+
3640

3741
## Notes
3842
1. Fails silently on bad credentials, make sure you set those correctly.
3943
2. Only allows one tweet per day by design. If requested, this can be modified to allow tweets by the minute or hour.
40-
3. I suggest moving posted tweets that have already been posted to a `posted/` subdirectory under `tweets/`.
41-
4. Tweets will not be posted exactly at the cron time set in `go.yml` because of how github actions work. If you need minute precision, run this script locally on a cronjob.
44+
3. Tweets will not be posted exactly at the cron time set in `go.yml`. In my experience in can be 5-10 minutes late. If you need minute precision, run this script locally on a cronjob.
4245

main.go

+19-35
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,26 @@ func ScheduledTweet() (string, string, error) {
8888
formatted_time := current_time.Format("2006-Jan-02")
8989
path := os.Getenv("FILE_PATH")
9090
fmt.Println(path)
91+
9192
todays_file_name := path + formatted_time + ".md"
92-
fmt.Println(todays_file_name)
9393
content, err := ReadFile(todays_file_name)
94-
if err != nil {
95-
return "", "", err
94+
// Fixes issue #3: Allow different date format
95+
formatted_time2 := current_time.Format("January 2, 2006")
96+
todays_file_name2 := path + formatted_time2 + ".md"
97+
content2, err2 := ReadFile(todays_file_name2)
98+
99+
if err == nil {
100+
fmt.Println("Attempting to post content from: ", todays_file_name)
101+
return content, todays_file_name, nil
102+
} else if err2 == nil {
103+
fmt.Println("Attempting to post content from: ", todays_file_name2)
104+
return content2, todays_file_name2, nil
96105
}
97106

98-
return content, todays_file_name, nil
107+
return "", "", errors.New(err.Error() + err2.Error())
99108
}
100109

110+
// TODO: either fix or remove queue system:
101111
func IsQueueNameFormat(filename string) bool {
102112
// if name fits the format q-#, return true
103113
if string(filename[0]) == "q" && string(filename[1]) == "-" {
@@ -106,6 +116,7 @@ func IsQueueNameFormat(filename string) bool {
106116
return false
107117
}
108118

119+
// TODO: either fix or remove queue system:
109120
func QueuedTweet() (string, string, error) {
110121
LoadDotEnv()
111122
path := os.Getenv("FILE_PATH")
@@ -148,37 +159,10 @@ func QueuedTweet() (string, string, error) {
148159

149160
func main() {
150161
scheduled_content, scheduled_tweet_filename, scheduled_tweet_error := ScheduledTweet()
151-
path := os.Getenv("FILE_PATH")
152-
153162
if scheduled_tweet_error != nil {
154-
fmt.Println(scheduled_tweet_error)
155-
// If there's not a scheduled tweet today, then try a queued tweet
156-
queued_content, queued_tweet_filename, queued_tweet_error := QueuedTweet()
157-
if queued_tweet_error != nil {
158-
fmt.Println(queued_tweet_error)
159-
} else {
160-
fmt.Println("Posting", queued_content)
161-
post_failure := Tweet(queued_content)
162-
if post_failure != nil {
163-
// TODO: check if this is correct naming
164-
rename_err := os.Rename(queued_tweet_filename, path+"failed_"+queued_tweet_filename[19:])
165-
fmt.Println(rename_err)
166-
} else {
167-
// TODO
168-
rename_err := os.Rename(queued_tweet_filename, path+"posted_"+queued_tweet_filename[19:])
169-
fmt.Println(rename_err)
170-
}
171-
}
172-
} else {
173-
// try to post a scheduled tweet
174-
post_failure := Tweet(scheduled_content)
175-
if post_failure != nil {
176-
rename_err := os.Rename(scheduled_tweet_filename, path+"failed_"+scheduled_tweet_filename[9:])
177-
fmt.Println(rename_err)
178-
} else {
179-
// fmt.Println("todo: change filename to posted_", scheduled_tweet_filename)
180-
rename_err := os.Rename(scheduled_tweet_filename, path+"posted_"+scheduled_tweet_filename[9:])
181-
fmt.Println(rename_err)
182-
}
163+
fmt.Println("Error scheduling file:", scheduled_tweet_filename)
164+
fmt.Println("Error:", scheduled_tweet_error)
183165
}
166+
post_failure := Tweet(scheduled_content)
167+
fmt.Println("Error posting to Twitter:", post_failure)
184168
}

tweets/August 20, 2021.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is another supported date format (August 20, 2021.md) of posting to twitter.

tweets/draft.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Example of a tweet draft that will not be posted because its name (draft.md) does not match either of the required formats (YYYY-Mon-DD.md)
2+

tweets/foo.md

-1
This file was deleted.

tweets/q-1.md

-1
This file was deleted.

0 commit comments

Comments
 (0)