Skip to content

Commit

Permalink
Merge pull request #2 from danog/master
Browse files Browse the repository at this point in the history
Major changes
  • Loading branch information
Zahaib committed Jul 19, 2015
2 parents 5e404df + daa57e9 commit f519256
Showing 1 changed file with 92 additions and 17 deletions.
109 changes: 92 additions & 17 deletions dmUpload.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,65 @@
#!/bin/bash
APIKEY=""
APISECRET=""
USERNAME=""
PASSWORD=""

[ $# -lt 11 -o $# -lt 10 -o "$1" = "--help" ] && { echo "Usage: $(basename $0) -u username -p password -k api_key -s api_secret -c category [ -t \"title\" ] [ -l language ] video.mp4 [ tag,another tag ]
Options:
--help Show this extremely helpful message.
--multiple Upload multiple videos
To upload more than one video, run the program using the following syntax:
$(basename $0) -u username -p password -k api_key -s api_secret -l language --multiple list1.txt list2.txt
The text files must contain the following line for every video:
\"title\" [ tag,another tag ] video.mp4 category
If no title is specified, the filename (without extension) is used as title.
Default language is english (en)."; exit 1; }

lineclear() { echo -en "\r\033[K"; }
SCOPE="read+write"
FILE="/home/foo/bar.mp4"
TAGS="foo,bar"
#TITLE=$(basename "$FILE")
#TITLE="${TITLE%.*}" #<--- Same title as file's name
TITLE="foobar"
echo "$*" | grep -q "\--multiple" && multi=y

#declare -a categories=('videogames' 'music' 'fun' 'shortfilms' 'news' 'sport' 'auto' 'animals' 'people' 'webcam' 'creation' 'school' 'lifestyle' 'tech' 'travel' 'tv');
#random=$((RANDOM%${#categories[@]}-1))
#CATEGORY=${categories[$random]} #<--- Randomly select a category
CATEGORY="videogames"
while getopts :u:p:k:s:t:c:l: FLAG; do
case "$FLAG" in
k)
APIKEY="$OPTARG"
;;
s)
APISECRET="$OPTARG"
;;
u)
USERNAME="$OPTARG"
;;
p)
PASSWORD="$OPTARG"
;;
t)
[ "$multi" = "" ] && TITLE="$OPTARG"
;;
c)
[ "$multi" = "" ] && CATEGORY="$OPTARG"
;;
l)
LANGUAGE="$OPTARG"
;;
esac
done
shift $((OPTIND-1))

#declare -a languages=('en' 'cn' 'ja' 'ru' 'tr' 'pt' 'fa' 'ko' 'it' 'da' 'ur' 'vi' 'pl' 'lv' 'id' 'he' 'fr');
#random2=$((RANDOM%${#languages[@]}-1))
#LANGUAGE=${languages[$random2]} #<--- Randomly select a language
LANGUAGE="en"

ul() {
FILE=$(realpath $FILE)
echo -n "Getting access token..."

curl -s --output out.txt --data 'grant_type=password&client_id='$APIKEY'&client_secret='$APISECRET'&username='$USERNAME'&password='$PASSWORD'&scope='$SCOPE'' https://api.dailymotion.com/oauth/token


var1=$(grep "access_token" out.txt | cut -d: --complement -f1)

acc_token=$(echo $var1 | cut -d, -f1 | cut -d\" --complement -f1 | cut -d\" -f1)
lineclear
echo -n "Getting authorization..."

curl -s --output out.txt --header "Authorization: Bearer $acc_token" \
"https://api.dailymotion.com/file/upload"
Expand All @@ -33,11 +68,15 @@ curl -s --output out.txt -i https://api.dailymotion.com/file/upload?access_token

upload_url=$(grep "upload_url" out.txt | cut -d: --complement -f1 | cut -d\" --complement -f1 | cut -d\" -f1 | sed 's/\\//g')

lineclear
echo -n "Uploading video..."
curl -s --output out.txt --request POST \
--form 'file=@'"$FILE" \
"$upload_url"
video_url=$(grep "url" out.txt | cut -d: --complement -f1-10 | cut -d\" --complement -f1 | cut -d\" -f1 | sed 's/\\//g')

lineclear
echo "Publishing video..."
curl -s --output out.txt --request POST \
--header "Authorization: Bearer $acc_token" \
--form 'url='"$video_url" \
Expand All @@ -55,3 +94,39 @@ then
else
echo "Video uploaded with id $video_id"
fi
}


[ "$LANGUAGE" = "" ] && LANGUAGE=en
shift

[ "$multi" = "y" ] && {
set +u
queue=$(cat $*)
until [ "$queue" = "" ];do
video="$(echo "$queue" | sed -n '/^\s*$/!{p;q}')"
queue="$(echo "$queue" | sed '$ d')"
TITLE="$(echo "$video" | awk -F\" '{print $NF}')"
FILE="$(echo "$video" | awk '{print $(NF-1)}')"
TAGS="$(echo "$video" | sed "s/.*\"//;s/$FILE.*//")"
CATEGORY="$(echo "$video" | awk '{ print $NF }')"
ul
done
} || {
FILE="$1"
TAGS="$*"
[ "$TITLE" = "" ] && TITLE="$(basename $1)" && TITLE="${TITLE%.*}"
ul
}


#TITLE=$(basename "$FILE")
#TITLE="${TITLE%.*}"
#<--- Same title as file's name
#declare -a categories=('videogames' 'music' 'fun' 'shortfilms' 'news' 'sport' 'auto' 'animals' 'people' 'webcam' 'creation' 'school' 'lifestyle' 'tech' 'travel' 'tv');
#random=$((RANDOM%${#categories[@]}-1))
#CATEGORY=${categories[$random]} #<--- Randomly select a category
#declare -a languages=('en' 'cn' 'ja' 'ru' 'tr' 'pt' 'fa' 'ko' 'it' 'da' 'ur' 'vi' 'pl' 'lv' 'id' 'he' 'fr');
#random2=$((RANDOM%${#languages[@]}-1))
#LANGUAGE=${languages[$random2]}
#<--- Randomly select a language

0 comments on commit f519256

Please sign in to comment.