Social Posts image upload: Overwrite existing database entry and add image upload functionality to YouTube videos
This commit is contained in:
parent
621d3e656f
commit
3a4e14a96f
|
@ -31,16 +31,33 @@ def insertIntoDatabase(platform, title, url, author, authorUrl, published, image
|
||||||
def generateImageName():
|
def generateImageName():
|
||||||
return random.getrandbits(128)
|
return random.getrandbits(128)
|
||||||
|
|
||||||
|
# Reads image from entered url and uploads it to the own server while creating a corresponding entry in the database
|
||||||
def uploadImage(imageUrl, originalName):
|
def uploadImage(imageUrl, originalName):
|
||||||
|
|
||||||
response = requests.get(imageUrl)
|
response = requests.get(imageUrl)
|
||||||
img = Image.open(BytesIO(response.content))
|
img = Image.open(BytesIO(response.content))
|
||||||
|
|
||||||
name = '%032x' % generateImageName()
|
name = '%032x' % generateImageName()
|
||||||
|
|
||||||
|
# Checks if there is already an uploaded image with the same original name
|
||||||
|
# If so the previously generated random name is overwritten and the entry in the database deleted
|
||||||
|
try:
|
||||||
|
cur.execute("SELECT * FROM files WHERE original_name = %s", (originalName))
|
||||||
|
result = cur.fetchone()
|
||||||
|
name = result[1]
|
||||||
|
|
||||||
|
cur.execute("DELETE FROM files WHERE ID = %s", (result[0]))
|
||||||
|
db.commit()
|
||||||
|
except:
|
||||||
|
db.rollback()
|
||||||
|
|
||||||
|
# Writes the image to the correct path
|
||||||
path = 'files/userContent/%s.jpg' % name
|
path = 'files/userContent/%s.jpg' % name
|
||||||
img.save(path)
|
img.save(path)
|
||||||
|
|
||||||
fileSize = os.path.getsize(path)
|
fileSize = os.path.getsize(path)
|
||||||
|
|
||||||
|
# Adds a new entry to the database with all the data
|
||||||
try:
|
try:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO files (name, original_name, type, size, path, isUserData) VALUES (%s, %s, 'image/jpeg', %s, %s, 1)",
|
"INSERT INTO files (name, original_name, type, size, path, isUserData) VALUES (%s, %s, 'image/jpeg', %s, %s, 1)",
|
||||||
|
@ -50,6 +67,7 @@ def uploadImage(imageUrl, originalName):
|
||||||
print('it didnt work')
|
print('it didnt work')
|
||||||
db.rollback()
|
db.rollback()
|
||||||
|
|
||||||
|
# Returns the relative URL path for displaying the image later on
|
||||||
return '/f/' + name
|
return '/f/' + name
|
||||||
|
|
||||||
def getYouTubeVideos():
|
def getYouTubeVideos():
|
||||||
|
@ -69,6 +87,9 @@ def getYouTubeVideos():
|
||||||
else:
|
else:
|
||||||
thumbnail = snippet['thumbnails']['standard']['url']
|
thumbnail = snippet['thumbnails']['standard']['url']
|
||||||
|
|
||||||
|
if thumbnail:
|
||||||
|
thumbnail = uploadImage(thumbnail, 'youtube_' + snippet['resourceId']['videoId'])
|
||||||
|
|
||||||
insertIntoDatabase('YouTube', title, url, author, authorUrl, published, thumbnail, 0)
|
insertIntoDatabase('YouTube', title, url, author, authorUrl, published, thumbnail, 0)
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,7 +111,7 @@ def getTwitterPosts():
|
||||||
image = ''
|
image = ''
|
||||||
if tweet.media:
|
if tweet.media:
|
||||||
image = tweet.media[0].media_url
|
image = tweet.media[0].media_url
|
||||||
image = uploadImage(image, tweet.id_str)
|
image = uploadImage(image, 'twitter_' + tweet.id_str)
|
||||||
|
|
||||||
insertIntoDatabase('Twitter', content, url, author, authorUrl, published, image, originalID)
|
insertIntoDatabase('Twitter', content, url, author, authorUrl, published, image, originalID)
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user