You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
5.5 KiB
Python
175 lines
5.5 KiB
Python
4 years ago
|
from io import StringIO
|
||
8 years ago
|
import re
|
||
|
from subprocess import check_output, STDOUT
|
||
3 years ago
|
from time import sleep
|
||
|
import ntpath
|
||
8 years ago
|
|
||
|
from os import utime
|
||
7 years ago
|
from os.path import join
|
||
8 years ago
|
|
||
7 years ago
|
from pushbullet import Pushbullet
|
||
8 years ago
|
|
||
|
from optparse import OptionParser
|
||
4 years ago
|
from configparser import ConfigParser
|
||
7 years ago
|
|
||
8 years ago
|
def enable_notifications(options):
|
||
|
if options.pushbullet:
|
||
7 years ago
|
fail = False
|
||
|
try:
|
||
|
pb = Pushbullet(options.pushbullet)
|
||
7 years ago
|
except BaseException:
|
||
4 years ago
|
print("Problem wtih connecting to pushbullet. API Key likely invalid")
|
||
7 years ago
|
fail = True
|
||
|
if options.pbdevice and not fail:
|
||
8 years ago
|
try:
|
||
|
pb = pb.get_device(options.pbdevice)
|
||
7 years ago
|
except BaseException:
|
||
4 years ago
|
print("Cannot get this device.")
|
||
7 years ago
|
fail = True
|
||
8 years ago
|
pass
|
||
7 years ago
|
if not fail:
|
||
|
temp_note = Notification()
|
||
|
temp_note.send_notification = pb.push_note
|
||
|
yield temp_note
|
||
7 years ago
|
|
||
|
|
||
8 years ago
|
def touch(fname, times=None):
|
||
|
with open(fname, 'a'):
|
||
|
utime(fname, times)
|
||
7 years ago
|
|
||
|
|
||
8 years ago
|
def main(options):
|
||
7 years ago
|
try:
|
||
7 years ago
|
res = check_output(
|
||
3 years ago
|
"python3.9 fanficdownload.py -c config.ini",
|
||
7 years ago
|
shell=True,
|
||
|
stderr=STDOUT)
|
||
7 years ago
|
except Exception as e:
|
||
4 years ago
|
print(e)
|
||
7 years ago
|
res = None
|
||
7 years ago
|
if not res:
|
||
|
return
|
||
|
else:
|
||
4 years ago
|
res = res.decode('utf-8')
|
||
|
print(res)
|
||
8 years ago
|
buf = StringIO(res)
|
||
|
regex = re.compile("Added (?:.*/)?(.*)-.* to library with id \d*")
|
||
7 years ago
|
searcher = regex.search
|
||
|
stripper = False
|
||
8 years ago
|
for line in buf.readlines():
|
||
7 years ago
|
r = searcher(line)
|
||
8 years ago
|
if r:
|
||
3 years ago
|
story = ntpath.basename(r.group(1).strip())
|
||
7 years ago
|
stripper = True
|
||
8 years ago
|
for notify in enable_notifications(options):
|
||
|
notify.send_notification("New Fanfiction Download", story)
|
||
7 years ago
|
if stripper and options.tag:
|
||
7 years ago
|
import sqlite3
|
||
7 years ago
|
with sqlite3.connect(join(options.library_path, "metadata.db")) as conn:
|
||
|
c = conn.cursor()
|
||
|
c.execute("delete from books_tags_link where id in (select id from books_tags_link where tag in (select id from tags where name like '%Last Update%'));")
|
||
8 years ago
|
return
|
||
|
|
||
7 years ago
|
|
||
8 years ago
|
if __name__ == "__main__":
|
||
|
option_parser = OptionParser(usage="usage: %prog [flags]")
|
||
7 years ago
|
option_parser.add_option(
|
||
|
'-p',
|
||
|
'--pushbullet',
|
||
|
action='store',
|
||
|
dest='pushbullet',
|
||
|
help='If you want to use pushbullet, pass in your key here.')
|
||
|
option_parser.add_option(
|
||
|
'-d',
|
||
|
'--device',
|
||
|
action='store',
|
||
|
dest='pbdevice',
|
||
|
help='If you wish to only send to a certian pushbullet device, put the device name here. If the device name is invalid, will just send to all pushbullets associated with the acc')
|
||
|
option_parser.add_option(
|
||
|
'-n',
|
||
|
'--notify',
|
||
|
action='store_true',
|
||
|
dest='notify',
|
||
|
help='Enable if you want to use system notifications. Only for Win/Linux.')
|
||
|
option_parser.add_option(
|
||
|
'-c',
|
||
|
'--config',
|
||
|
action='store',
|
||
|
dest='config',
|
||
|
help='Config file for inputs. Blank config file is provided. No default. If an option is present in whatever config file is passed it, the option will overwrite whatever is passed in through command line arguments unless the option is blank. Do not put any quotation marks in the options.')
|
||
|
option_parser.add_option(
|
||
|
'-t',
|
||
|
'--tag',
|
||
|
action='store_true',
|
||
|
dest='tag',
|
||
|
help='Strip Last Updated tags from calibredb. Requires library to be passed in.')
|
||
|
option_parser.add_option(
|
||
|
'-l',
|
||
|
'--library',
|
||
|
action='store',
|
||
|
dest='library',
|
||
|
help='Path to calibre library. If you are connecting to a calibre webserver then this should be the url.')
|
||
|
option_parser.add_option(
|
||
|
'-a',
|
||
|
'--library-path',
|
||
|
action='store',
|
||
|
dest='library_path',
|
||
|
help='Path location of library. Will be equal to library if nothing is passed in.')
|
||
|
|
||
8 years ago
|
(options, args) = option_parser.parse_args()
|
||
7 years ago
|
|
||
|
if options.library and not options.library_path:
|
||
|
options.library_path = options.library
|
||
|
|
||
8 years ago
|
if options.config:
|
||
|
touch(options.config)
|
||
|
config = ConfigParser(allow_no_value=True)
|
||
|
config.read(options.config)
|
||
7 years ago
|
|
||
|
def updater(option, newval): return newval if newval != "" else option
|
||
|
|
||
|
try:
|
||
|
options.pushbullet = updater(
|
||
|
options.pushbullet, config.get(
|
||
|
'runner', 'pushbullet'))
|
||
|
except BaseException:
|
||
|
pass
|
||
|
|
||
|
try:
|
||
|
options.pbdevice = updater(
|
||
|
options.pbdevice, config.get(
|
||
|
'runner', 'pbdevice'))
|
||
|
except BaseException:
|
||
|
pass
|
||
|
|
||
|
try:
|
||
|
options.tag = updater(
|
||
|
options.tag, config.getboolean(
|
||
|
'runner', 'tag'))
|
||
|
except BaseException:
|
||
|
pass
|
||
|
|
||
|
try:
|
||
|
options.library = updater(
|
||
|
options.library, config.get(
|
||
|
'locations', 'library'))
|
||
|
except BaseException:
|
||
|
pass
|
||
|
|
||
|
try:
|
||
|
options.library_path = updater(
|
||
|
options.library, config.get(
|
||
|
'locations', 'library_path'))
|
||
|
except BaseException:
|
||
|
pass
|
||
|
|
||
8 years ago
|
if options.pbdevice and not options.pushbullet:
|
||
|
raise ValueError("Can't use a pushbullet device without key")
|
||
7 years ago
|
if options.tag and not options.library:
|
||
7 years ago
|
raise ValueError(
|
||
|
"Can't strip tags from calibre library without a library location.")
|
||
|
|
||
3 years ago
|
while(True):
|
||
|
main(options)
|
||
|
sleep(60)
|