Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import boto3
  2. import MySQLdb
  3. import requests
  4. import shutil
  5. import subprocess, shlex
  6.  
  7.  
  8. s3 = boto3.resource('s3',
  9. endpoint_url = 'https://s3.wasabisys.com')
  10.  
  11. BUCKET = "images.hizzle.me"
  12.  
  13. db2 = MySQLdb.connect(host="127.0.0.1", # your host, usually localhost
  14. charset='utf8',
  15. user="", # your username
  16. passwd="", # your password
  17. db="") # name of the data base
  18.  
  19. cur2 = db2.cursor()
  20.  
  21. cur2.execute('SET NAMES utf8;')
  22. cur2.execute('SET CHARACTER SET utf8;')
  23. cur2.execute('SET character_set_connection=utf8;')
  24.  
  25. cur2.execute("SELECT MAX(lastupdate) FROM updates WHERE name = 'images'")
  26. last_update = cur2.fetchone()[0]
  27. print "Getting records since", last_update
  28. cur2.execute("SELECT mediaName, tableuid, displayorder FROM media where modified >= %s and mediatype = 'PHOTO';", [last_update])
  29. rows = cur2.fetchall()
  30.  
  31. for row in rows:
  32. url = "https://matrixmedia.northstarmls.com/MediaServer/GetMedia.ashx?Key=" + row[1] + "&TableID=1&Size=5&Type=1&Number=" + row[2]
  33. print("Getting image url: " + url)
  34. response = requests.get(url)
  35. media_name = row[0]
  36. final_path = "/home/images/2019/" + media_name
  37. if response.status_code == 200:
  38. with open(media_name, 'wb') as f:
  39. f.write(response.content)
  40. shutil.move(media_name, final_path)
  41. cmd = "aws s3 mv " + final_path + " s3://test1testtest11/ --include 'houses.python' --endpoint-url=https://s3.wasabisys.com"
  42. s3.Bucket(BUCKET).upload_file(final_path, media_name)
  43. print media_name, "uploaded"
  44. cur2.execute("INSERT INTO updates (name, lastupdatecount) VALUES (%s, %s)",("images", len(rows)))
  45. print "Updated date, total uploaded to bucket:", len(rows)
  46. db2.commit()
  47. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement