Skip to content

Commit

Permalink
change src location and pymysql instead of pymysqlclient
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavePate committed Dec 24, 2015
1 parent d93a1b1 commit 9c3e89b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -7,7 +7,7 @@ test:
py.test -c ./ressources/pytest.ini --showlocals --duration=3 -v -s --confpath=${PWD}/ressources/test_conf.json

testdev:
py.test -c ./ressources/pytest.ini --showlocals --duration=3 -v -s --confpath=${PWD}/ressources/test_conf.json -k test_bad_taketime
py.test -c ./ressources/pytest.ini --showlocals --duration=3 -v -s --confpath=${PWD}/ressources/test_conf.json -k test_dash_r

initvenv:
pip install -r requirements.txt
Expand Down
9 changes: 8 additions & 1 deletion README.md
@@ -1,11 +1,18 @@
# Lycheesync

[![Build Status](https://travis-ci.org/GustavePate/lycheesync.svg)](https://travis-ci.org/GustavePate/lycheesync)
[![Build Status](https://travis-ci.org/GustavePate/perfect_python_script.svg)](https://travis-ci.org/GustavePate/perfect_python_script)

Lycheesync is a command line tool to synchronise a directory containing photos with Lychee.
* Lycheesync is meant to be used on the same server that run Lychee. If your photo source directory is on another computer, use synchronize tools like rsync or owncloud.
* Lycheesync is often meant to be run regulary and automatically, use cron for this (or monitor [filesystem events](https://github.com/seb-m/pyinotify) if you want your photos really fast online )

# TODO

* documentation for pymysql
* add loggers
* launch with lycheesync instead of main.py


# What's new

See [changlog](./doc/changelog.md)
Expand Down
69 changes: 36 additions & 33 deletions lycheedao.py → lycheesync/lycheedao.py
Expand Up @@ -3,7 +3,7 @@

from __future__ import print_function
from __future__ import unicode_literals
import MySQLdb
import pymysql
import datetime
import traceback
import re
Expand All @@ -27,11 +27,17 @@ def __init__(self, conf):
"""

self.conf = conf
self.db = MySQLdb.connect(host=self.conf["dbHost"],
user=self.conf["dbUser"],
passwd=self.conf["dbPassword"],
db=self.conf["db"],
charset='utf8mb4')
# self.db = MySQLdb.connect(host=self.conf["dbHost"],
# user=self.conf["dbUser"],
# passwd=self.conf["dbPassword"],
# db=self.conf["db"],
# charset='utf8mb4')
self.db = pymysql.connect(host=self.conf['dbHost'],
user=self.conf['dbUser'],
passwd=self.conf['dbPassword'],
db=self.conf['db'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cur = self.db.cursor()
cur.execute("set names utf8;")

Expand All @@ -40,20 +46,14 @@ def __init__(self, conf):

self.loadAlbumList()

def _p(self, data):
"""
protect / escape strings
"""
return MySQLdb.escape_string(str(data)).decode(encoding='UTF-8')

def getAlbumNameDBWidth(self):
res = 50 # default value
query = "show columns from lychee_albums where Field='title'"
cur = self.db.cursor()
try:
cur.execute(query)
row = cur.fetchone()
type = row[1]
type = row['Type']
# is type ok
p = re.compile('varchar\(\d+\)', re.IGNORECASE)
if p.match(type):
Expand All @@ -74,22 +74,20 @@ def getAlbumMinMaxIds(self):
"""
returns min, max album ids
"""
min_album_query = "select min(id) from lychee_albums"
max_album_query = "select max(id) from lychee_albums"
min_album_query = "select min(id) as min from lychee_albums"
max_album_query = "select max(id) as max from lychee_albums"
try:
min = -1
max = -1
cur = self.db.cursor()

cur.execute(min_album_query)
rows = cur.fetchall()
for row in rows:
min = row[0]
rows = cur.fetchone()
min = rows['min']

cur.execute(max_album_query)
rows = cur.fetchall()
for row in rows:
max = row[0]
rows = cur.fetchone()
max = rows['max']

if (self.conf['verbose'] is True):
print("INFO min max album id: " + str(min) + " to " + str(max))
Expand Down Expand Up @@ -155,7 +153,7 @@ def loadAlbumList(self):
cur.execute("SELECT title,id from lychee_albums")
rows = cur.fetchall()
for row in rows:
self.albumslist[row[0]] = row[1]
self.albumslist[row['title']] = row['id']

if self.conf['verbose']:
print("INFO album list in db:", self.albumslist)
Expand All @@ -181,14 +179,14 @@ def getAlbumNameFromIdsList(self, list_id):
cur = self.db.cursor()
cur.execute(query)
rows = cur.fetchall()
album_names = [column[0] for column in rows]
print(rows)
album_names = [column['title'] for column in rows]
except Exception as e:
album_names = ''
print('ERROR impossible to execute ' + query)
finally:
return album_names


def photoExists(self, photo):
"""
Check if a photo already exists in its album based on its original name or checksum
Expand All @@ -206,15 +204,20 @@ def photoExists(self, photo):
if len(row) != 0:
res = True

## Add Warning if photo exists in another album
query = ("select album from lychee_photos where (title='" + photo.originalname + "' OR checksum='" + photo.checksum + "')")
# Add Warning if photo exists in another album
query = (
"select album from lychee_photos where (title='" +
photo.originalname +
"' OR checksum='" +
photo.checksum +
"')")
cur = self.db.cursor()
cur.execute(query)
rows = cur.fetchall()
album_ids = [id[0] for id in rows]
album_ids = [row['album'] for row in rows]
if len(album_ids) > 0:
print("WARN a photo with this name or checksum already exists in at least another album: " + str(self.getAlbumNameFromIdsList(album_ids)))

print("WARN a photo with this name or checksum already exists in at least another album: " +
str(self.getAlbumNameFromIdsList(album_ids)))

except Exception:
print("ERROR photoExists:", photo.srcfullpath, "won't be added to lychee")
Expand Down Expand Up @@ -250,8 +253,8 @@ def createAlbum(self, album):
cur.execute(query)
row = cur.fetchone()
print('row:' + str(row))
self.albumslist['name'] = row[0]
album['id'] = row[0]
self.albumslist['name'] = row['id']
album['id'] = row['id']
if self.conf["verbose"]:
print("INFO album created:", album)

Expand All @@ -278,7 +281,7 @@ def eraseAlbum(self, album):
cur.execute(selquery)
rows = cur.fetchall()
for row in rows:
res.append(row[0])
res.append(row['url'])
cur.execute(query)
self.db.commit()
if self.conf["verbose"]:
Expand Down Expand Up @@ -317,7 +320,7 @@ def listAllPhoto(self):
cur.execute(selquery)
rows = cur.fetchall()
for row in rows:
res.append(row[0])
res.append(row['url'])
except Exception:
print("listAllPhoto", Exception)
traceback.print_exc()
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions lycheesyncer.py → lycheesync/lycheesyncer.py
Expand Up @@ -7,8 +7,8 @@
import shutil
import stat
import traceback
from lycheedao import LycheeDAO
from lycheemodel import LycheePhoto
from lycheesync.lycheedao import LycheeDAO
from lycheesync.lycheemodel import LycheePhoto
from PIL import Image
import datetime
import time
Expand Down
File renamed without changes.
Expand Up @@ -3,7 +3,7 @@
import os
import pwd
import grp
from lycheesyncer import LycheeSyncer
from lycheesync.lycheesyncer import LycheeSyncer
import MySQLdb
import hashlib
import stat
Expand Down
4 changes: 2 additions & 2 deletions main.py
Expand Up @@ -3,8 +3,8 @@

from __future__ import unicode_literals
from __future__ import print_function
from lycheesyncer import LycheeSyncer
from update_scripts import inf_to_lychee_2_6_2
from lycheesync.lycheesyncer import LycheeSyncer
from lycheesync.update_scripts import inf_to_lychee_2_6_2
import argparse
import os
import sys
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Expand Up @@ -2,5 +2,4 @@ pillow
pymysql
pytest==2.7.3
click
mysqlclient
python-dateutil

0 comments on commit 9c3e89b

Please sign in to comment.