-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadToTwitter.sh
executable file
·71 lines (55 loc) · 1.6 KB
/
uploadToTwitter.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
###
### uploadToTwitter.sh
###
### Andrew Su 2018-12-21
###
### USAGE: ./uploadToTwitter.sh <file to upload>
### READ CONFIGURATION FILE
CONFIG_FILE="config.txt"
if [ ! -f $CONFIG_FILE ]; then
echo "Configuration file not found! Exiting..."
exit 1
fi
source $CONFIG_FILE
# Initialize parameters
message=""
# read in command-line options
while getopts ":m:f:" opt; do
case $opt in
m) message="$OPTARG"
;;
f) file="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
echo "`date`: Uploading to twitter" >> $LOG_FILE
MEDIA_FILE=$file
echo "FILE: $MEDIA_FILE"
# make sure MEDIA_FILE exists
if [ ! -f $MEDIA_FILE ]; then
echo "Media file not found! Exiting..."
exit 1
fi
# get file size
FILE_SIZE=$(stat -c%s "$MEDIA_FILE")
echo "$MEDIA_FILE // $FILE_SIZE"
# INIT
INIT_RESPONSE=`/usr/local/bin/twurl -H upload.twitter.com "/1.1/media/upload.json" -d "command=INIT&media_type=video/mp4&total_bytes=$FILE_SIZE" `
echo "INIT_RESPONSE: $INIT_RESPONSE"
MEDIA_ID=`echo $INIT_RESPONSE | jq -r '.media_id_string'`
echo "MEDIA_ID: $MEDIA_ID"
# APPEND
cmd="/usr/local/bin/twurl -H upload.twitter.com '/1.1/media/upload.json' -d \"command=APPEND&media_id=${MEDIA_ID}&segment_index=0\" --file ${MEDIA_FILE} --file-field 'media'"
echo "Uploading... $cmd"
eval $cmd
# FINALIZE
cmd="/usr/local/bin/twurl -H upload.twitter.com '/1.1/media/upload.json' -d \"command=FINALIZE&media_id=${MEDIA_ID}\""
echo "Finalizing: $cmd"
eval $cmd
# POST
cmd="/usr/local/bin/twurl -d \"status=$message&media_ids=${MEDIA_ID}\" /1.1/statuses/update.json"
echo "Posting: $cmd"
eval $cmd