archSpeechReco/src/storageUpload.py

163 lines
5.0 KiB
Python
Raw Normal View History

2019-12-15 20:56:51 +01:00
from google.cloud import storage
import sys
import urllib
from pymongo import MongoClient
from bson.objectid import ObjectId
import os
import datetime
2020-01-06 12:50:46 +01:00
from subprocess import run,DEVNULL
import argparse
2019-12-15 20:56:51 +01:00
2020-01-06 12:50:46 +01:00
def main(args):
2019-12-15 20:56:51 +01:00
uri = "mongodb://speechRecoUser:speech!reco@localhost/archSpeechReco"
dbName = "archSpeechReco"
colName = "moviesMeta"
bucket = 'archspeechreco'
col = getMongoCollection(colName,dbName,uri)
2020-01-06 12:50:46 +01:00
fileFormat = args.format
if (fileFormat == 'mp4'):
uploadMp4(col,bucket)
elif (fileFormat == 'wav'):
uploadWave(col,bucket)
2019-12-15 20:56:51 +01:00
2020-01-06 12:50:46 +01:00
def uploadMp4(col,bucket):
2019-12-15 20:56:51 +01:00
toUpload = getUploadList(col)
for i in toUpload:
fileName = ObjectId(i['_id'])
getVid( i['url'], ObjectId( i['_id'] ) )
2020-01-06 12:50:46 +01:00
upload_blob(bucket, "{}.mp4".format(fileName), "mp4/{}.mp4".format(fileName),col,"Mp4")
2019-12-15 20:56:51 +01:00
try:
os.remove("{}.mp4".format(fileName))
except:
print("{}.mp4 has NOT been removed".format(fileName))
else:
print("{}.mp4 has been removed".format(fileName))
2020-01-06 12:50:46 +01:00
def uploadWave(col,bucket):
toUpload = getWavUploadList(col)
for i in toUpload:
fileName = ObjectId(i['_id'])
getVid( i['url'], ObjectId( i['_id'] ) )
getWave("{}.mp4".format(fileName))
upload_blob(bucket, "{}.wav".format(fileName), "wave/{}.wav".format(fileName),col,"Wav")
try:
os.remove("{}.wav".format(fileName))
except:
print("{}.wav has NOT been removed".format(fileName))
else:
print("{}.wav has been removed".format(fileName))
def upload_blob(bucket_name, source_file_name, destination_blob_name,col,fileFormat):
2019-12-15 20:56:51 +01:00
"""Uploads a file to the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
try:
2020-01-06 12:50:46 +01:00
blob.upload_from_filename(source_file_name)
2019-12-15 20:56:51 +01:00
except:
print("gcs upload failed")
else:
2020-01-06 12:50:46 +01:00
print('File {}.{} uploaded to {}.'.format(
2019-12-15 20:56:51 +01:00
source_file_name,
2020-01-06 12:50:46 +01:00
fileFormat,
2019-12-15 20:56:51 +01:00
destination_blob_name))
now = datetime.datetime.now()
try:
col.update_one(
2020-01-06 12:50:46 +01:00
{"_id": ObjectId(source_file_name.split('.')[0])},
2019-12-15 20:56:51 +01:00
{"$set":{
2020-01-06 12:50:46 +01:00
"gcs{}".format(fileFormat):{
2019-12-15 20:56:51 +01:00
"location":destination_blob_name,
"uploadDate":now.strftime("%Y-%m-%d %H:%M:%S")
}
}
}
)
except:
print("mongo update failed")
else:
print("mongo update OK")
def getMongoCollection(colName,dbName,uri):
client = MongoClient(uri)
db = client[dbName]
col = db[colName]
return col
def getUploadList(col):
pipeline = []
#$match phase, filetr documents withour gcs field - movies not uploaded to gcs
pipeline.append({"$match": {
2020-01-06 12:50:46 +01:00
"gcsMp4": {"$exists": False}
2019-12-15 20:56:51 +01:00
}
})
#project phase, show only url and _id keys
pipeline.append({"$project": {
"url": { "$concat": [ "http://repozytorium.fn.org.pl/",{"$arrayElemAt": [ "$mp4",0 ]}] }
}
})
#skip first N documents
#pipeline.append({"$skip":362})
#fetch only N documents
#pipeline.append({"$limit":20})
return col.aggregate(pipeline)
2020-01-06 12:50:46 +01:00
def getWavUploadList(col):
pipeline = []
#$match phase, filetr documents withour gcs field - movies not uploaded to gcs
pipeline.append({"$match": {
"gcsWav": {"$exists": False}
}
})
#project phase, show only url and _id keys
pipeline.append({"$project": {
"url": { "$concat": [ "http://repozytorium.fn.org.pl/",{"$arrayElemAt": [ "$mp4",0 ]}] }
}
})
#skip first N documents
#pipeline.append({"$skip":362})
#fetch only N documents
#pipeline.append({"$limit":500})
return col.aggregate(pipeline)
2019-12-15 20:56:51 +01:00
def getVid(url,out):
try:
urllib.request.urlretrieve(url, "{}.mp4".format(out))
except:
print("wrong URL, can't download")
2020-01-06 12:50:46 +01:00
def getWave(filename):
try:
run(['ffmpeg','-i', filename, '-vn', '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '1', filename.replace("mp4","wav")],stdout=DEVNULL)
except:
print("problem with ffmpeg")
else:
try:
os.remove(filename)
except:
print("{} has NOT been removed".format(filename))
else:
print("{} has been removed".format(filename))
2019-12-15 20:56:51 +01:00
if __name__ == '__main__':
2020-01-06 12:50:46 +01:00
parser = argparse.ArgumentParser(description='GCS uploader')
parser.add_argument("--format", default='mp4', help="format to fetch and upload, [mp4, wav]")
args = parser.parse_args()
main(args)
2019-12-15 20:56:51 +01:00