misc python code
This commit is contained in:
331
code/misc/python/scripts/DigitalPaper Management.ipynb
Normal file
331
code/misc/python/scripts/DigitalPaper Management.ipynb
Normal file
@@ -0,0 +1,331 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from dptrp1.dptrp1 import DigitalPaper\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"password = getpass.getpass()\n",
|
||||
"command = \"sudo -S sudo chmod 666 /dev/ttyACM0\" #can be any command but don't forget -S as it enables input from stdin\n",
|
||||
"os.system('echo %s | %s' % (password, command))\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/dev/ttyACM0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import serial\n",
|
||||
"ser = serial.Serial('/dev/ttyACM0') # open serial port\n",
|
||||
"print(ser.name) # check which port was really used\n",
|
||||
"ser.write(b'\\x01\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x04') # write a string b\"\\x01\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x04\"\n",
|
||||
"ser.close() # close port\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client_id=''\n",
|
||||
"key=''\n",
|
||||
"SYNC_DIR = '/home/dl92/Downloads/DigitalPaper/'\n",
|
||||
"def connect(address=''):\n",
|
||||
" \"\"\"\n",
|
||||
" Loads the key and client ID to authenticate with the DPT-RP1\n",
|
||||
" \"\"\"\n",
|
||||
" with open('/home/dl92/.dpt-client.txt', 'r') as f:\n",
|
||||
" client_id = f.readline().strip()\n",
|
||||
"\n",
|
||||
" with open('/home/dl92/.dpt-key.txt', 'r') as f:\n",
|
||||
" key = f.read()\n",
|
||||
"\n",
|
||||
" dpt = DigitalPaper(address)\n",
|
||||
" dpt.authenticate(client_id, key)\n",
|
||||
" return dpt\n",
|
||||
"\n",
|
||||
"def sync(dpt):\n",
|
||||
" \"\"\"\n",
|
||||
" Given an authenticated DigitalPaper instance, download all note files to a\n",
|
||||
" specified directory.\n",
|
||||
" \"\"\"\n",
|
||||
" for doc in [f for f in dpt.list_documents() if (is_modified_note(f) and is_ReadingFolder(f)) ]:\n",
|
||||
" #docpath=os.path.dirname(doc['entry_path'])\n",
|
||||
" #if docpath=='Document/Reading' :\n",
|
||||
" data = dpt.download(doc['entry_path'])\n",
|
||||
" local_path = SYNC_DIR + os.path.basename(doc['entry_path'])\n",
|
||||
" with open(local_path, 'wb') as f:\n",
|
||||
" f.write(data)\n",
|
||||
" print('Saved {} to {}'.format(doc['entry_path'], local_path))\n",
|
||||
" \n",
|
||||
"def is_modified_note(doc):\n",
|
||||
" import dateparser\n",
|
||||
" if doc['document_type'] == 'note' or doc['document_type'] == 'normal':\n",
|
||||
" local_path = SYNC_DIR + os.path.basename(doc['entry_path'])\n",
|
||||
" \n",
|
||||
" if not os.path.exists(local_path):\n",
|
||||
" return True\n",
|
||||
" else:\n",
|
||||
" #print (local_path,doc['modified_date'], os.path.getmtime(local_path), dateparser.parse(doc['modified_date']).timestamp())\n",
|
||||
" return os.path.getmtime(local_path) < dateparser.parse(doc['modified_date']).timestamp()\n",
|
||||
" \n",
|
||||
"def is_ReadingFolder(doc):\n",
|
||||
" docpath=os.path.dirname(doc['entry_path'])\n",
|
||||
" if docpath=='Document/Reading':\n",
|
||||
" return True\n",
|
||||
" else:\n",
|
||||
" return False\n",
|
||||
" \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def upload_overwrite(dpt,localpath, remotepath='Document/Reading'):\n",
|
||||
" import glob, os\n",
|
||||
" \n",
|
||||
" files= glob.glob(localpath+'/*.pdf')\n",
|
||||
" for f in files:\n",
|
||||
" print (f)\n",
|
||||
" dpt.upload_file(f,remotepath)\n",
|
||||
" \n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dpt=connect('192.168.0.131')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"#dpt=connect('https://192.168.0.13')\n",
|
||||
"#dpt=connect('22:6f:5d:46:3f:16')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'device_color': '#ffffff',\n",
|
||||
" 'model_name': 'DPT-RP1',\n",
|
||||
" 'serial_number': '5021254',\n",
|
||||
" 'sku_code': 'U'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"dpt.get_info()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sync(dpt)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/dl92/Downloads/DigitalPaper/Upload/Kevin P. Murphy_Machine Learning_ A Probabilistic Perspective.pdf\n",
|
||||
"/home/dl92/Downloads/DigitalPaper/Upload/John D. Kelleher_Deep Learning.pdf\n",
|
||||
"/home/dl92/Downloads/DigitalPaper/Upload/Andriy Burkov_The Hundred-Page Machine Learning Book.pdf\n",
|
||||
"/home/dl92/Downloads/DigitalPaper/Upload/John D. Kelleher_Fundamentals of Machine Learning for Predictive Data Analytics_ Algorithms, Worked Examples, and Case Studies.pdf\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"UPLOAD_DIR='/home/dl92/Downloads/DigitalPaper/Upload/'\n",
|
||||
"upload_overwrite(dpt,UPLOAD_DIR)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'health': 'good',\n",
|
||||
" 'icon_type': 'level_bar_4',\n",
|
||||
" 'level': '95',\n",
|
||||
" 'pen': '100',\n",
|
||||
" 'plugged': 'not_plugged',\n",
|
||||
" 'status': 'discharging'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"dpt.get_battery()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dpt.new_folder('Document/Work')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "ModuleNotFoundError",
|
||||
"evalue": "No module named 'pypdf'",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-29-913447a672fb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpypdf\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'pypdf'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import pypdf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"rate=0.33\n",
|
||||
"price=394\n",
|
||||
"motherboardasset=price-2*price*rate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"rate=0.33\n",
|
||||
"price=216\n",
|
||||
"powersupplyasset=price-2*price*rate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"73.44"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"powersupplyasset\n",
|
||||
"#motherboardasset"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"celltoolbar": "Attachments",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.8.10 ('General')",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.10"
|
||||
},
|
||||
"toc": {
|
||||
"base_numbering": 1,
|
||||
"nav_menu": {},
|
||||
"number_sections": true,
|
||||
"sideBar": true,
|
||||
"skip_h1_title": false,
|
||||
"title_cell": "Table of Contents",
|
||||
"title_sidebar": "Contents",
|
||||
"toc_cell": false,
|
||||
"toc_position": {},
|
||||
"toc_section_display": true,
|
||||
"toc_window_display": false
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "97f3fdd28b09b8a55d08fd0c4c2cf066e1a4715931f0fa71c8f74dcb74701738"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user