mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Finalized filter_books. dumps to shelf.json
This commit is contained in:
@@ -2,91 +2,39 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from library import Catalogue\n",
|
||||
"import zipfile\n",
|
||||
"\n",
|
||||
"Catalogue = Catalogue()\n",
|
||||
"book_list = Catalogue.filter_books(1)\n",
|
||||
"unpacked = []"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "SyntaxError",
|
||||
"evalue": "invalid syntax (<ipython-input-29-bd44cb25f134>, line 4)",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-29-bd44cb25f134>\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m book.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for book in book_list:\n",
|
||||
" book = zipfile.ZipFile(book, 'r')\n",
|
||||
" with book as bookzip:\n",
|
||||
" book."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<zipfile.ZipExtFile name='content.opf' mode='r' compress_type=deflate>\n",
|
||||
"<zipfile.ZipExtFile name='content.opf' mode='r' compress_type=deflate>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "KeyError",
|
||||
"evalue": "\"There is no item named 'content.opf' in the archive\"",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-23-f050f13a8910>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0munpacked\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mbookzip\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mdetails\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbookzip\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'content.opf'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdetails\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib64/python3.7/zipfile.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self, name, mode, pwd, force_zip64)\u001b[0m\n\u001b[1;32m 1468\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1469\u001b[0m \u001b[0;31m# Get info object for name\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1470\u001b[0;31m \u001b[0mzinfo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetinfo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1471\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1472\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmode\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib64/python3.7/zipfile.py\u001b[0m in \u001b[0;36mgetinfo\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 1396\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0minfo\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1397\u001b[0m raise KeyError(\n\u001b[0;32m-> 1398\u001b[0;31m 'There is no item named %r in the archive' % name)\n\u001b[0m\u001b[1;32m 1399\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1400\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0minfo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mKeyError\u001b[0m: \"There is no item named 'content.opf' in the archive\""
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for i in unpacked:\n",
|
||||
" with i as bookzip:\n",
|
||||
" details = bookzip.open('content.opf')\n",
|
||||
" print(details)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "'ZipFile' object is not subscriptable",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-25-519ce974a672>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mbookzip\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m: 'ZipFile' object is not subscriptable"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"bookzip"
|
||||
"from library import Catalogue\n",
|
||||
"import zipfile\n",
|
||||
"import pprint as pp\n",
|
||||
"import re\n",
|
||||
"\n",
|
||||
"Catalogue = Catalogue()\n",
|
||||
"book_list = Catalogue.filter_books(1)\n",
|
||||
"unpacked = []\n",
|
||||
"\n",
|
||||
"for book in book_list:\n",
|
||||
" book = zipfile.ZipFile(book, 'r')\n",
|
||||
" with book as bookzip:\n",
|
||||
" try:\n",
|
||||
" content_opf = bookzip.open('content.opf')\n",
|
||||
" print(content_opf)\n",
|
||||
" except KeyError as e:\n",
|
||||
" expanded = bookzip.infolist()\n",
|
||||
" regx1 = re.compile(r'\\.opf|^cover')\n",
|
||||
" for i in expanded:\n",
|
||||
" if re.search(regx1, str(i)) == True: pp.pprint(i.filename); print(res)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -8,5 +8,6 @@
|
||||
],
|
||||
"python.testing.pytestEnabled": false,
|
||||
"python.testing.nosetestsEnabled": false,
|
||||
"python.testing.unittestEnabled": true
|
||||
"python.testing.unittestEnabled": true,
|
||||
"python.pythonPath": "/home/raelon/.virtualenvs/spyder/bin/python"
|
||||
}
|
||||
Binary file not shown.
145
data/shelf.json
145
data/shelf.json
File diff suppressed because one or more lines are too long
23
library.py
23
library.py
@@ -44,18 +44,29 @@ class Catalogue:
|
||||
self.scan_folder()
|
||||
regx = re.compile(r"\.epub")
|
||||
self.books = list(filter(regx.search, filter(None, self.file_list)))
|
||||
_book_list_expanded = {}
|
||||
if ret == 0:
|
||||
with open(config.book_shelf, 'w') as f:
|
||||
json.dump(self.books, f)
|
||||
for book in self.books:
|
||||
_book_list_expanded[book] = self.process_book(book)
|
||||
json.dump(_book_list_expanded, f)
|
||||
else:
|
||||
return self.books
|
||||
|
||||
def filter_tags(self, book):
|
||||
def process_book(self, book):
|
||||
f_name = 'content.opf'
|
||||
try:
|
||||
content_opf = list(filter(re.search, book))
|
||||
except:
|
||||
return False
|
||||
book = zipfile.ZipFile(book, 'r')
|
||||
details = {}
|
||||
with book as book_zip:
|
||||
details['files'] = []
|
||||
expanded = book_zip.infolist()
|
||||
regx = re.compile(r'\.opf|cover')
|
||||
for i in expanded:
|
||||
match = re.search(regx, i.filename)
|
||||
if match:
|
||||
# Returns zip file location of requested files
|
||||
details['files'].append(match.string)
|
||||
return details
|
||||
|
||||
def compare_shelf_current(self):
|
||||
try:
|
||||
|
||||
10
main.py
10
main.py
@@ -3,9 +3,13 @@
|
||||
from pyShelf import InitFiles
|
||||
from config import Config
|
||||
from library import Catalogue
|
||||
|
||||
# Get configuration settings
|
||||
config = Config()
|
||||
|
||||
# Initialize file system
|
||||
InitFiles(config.file_array)
|
||||
# Open the Catalogue
|
||||
Catalogue = Catalogue()
|
||||
book_list = Catalogue.filter_books(1)
|
||||
# Filter Your books
|
||||
# This only needs to be run on first run, & when new books are added
|
||||
book_list = Catalogue.filter_books(0)
|
||||
|
||||
|
||||
@@ -2,35 +2,23 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<zipfile.ZipFile filename='/home/raelon/Projects/pyShelf/books/Python Tricks by Dan Bader.epub' mode='r'>\n",
|
||||
"<zipfile.ZipExtFile name='content.opf' mode='r' compress_type=deflate>\n",
|
||||
"<zipfile.ZipFile filename='/home/raelon/Projects/pyShelf/books/Automate the Boring Stuff with Python - Practical Programming for Total Beginners - 1st Edition (2015) (Pdf, Epub & Mobi) Gooner/Automate the Boring Stuff with Python (2015).epub' mode='r'>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "KeyError",
|
||||
"evalue": "\"There is no item named 'content.opf' in the archive\"",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-21-f074cb5c419a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mbook\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mbookzip\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbookzip\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbookzip\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'content.opf'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;32m/usr/lib64/python3.7/zipfile.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self, name, mode, pwd, force_zip64)\u001b[0m\n\u001b[1;32m 1468\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1469\u001b[0m \u001b[0;31m# Get info object for name\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1470\u001b[0;31m \u001b[0mzinfo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetinfo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1471\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1472\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmode\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib64/python3.7/zipfile.py\u001b[0m in \u001b[0;36mgetinfo\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 1396\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0minfo\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1397\u001b[0m raise KeyError(\n\u001b[0;32m-> 1398\u001b[0;31m 'There is no item named %r in the archive' % name)\n\u001b[0m\u001b[1;32m 1399\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1400\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0minfo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mKeyError\u001b[0m: \"There is no item named 'content.opf' in the archive\""
|
||||
"<zipfile.ZipExtFile name='content.opf' mode='r' compress_type=deflate>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from library import Catalogue\n",
|
||||
"import zipfile\n",
|
||||
"import pprint as pp\n",
|
||||
"import re\n",
|
||||
"\n",
|
||||
"Catalogue = Catalogue()\n",
|
||||
"book_list = Catalogue.filter_books(1)\n",
|
||||
@@ -39,8 +27,14 @@
|
||||
"for book in book_list:\n",
|
||||
" book = zipfile.ZipFile(book, 'r')\n",
|
||||
" with book as bookzip:\n",
|
||||
" print(bookzip)\n",
|
||||
" print(bookzip.open('content.opf'))"
|
||||
" try:\n",
|
||||
" content_opf = bookzip.open('content.opf')\n",
|
||||
" print(content_opf)\n",
|
||||
" except KeyError as e:\n",
|
||||
" expanded = bookzip.infolist()\n",
|
||||
" regx1 = re.compile(r'\\.opf|^cover')\n",
|
||||
" for i in expanded:\n",
|
||||
" if re.search(regx1, str(i)) == True: pp.pprint(i.filename); print(res)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
29
scratchpad.py
Normal file
29
scratchpad.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# To add a new cell, type '#%%'
|
||||
# To add a new markdown cell, type '#%% [markdown]'
|
||||
#%%
|
||||
from library import Catalogue
|
||||
import zipfile
|
||||
import pprint as pp
|
||||
import re
|
||||
|
||||
Catalogue = Catalogue()
|
||||
book_list = Catalogue.filter_books(1)
|
||||
unpacked = []
|
||||
|
||||
for book in book_list:
|
||||
book = zipfile.ZipFile(book, 'r')
|
||||
with book as bookzip:
|
||||
try:
|
||||
content_opf = bookzip.open('content.opf')
|
||||
print(content_opf)
|
||||
except KeyError as e:
|
||||
expanded = bookzip.infolist()
|
||||
regx1 = re.compile(r'\.opf|^cover')
|
||||
for i in expanded:
|
||||
if re.search(regx1, str(i)) == True: pp.pprint(i.filename); print(res)
|
||||
|
||||
|
||||
#%%
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user