From d73c9f9d1f4812f97d1f0362b5954e723c7dfff6 Mon Sep 17 00:00:00 2001 From: denven Date: Wed, 25 Jan 2023 00:26:15 -0800 Subject: [PATCH] Cancel token refresh subshell process, renew the tokens in main shell process --- README.md | 18 +++++++++--------- init.sh | 2 ++ scripts/oauth2.sh | 20 +++++++++++++++----- scripts/upload.sh | 7 ++++--- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2db50db..807be7b 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # OneDrive-uploader for Yi IP Cameras ### To visitors and cloners: -- A star 🌟 is the best encouragement to me if this repository helps you - Any issue 🐛 is welcome if you find a bug or have a question to ask - Test feedback on your camera is invaluable to all users at anytime -- Please note that OneDrive resumable API for uploading file larger than 4MB is not reliable for my case, see [issue](https://github.com/denven/yihack-onedrive-uploader/issues/6). However, YMMV. +- Please note that OneDrive resumable API for uploading file larger than 4MB is not very reliable for my case, see [issue](https://github.com/denven/yihack-onedrive-uploader/issues/6). However, YMMV. +- A star 🌟 from you is the best encouragement to me if you think this repository helps you. This repository is inspired by [roleoroleo](https://github.com/roleoroleo)'s [yi-hack-MStar.gdrive uploader](https://github.com/roleoroleo/yi-hack-MStar.gdrive). The gdrive uploader does provide some convenience, however, my Google drive has only 15GB space shared with Gmail and other account applications, and it is very easy to get storage fully packed with uploaded media files. Fortunately, I've subscribed Microsoft 365 Developer Program which provides 5TB storage space. I think uploading files to OneDrive is a better option for me. @@ -17,6 +17,7 @@ If you have a subscription of Microsoft OneDrive Stroage or Microsoft 365 Develo - unattended upload your video (.mp4) and image (.jpg) files once set up successfully - both personal and tenant Microsoft accounts are supported - safe auto-clean of your earliest files when storage reaches the specified threshold +- Re-transmission control is used to assure files upload reliability ## Supported camera models > Yi cameras hacked with the same file hiearacy or builtin applications are more likely to be supported. Anyway, your own separate tests are necessary before you use it. I've tested on my own camera. @@ -111,17 +112,16 @@ cd /tmp/sd/yi-hack/onedrive ./stop.sh ``` - You might have messed up the configuration or you are not content with the running situation, you want to start again from the beginning. - - first, stop the uploader first by following above - - delete all generated data (or keep file `data/last_upload.json` if you want the uploader to resume your uploading without any repetitive uploads). + - first, stop the uploader first by following above; + - delete all generated data (or keep file `data/last_upload.json` if you want the uploader to resume your uploading without any repetitive uploads); + - run `./init.sh` again to get your access tokens again. ```bash rm -rf data -rm -rm log +rm -rf log +./init.sh ``` - If you want to setup with another OneDrive account (or Azure application data), you can delete or edit your current `config.json`, or replace it with a new one as well. - Once you have deleted `data` directory or changed application credentials in `config.json` files, you must run `init.sh` script manually to setup or reload your uploader again. ```bash ./init.sh -``` - -## Todo list -- [x] Auto-clean the oldest uploaded folders before the drive space is exhausted \ No newline at end of file +``` \ No newline at end of file diff --git a/init.sh b/init.sh index f3ee8ee..a5b8a63 100644 --- a/init.sh +++ b/init.sh @@ -12,6 +12,8 @@ init_globals() { query=''; resp=''; error=''; video_root_folder='' upload_video_only=true # upload mp4 files only auto_clean_threshold=100 # disable the auto-clean feature + + app_token_timer=$(date +%s) # global timer used for token check DRIVE_BASE_URI="https://graph.microsoft.com/v1.0/me/drive" SD_RECORD_ROOT="/tmp/sd/record" diff --git a/scripts/oauth2.sh b/scripts/oauth2.sh index 1ac9849..f8a02b5 100644 --- a/scripts/oauth2.sh +++ b/scripts/oauth2.sh @@ -66,9 +66,9 @@ redeem_oauth2_tokens() { fi } -# periodically refresh tokens in subshell process in case of expiry by default +# periodically refresh tokens in subshell process in case of expiry by default (this way is abandoned) # the execution time interval is 30 minutes (the lifetime of access token is 60-75 minutes) -# param: not required, if $1="--test" or "-onetime", it refresh access token once only +# param: not required, if $1="--test" or "--onetime", it refresh access token once only refresh_oauth2_tokens() { local error=''; local resp='' while [ 1 ] ; do @@ -89,7 +89,8 @@ refresh_oauth2_tokens() { error=$(echo ${resp} | jq --raw-output '.error.message') if [ ! -z $access_token ]; then echo ${resp} | jq '.' > ./data/token.json - color_print "GREEN" "Refresh API tokens successfully, your token is still valid." + color_print "GREEN" "Refresh API tokens successfully, your token is still valid." + write_log "Refresh API tokens successfully, your token is still valid." elif [ ! -z ${error} ]; then write_log ${error} break @@ -131,8 +132,8 @@ manage_oauth2_tokens() { if [ ${need_re_assign} = true ]; then get_oauth2_auth_code redeem_oauth2_tokens - else - ( refresh_oauth2_tokens ) & # update tokens in subshell + #else + # ( refresh_oauth2_tokens ) & # update tokens in subshell fi } @@ -145,4 +146,13 @@ oauth2_read_tokens() { else refresh_oauth2_tokens "--onetime" fi +} + +# param: $1=minutes +refresh_token_by_minutes() { + local eclipsed_mins=$(get_elipsed_minutes ${app_token_timer}) + if [ ${eclipsed_mins} -ge $1 ]; then + refresh_oauth2_tokens "--onetime" + app_token_timer=$(date +%s) + fi } \ No newline at end of file diff --git a/scripts/upload.sh b/scripts/upload.sh index aedde0e..fdc10a1 100644 --- a/scripts/upload.sh +++ b/scripts/upload.sh @@ -27,13 +27,14 @@ manage_video_uploads() { camera_idled=$(check_camera_idle_status) fi + refresh_token_by_minutes 30 # when the token has been refreshed, update it in this process # upload file, check and do auto-clean if [ "${idle_transfer_mode}" != true ] || [ ${camera_idled} = true ]; then local file=$(get_one_file_to_upload) if [ ! -z "${file}" ] && [ -f ${file} ]; then echo "Start to upload ${file}" upload_one_file ${file} - process_log_file + process_log_file # sleep 2 # send file every 2s else echo "All files were uploaded, wait for a new recorded video or image file." @@ -100,7 +101,7 @@ remove_earliest_folder() { elif [ ${#item_key} -gt 14 ]; then delete_drive_item ${item_key} if [ -z "${error}" ] || [ "${error}" = "null" ]; then - write_log "Deleted folder ${item_key}" + # write_log "Deleted folder ${item_key}" echo `date +"%F %H:%M:%S"`": /${video_root_folder}/${folder_to_delete}" >> ./log/deletion.history fi else @@ -181,7 +182,7 @@ build_media_file_index() { if [ ! -d ${file_parent} ]; then local last_uploaded_file_ts=$(get_file_created_timestamp $1) local current_time_ts=$(date +%s) - local eclipsed_mins=$(((${current_time_ts}-${last_uploaded_file_ts})/60)) + # local eclipsed_mins=$(((${current_time_ts}-${last_uploaded_file_ts})/60)) local eclipsed_mins=$(get_elipsed_minutes ${last_uploaded_file_ts}) # find a newer files to build index (if no files found, the files.index will be empty)