@abcabc the function "download_file" works like a charm! So I have updated my script with it. Thanks a lot.
https://gist.github.com/henryaukc/6fd00b8baee4c069a2b44e574829469f
BTW, can I know why my function can't work in that particular case? I can use it to download the same video from sd_src but not hd_src so I think it may due to memory constraint? And it is ok to download in pythonista environment but not through appex, is it some memory constraint when calling the app extension or some memory leak problems in triggering the scripts through extension? I just wanna learn more about it. Thanks
def download_file(url, tmpfile=None): if not tmpfile: local_filename = url.split('/')[-1] else: local_filename = tmpfile with open(local_filename, 'wb') as f: r = requests.get(url, stream=True) total_length = r.headers.get('content-length') if not total_length: print("Content mode...") f.write(r.content) else: print("Chunk mode...") # the below is used when getting that link dl = 0 total_length = float(total_length) for chunk in r.iter_content(1024): dl += len(chunk) f.write(chunk) return local_filename