Skip to content

Commit

Permalink
Cancel token refresh subshell process, renew the tokens in main shell…
Browse files Browse the repository at this point in the history
… process
  • Loading branch information
denven committed Jan 25, 2023
1 parent a0e82b1 commit d73c9f9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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.
Expand Down Expand Up @@ -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
```
2 changes: 2 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 15 additions & 5 deletions scripts/oauth2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
7 changes: 4 additions & 3 deletions scripts/upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d73c9f9

Please sign in to comment.