Not checking and kirbybase

Using kirbybase to store downloaded images
A nasty bug that always returned the wrong result when checking whether an image had already been downloaded has been fixed
This commit is contained in:
ryuslash 2010-04-19 09:16:04 +02:00
parent 2db5555609
commit af529bcd4e
6 changed files with 81 additions and 22 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@
*.pyc
\#*\#
.*
modules/plane.tbl

View file

@ -24,16 +24,12 @@ import sys
import os
import config
from util import raw_input_with_default
import download
import progressbar
import sorter
def raw_input_with_default(default, prompt):
inp = raw_input("%s (default=%s): " % (prompt, default))
if inp == "":
return default
return inp
config._optioncreator = raw_input_with_default
base_url = "http://boards.4chan.org/"

60
backend.py Normal file
View file

@ -0,0 +1,60 @@
import os
import modules.kirbybase
from util import confdir,raw_input_with_default
import config
class _Backend(object):
""" A class that communicates with the datastore """
def __init__(self):
self.table = os.path.join(confdir, "images.tbl")
self.store = modules.kirbybase.KirbyBase()
self.__collection = ""
self.__new_collection = []
self.load()
def create_store_if_needed(self):
if not os.path.exists(self.table):
return self.store.create(self.table, ["filename:String"])
return True
def add(self, filename):
self.__new_collection.append(filename)
def check(self, filename):
collected = filename in self.__collection
downloaded = filename in self.__new_collection
if not downloaded:
self.add(filename)
if collected or downloaded:
return True
return False
def save(self):
os.remove(self.table)
self.create_store_if_needed()
for f in self.__new_collection:
self.store.insert(self.table, [f])
def load(self):
if os.path.exists(self.table):
self.__collection = self.store.select(self.table, ['recno'], ['*'], ['filename'], returnType="report")
_backend = None
def Backend():
global _backend
if _backend == None:
_backend = _Backend()
return _backend
if __name__ == "__main__":
backend = Backend()
config._optioncreator = raw_input_with_default
cfg = config.Configuration()
for f in os.listdir(cfg.get_archive_location()):
backend.add(f)

View file

@ -144,6 +144,10 @@ class Downloader(object):
i += 1
progress.complete()
be = backend.Backend()
be.save()
return (skipped, failed, downloaded, total)
if __name__ == "__main__":

View file

@ -22,6 +22,7 @@ import Image
import shutil
import os
import datetime
import backend
def dummy_option_creator(value1, value2): pass
config._optioncreator = dummy_option_creator
@ -36,7 +37,7 @@ class Sorter:
download_base = self.conf.get_download_location()
retval = True
if self.check_filename(filename):
if not self.check_filename(filename):
image = None
try:
image = Image.open(os.path.join(download_base,
@ -59,7 +60,6 @@ class Sorter:
self.copy(filename, folderpath)
break
self.archive(filename)
self.remove(filename)
return retval
@ -84,20 +84,12 @@ class Sorter:
print "\nHow can this even happen?! Copying", source, "to", dest
def archive(self, filename):
location = self.conf.get_archive_location()
if not os.path.exists(location):
os.makedirs(location)
dest = os.path.join(location, os.path.basename(filename))
#print dest
f = open(dest, "w")
file.close(f)
be = backend.Backend()
be.add(os.path.basename(filename))
def archive_check(self, filename):
archive_path = self.conf.get_archive_location()
fullname = os.path.join(archive_path, filename)
return os.path.exists(fullname)
be = backend.Backend()
return be.check(os.path.basename(filename))
def check_filename(self, filename):
ext = os.path.splitext(filename)[1]

View file

@ -5,3 +5,9 @@ homedir = os.getenv("HOME")
if homedir is None:
homedir = os.path.dirname(sys.argv[0])
confdir = os.path.join(homedir, ".4grab")
def raw_input_with_default(default, prompt):
inp = raw_input("%s (default=%s): " % (prompt, default))
if inp == "":
return default
return inp