flake8 format

main
Bob Mottram 2020-04-03 16:55:55 +00:00
parent e2e2db91d4
commit 840e726753
1 changed files with 95 additions and 81 deletions

View File

@ -10,8 +10,6 @@ from blurhash import blurhash_encode as blurencode
from PIL import Image
import numpy
import os
import sys
import json
import datetime
from hashlib import sha1
from auth import createPassword
@ -19,6 +17,7 @@ from shutil import copyfile
from shutil import rmtree
from shutil import move
def replaceYouTube(postJsonObject: {}) -> None:
"""Replace YouTube with invidio.us
This denies Google some, but not all, tracking data
@ -30,7 +29,9 @@ def replaceYouTube(postJsonObject: {}) -> None:
if 'www.youtube.com' not in postJsonObject['object']['content']:
return
postJsonObject['object']['content'] = \
postJsonObject['object']['content'].replace('www.youtube.com','invidio.us')
postJsonObject['object']['content'].replace('www.youtube.com',
'invidio.us')
def removeMetaData(imageFilename: str, outputFilename: str) -> None:
"""Attempts to do this with pure python didn't work well,
@ -44,47 +45,55 @@ def removeMetaData(imageFilename: str,outputFilename: str) -> None:
print('Removing metadata from ' + outputFilename + ' using mogrify')
os.system('/usr/bin/mogrify -strip '+outputFilename)
def getImageHash(imageFilename: str) -> str:
return blurencode(numpy.array(Image.open(imageFilename).convert("RGB")))
def isMedia(imageFilename: str) -> bool:
permittedMedia=['png','jpg','gif','webp','mp4','ogv','mp3','ogg']
permittedMedia = ('png', 'jpg', 'gif', 'webp',
'mp4', 'ogv', 'mp3', 'ogg')
for m in permittedMedia:
if imageFilename.endswith('.' + m):
return True
print('WARN: ' + imageFilename + ' is not a permitted media type')
return False
def createMediaDirs(baseDir: str, mediaPath: str) -> None:
if not os.path.isdir(baseDir + '/media'):
os.mkdir(baseDir + '/media')
if not os.path.isdir(baseDir + '/' + mediaPath):
os.mkdir(baseDir + '/' + mediaPath)
def getMediaPath() -> str:
currTime = datetime.datetime.utcnow()
weeksSinceEpoch = int((currTime - datetime.datetime(1970, 1, 1)).days / 7)
return 'media/' + str(weeksSinceEpoch)
def getAttachmentMediaType(filename: str) -> str:
"""Returns the type of media for the given file
image, video or audio
"""
mediaType = None
imageTypes=['png','jpg','jpeg','gif','webp']
imageTypes = ('png', 'jpg', 'jpeg',
'gif', 'webp')
for mType in imageTypes:
if filename.endswith('.' + mType):
return 'image'
videoTypes=['mp4','webm','ogv']
videoTypes = ('mp4', 'webm', 'ogv')
for mType in videoTypes:
if filename.endswith('.' + mType):
return 'video'
audioTypes=['mp3','ogg']
audioTypes = ('mp3', 'ogg')
for mType in audioTypes:
if filename.endswith('.' + mType):
return 'audio'
return mediaType
def updateEtag(mediaFilename: str) -> None:
""" calculate the etag, which is a sha1 of the data
"""
@ -101,7 +110,7 @@ def updateEtag(mediaFilename: str) -> None:
try:
with open(mediaFilename, 'rb') as mediaFile:
data = mediaFile.read()
except:
except BaseException:
pass
if not data:
@ -112,22 +121,25 @@ def updateEtag(mediaFilename: str) -> None:
try:
with open(mediaFilename + '.etag', 'w') as etagFile:
etagFile.write(etag)
except:
except BaseException:
pass
def attachMedia(baseDir: str,httpPrefix: str,domain: str,port: int, \
postJson: {},imageFilename: str, \
mediaType: str,description: str, \
def attachMedia(baseDir: str, httpPrefix: str, domain: str, port: int,
postJson: {}, imageFilename: str,
mediaType: str, description: str,
useBlurhash: bool) -> {}:
"""Attaches media to a json object post
The description can be None
Blurhash is optional, since low power systems may take a long time to calculate it
Blurhash is optional, since low power systems may take a long
time to calculate it
"""
if not isMedia(imageFilename):
return postJson
fileExtension = None
acceptedTypes=['png','jpg','gif','webp','mp4','webm','ogv','mp3','ogg']
acceptedTypes = ('png', 'jpg', 'gif', 'webp',
'mp4', 'webm', 'ogv', 'mp3', 'ogg')
for mType in acceptedTypes:
if imageFilename.endswith('.' + mType):
if mType == 'jpg':
@ -177,6 +189,7 @@ def attachMedia(baseDir: str,httpPrefix: str,domain: str,port: int, \
return postJson
def archiveMedia(baseDir: str, archiveDirectory: str, maxWeeks=4) -> None:
"""Any media older than the given number of weeks gets archived
"""
@ -194,7 +207,8 @@ def archiveMedia(baseDir: str,archiveDirectory: str,maxWeeks=4) -> None:
for weekDir in dirs:
if int(weekDir) < minWeek:
if archiveDirectory:
move(os.path.join(baseDir+'/media', weekDir),archiveDirectory+'/media')
move(os.path.join(baseDir + '/media', weekDir),
archiveDirectory + '/media')
else:
# archive to /dev/null
rmtree(os.path.join(baseDir + '/media', weekDir))