Skip to content

Commit

Permalink
update project structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyqin committed Mar 21, 2018
1 parent d435b9a commit e5ae54b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -99,3 +99,5 @@ ENV/

# mypy
.mypy_cache/
.idea/
*.db
14 changes: 12 additions & 2 deletions README.md
Expand Up @@ -11,7 +11,17 @@ pip install -r requirements.txt

# Step 1

Run command to scan all photos from a location.
Create the database structure.

```shell
python db.py
```

This action will create a `photo.db` in workspace.

# Step 2

Run command to scan all photos from somewhere.

```shell
python scan.py dir1 dir2
Expand All @@ -24,7 +34,7 @@ This action will save all photo files(*.jpg only) into database.
Run command to start a webserver to help identify duplicate photos.

```
python main.py
python web.py
```

Launch browser to http://127.0.0.1:5000 to cleanup duplicate photos.
53 changes: 53 additions & 0 deletions db.py
@@ -0,0 +1,53 @@
import sqlite3

db_file = "photo.db"
conn = sqlite3.connect(db_file)
cursor = conn.cursor()


def create_table():
drop_table()
cursor.execute('''CREATE TABLE photo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hash VARCHAR(50) NOT NULL,
name TEXT NOT NULL,
path TEXT NOT NULL,
existed INTEGER DEFAULT 1)''')


def drop_table():
cursor.execute('DROP TABLE IF EXISTS photo')


def insert_file(hash, name, path):
cursor.execute('INSERT INTO photo (hash,name,path) VALUES (?,?,?)', (hash, name, path))


def is_existed(file_path):
cursor.execute('SELECT COUNT(*) FROM photo WHERE path = ?', (file_path,))
row = cursor.fetchone()
return row[0] != 0


def mark_deleted(file_path):
cursor.execute('UPDATE photo SET existed =0 WHERE NAME = ?', (file_path,))
conn.commit()


def find_duplicate():
cursor.execute('''SELECT * FROM photo WHERE hash IN
(SELECT hash FROM photo GROUP BY hash HAVING count(*) > 1)''')
rows = cursor.fetchall()
return rows


def close_db():
if conn is not None:
conn.close()


if __name__ == '__main__':
print('create photo.db ...')
create_table()
close_db()
print('done!')
Empty file added scan.py
Empty file.
Empty file added web.py
Empty file.

0 comments on commit e5ae54b

Please sign in to comment.