Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import mysql.connector
  2. import os.path, shutil
  3.  
  4.  
  5. SRC_PATH="/tmp/piwigo/upload/folder"
  6. OUT_PATH="/tmp/piwigo_images_extracted/"
  7.  
  8. USER="dbuser"
  9. PWD = "dbpassword"
  10. DB = "db"
  11. HOST= "localhost"
  12.  
  13. dbcon = mysql.connector.connect(user=USER, password=PWD, host=HOST, database=DB)
  14.  
  15. filecursor = dbcon.cursor(buffered=True)
  16. filecursor.execute("select id, file, path from images;")
  17.  
  18.  
  19. def getPath(id=0):
  20. cur2 = dbcon.cursor(buffered=True)
  21. path_ = list()
  22. while(True):
  23. cur2.execute("SELECT name, id_uppercat from categories where categories.id=%d;" % (id))
  24.  
  25. p, parentID = cur2.fetchone()
  26. path_.insert(0, p.replace("/", "_"))
  27. id = parentID
  28. if id is None:
  29. break
  30. return os.path.join(OUT_PATH, *path_)
  31.  
  32.  
  33. for (id, file, path) in filecursor:
  34. catID = dbcon.cursor()
  35. catID.execute("SELECT category_id from image_category where image_id=%d;"%(id))
  36. catID = catID.fetchone()[0]
  37.  
  38. src_path = os.path.join(SRC_PATH, path)
  39. out_path = getPath(catID)
  40. file_out = os.path.join(out_path, file.decode('utf-8'))
  41.  
  42. print(f"{src_path} => {out_path}/{file.decode('utf-8')}: ", end="")
  43. if not os.path.exists(out_path):
  44. os.makedirs(out_path)
  45.  
  46. if os.path.exists(src_path):
  47. if not os.path.exists(file_out):
  48. print("Copying...", end="")
  49. try:
  50. shutil.copy2(src_path, file_out)
  51. print("OK.")
  52. except Exception as e:
  53. print("FAIL: %s"%e)
  54. else:
  55. print("Do Nothing, Target already exists")
  56. else:
  57. print("Error: Source file does not exists")
  58.  
  59. dbcon.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement