Forum Archive

Play Video on iOS Quick Look?

henryaukc

Hi All,

I am very new to python and to Pythonista for iOS. I would like to ask if it is possible to write an extension script which parse the incoming URL(eg. From Safari share menu) and return a video opening in "Quick look" or sending the file through the "open in..." menu?

Thanks a lot

omz

That's certainly possible, have a look at console.open_in or console.quicklook.

Depending on where you get the URL from, you might need to download it to a local file first (open_in and quicklook take local file paths, not web URLs as arguments). There are various ways to do so, a simple one would be urllib.urlretrieve (Python 2) or urllib.request.urlretrieve (Python 3).

henryaukc

Thank a lot! Really grateful to have Pythonista, a powerful development platform on my only computer - iPad. Keep up the good work.

abcabc

Here is some code that can help you to play videos using quicklook.

# coding: utf-8
import appex
import console, clipboard
import requests

def download_file(url):
    local_filename = url.split('/')[-1]
    if not local_filename:
        local_filename = 'tmpfile.dat'
    with open(local_filename, 'wb') as f:
        r = requests.get(url, stream=True)
        total_length = r.headers.get('content-length')
        if not total_length:
            f.write(r.content)
        else:
            dl = 0
            total_length = float(total_length)
            for chunk in r.iter_content(1024):
                dl += len(chunk)
                f.write(chunk)
    return local_filename

def main():
    if not appex.is_running_extension():
        print('This script is intended to be run from the sharing extension.')
        return
    url = appex.get_url()
    if not url:
        url = clipboard.get()
    console.hud_alert('url: ' + url)
    local_filename = download_file(url)
    console.hud_alert('copying to ' + local_filename)
    console.quicklook(local_filename)

if __name__ == '__main__':
    main()
henryaukc

Thanks a lot! This is a really helpful and encouraging community for newbie like me.

I have finished my first little project (a facebook video download extension) but encounter a bug when downloading from a particular URL... but it works well in all others. I try to copy that URL to the iCab app to download it and it can be downloaded successfully.

Also, if I use another script to download that URL, it works fine too. Very weird.

My facebook video download extension script(I put it on a gist):
https://gist.github.com/henryaukc/6fd00b8baee4c069a2b44e574829469f

The below is working fine(The URL is captured by the above script):

# coding: utf-8
import webbrowser, os, requests, re, bs4
import appex
import console
import dialogs

data_dir = os.path.join(os.path.abspath('.'),'Videos')

if not os.path.isdir (data_dir):
    #print(data_dir)
    os.makedirs(data_dir)

os.chdir(data_dir)
#ßprint(os.path.abspath('.'))


def DownloadFile(url, filename):
    res = requests.get(url)

    try:
        res.raise_for_status()
    except Exception as exc:
        print('There was a problem: %s' % (exc))

    with open(filename, "wb") as code:
        code.write(res.content)

# the URL below is copy from the console of the above script, I don't know why it quits without saving the file after download but it works in this script 

url = 'https://video-hkg3-1.xx.fbcdn.net/v/t42.9040-2/10000000_1416830291949591_1471786757199495168_n.mp4?efg=eyJybHIiOjMzNjEsInJsYSI6NDA5NiwidmVuY29kZV90YWciOiJzdmVfaGQifQ%5Cu00253D%5Cu00253D&rl=3361&vabr=2241&oh=a820c423f12ecc6472d83bd9e9d8432b&oe=581ABB82'

DownloadFile(url, 'temp.mp4')
console.quicklook('temp.mp4')
os.remove('temp.mp4')


henryaukc

@abcabc Thanks a lot for your help. I try to use yours download function to replace mine but I got this:

Screenshot

jmv38

you can embed photos with
blabla

abcabc
  1. I have modified your gist code with my download function and it seems to work fine.
    https://gist.github.com/29ca31b52689d9c0ce99781097a7fe1b

  2. My script works if you have URL of the form http://.../file name.extension It will work not only for .mp4 extension but also for extensions like .png, .jpg, .py etc. But it does not do processing like your script and it is just a simple example script.

  3. Anyway it does not solve your problem. We may need more information on how to reproduce your problem. May be try to debug your script by directly running from pythonista application (after suitable modification) rather than with share script (appex extension) .

henryaukc

@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