Example #1
0
 def delete_from_db(condition=None):
     dbm = DBManager(expanduser(settings.IMAGES_DB))
     dbm.connect()
     count = dbm.delete('images', condition)
     dbm.commit()
     dbm.disconnect()
     return count
Example #2
0
 def __init__(self):
     if exists(settings.IMAGES_DB):
         self._dbm = DBManager(settings.IMAGES_DB)
         self._dbm.connect()
         self._nodb = False
     else:
         self._nodb = True
Example #3
0
 def __init__(self):
     if exists(settings.IMAGES_DB):
         self._dbm = DBManager(settings.IMAGES_DB)
         self._dbm.connect()
         self._nodb = False
         log.debug('opened db: %s' % settings.IMAGES_DB)
     else:
         self._nodb = True
         log.debug('could not open db: %s' % settings.IMAGES_DB)
Example #4
0
def setup_db():
	settings.IMAGES_DB = expanduser(settings.IMAGES_DB)

	db = DBManager(settings.IMAGES_DB)
	db.connect()
	schema_script = None
	with open(joinpath(dirname(realpath(__file__)).rsplit(sep, 1)[0], 'tables.sql'), 'r') as f:
		schema_script = f.read()
	db.executescript(schema_script)
	db.disconnect()
Example #5
0
 def __init__(self):
     self.move = os.rename if os.stat(
         settings.IMAGES_STORE_FINAL).st_dev == os.stat(
             settings.IMAGES_STORE).st_dev else shutil.move
     if exists(settings.IMAGES_DB):
         self._dbm = DBManager(settings.IMAGES_DB)
         self._dbm.connect()
         self._nodb = False
         log.debug('opened db: %s' % settings.IMAGES_DB)
     else:
         self._nodb = True
         log.debug('could not open db: %s' % settings.IMAGES_DB)
Example #6
0
import sys
from os.path import join as joinpath
from six.moves.urllib.parse import unquote

from imagebot.dbmanager import DBManager

sort_parts = [1, 2, 3]
job = 'default'

if len(sys.argv) >= 3:
    job = sys.argv[1]
    sort_parts = [int(i) for i in sys.argv[2].split(',')]
    #print sort_parts

db = DBManager('../images.db')
db.connect()

result = db.query("SELECT url FROM images WHERE job = '%s' LIMIT 100" % job)

for r in result:
    url = r['url']
    #jobname = r['jobname']
    path = job

    url_parts = [p for p in url.split('/') if p != '']
    del url_parts[0]
    del url_parts[-1]
    #print url_parts

    for i in sort_parts:
        if i < len(url_parts):
Example #7
0
def clear_duplicate_images(arg):
    dbm = DBManager(expanduser(settings.IMAGES_DB))
    dbm.connect()

    jobname = arg
    result = dbm.query('SELECT path FROM images WHERE job = \'%s\' LIMIT 1' %
                       jobname)
    if len(result) == 0:
        print('no such job')

    imagepath = result[0]['path']
    jobpath = imagepath[0:imagepath.rfind(jobname) + len(jobname)]

    print jobpath

    filelist = []
    for root, dirs, files in os.walk(jobpath):
        for filename in files:
            md5hash = md5()
            filepath = joinpath(root, filename)
            with open(filepath, 'rb') as f:
                md5hash.update(f.read())
            filehash = md5hash.hexdigest()
            filelist.append(FileItem(filepath, filehash, False))

    dups_total = 0

    for i in range(0, len(filelist)):
        if filelist[i].dup:
            continue
        hash = filelist[i].hash
        same_files = [(filelist[i].path, os.stat(filelist[i].path).st_mtime)]
        for j in range(i + 1, len(filelist)):
            if filelist[j].hash == hash:
                same_files.append(
                    (filelist[j].path, os.stat(filelist[j].path).st_mtime))
                filelist[j] = FileItem(None, None, True)

        if len(same_files) > 1:
            min_mtime = sys.float_info.max
            keep = -1
            for i in range(0, len(same_files)):
                if same_files[i][1] < min_mtime:
                    min_mtime = same_files[i][1]
                    keep = i

            for i in range(0, len(same_files)):
                if i != keep:
                    dups_total += 1
                    print('deleting %s' % same_files[i][0])
                    try:
                        os.remove(same_files[i][0])
                    except OSError as e:
                        print(e.message)

                    dbm.query(
                        'UPDATE images SET path = \'#duplicate\' WHERE path = \'%s\''
                        % same_files[i][0])

    dbm.commit()
    dbm.disconnect()

    print('%d duplicate images deleted.' % dups_total)