Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions PythonAndroid/application/app/src/main/assets/pythonforandroid.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion PythonAndroid/application/jni/Application.mk
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
APP_ABI := armeabi-v7a
APP_PLATFORM := android-12
APP_PLATFORM := android-23
35 changes: 28 additions & 7 deletions PythonAndroid/application/jni/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down