diff --git a/PythonAndroid/application/app/src/main/assets/pythonforandroid.py b/PythonAndroid/application/app/src/main/assets/pythonforandroid.py index 4a2f503..8a49626 100644 --- a/PythonAndroid/application/app/src/main/assets/pythonforandroid.py +++ b/PythonAndroid/application/app/src/main/assets/pythonforandroid.py @@ -1,2 +1,26 @@ +import youtube_dl + +class SimpleYDL(youtube_dl.YoutubeDL): + + def __init__(self, *args, **kargs): + super(SimpleYDL, self).__init__(*args, **kargs) + self.add_default_info_extractors() + +def get_stream(ytid): + ydl_opts = { + 'nocheckcertificate': True, + 'skip_download': True, + 'cachedir': False, + 'format': 18, + 'prefer_insecure': True, + } + res = SimpleYDL(ydl_opts).extract_info(ytid, process=False, download=False) + data = {item['format_id']: item['url'] for item in res['formats']} + data['title'] = res['title'] + data['info'] = res['description'] + obj = {'duration':res['duration'] } + obj['stream'] = data['18'] + return obj + def add_func(a,b): return a+b \ No newline at end of file diff --git a/PythonAndroid/application/jni/Application.mk b/PythonAndroid/application/jni/Application.mk index 02fa951..ee64638 100644 --- a/PythonAndroid/application/jni/Application.mk +++ b/PythonAndroid/application/jni/Application.mk @@ -1,2 +1,2 @@ APP_ABI := armeabi-v7a -APP_PLATFORM := android-12 \ No newline at end of file +APP_PLATFORM := android-23 \ No newline at end of file diff --git a/PythonAndroid/application/jni/main.c b/PythonAndroid/application/jni/main.c index 6ea5aa3..e2ff1b5 100644 --- a/PythonAndroid/application/jni/main.c +++ b/PythonAndroid/application/jni/main.c @@ -9,13 +9,13 @@ JNIEXPORT jint JNICALL Java_com_example_pythontest_MainActivity_add (JNIEnv *env, jclass js, jint arg0, jint arg1) { //set path and home,pay attention --> python3.5.zip - putenv("PYTHONPATH=/data/data/com.example.pythontest/files/python3.5.zip"); - //putenv("PYTHONHOME=/data/data/com.example.pythontest/files"); + putenv("PYTHONPATH=/data/data/com.example.pythontest/files/python3.5.zip"); + //putenv("PYTHONHOME=/data/data/com.example.pythontest/files"); Py_Initialize(); if ( !Py_IsInitialized()){ - __android_log_print(ANDROID_LOG_INFO, "JNIEnv","Py_Initialize failed!!\n"); + __android_log_print(ANDROID_LOG_INFO, "JNIEnv","Py_Initialize failed!!\n"); return -1; } @@ -24,35 +24,56 @@ JNIEXPORT jint JNICALL Java_com_example_pythontest_MainActivity_add //add current path to search pythonforandroid.py file PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('/data/data/com.example.pythontest/files')"); //this mean pythonforandroid.py under /data/data/com.example.pythontest/files + // PyRun_SimpleString("print('hello')"); //this mean pythonforandroid.py under /data/data/com.example.pythontest/files PyObject *pModule; PyObject *pFunction; + PyObject *ytFunction; PyObject *pArgs; + PyObject *ytArgs; PyObject *pRetValue; + PyObject *ytRetValue; + PyObject *streamLink; //import module which name pythonforandroid under /data/data/com.example.pythontest/files pModule = PyImport_ImportModule("pythonforandroid"); if(!pModule){ - __android_log_print(ANDROID_LOG_INFO, "JNIEnv","import python failed!!\n"); + __android_log_print(ANDROID_LOG_INFO, "JNIEnv","import python module failed!!\n"); return -1; } //invoke python function add_func pFunction = PyObject_GetAttrString(pModule, "add_func"); if(!pFunction){ - __android_log_print(ANDROID_LOG_INFO, "JNIEnv","get python function failed!!!\n"); - return -1; } + __android_log_print(ANDROID_LOG_INFO, "JNIEnv","get python function failed!!!\n"); + return -1; + } pArgs = PyTuple_New(2); PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", arg0)); PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", arg1)); pRetValue = PyObject_CallObject(pFunction, pArgs); - __android_log_print(ANDROID_LOG_INFO, "JNIEnv","PyImport_ImportModule %ld",PyLong_AsLong(pRetValue)); + __android_log_print(ANDROID_LOG_INFO, "JNIEnv","pRetValue %ld",PyLong_AsLong(pRetValue)); + + ytFunction = PyObject_GetAttrString(pModule, "get_stream"); + + if(!ytFunction){ + __android_log_print(ANDROID_LOG_INFO, "JNIEnv","get yt function failed!!!\n"); + return -1; + } + ytArgs = PyTuple_New(1); + PyTuple_SetItem(ytArgs, 0, Py_BuildValue("s", "fRh_vgS2dFE")); + ytRetValue = PyObject_CallObject(ytFunction, ytArgs); + streamLink = PyDict_GetItemString(ytRetValue, "stream"); + __android_log_print(ANDROID_LOG_INFO, "JNIEnv","pRetValue %s", PyUnicode_AsUTF8(streamLink)); Py_DECREF(pModule); Py_DECREF(pFunction); + Py_DECREF(ytFunction); Py_DECREF(pArgs); + Py_DECREF(ytArgs); Py_DECREF(pRetValue); + Py_DECREF(ytRetValue); Py_Finalize(); return PyLong_AsLong(pRetValue);