Reputation: 59
I have a Mongodb with documents. Some of them have a link to downloadable audio. It looks basicaly like this:
"_id" : ObjectId("5ece21d39dfe015b29ec4812"),
"date" : "some date",
"path" : "path/to/raw/file",
"has_audio" : true,
"audio_downloaded" : false,
"audio_link" : "some link",
"audio_file" : "None",
My Script should check if "has_audio" is True and "audio_downloaded" is False. This works well. Afterwards it downloads the files and when it finishes it should chance the entrys in the Mongodb:
audio_downloaded: true,
audio_file: "path/to/audiofile.mp3"
This is not working for me, it won't update. Here is my code and what I have tried:
import os
import urllib.request
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["DW"]
col = database["document"]
for entry in col.find({"has_audio": {"$eq": True}, "audio_downloaded": {"$ne": True}}):
audio_link = entry['audio_link']
file_path = entry['path']
file_path = os.path.dirname(file_path) + "/"
audio_file_name = os.path.basename(audio_link)
audio_file_path = file_path + audio_file_name
urllib.request.urlretrieve(audio_link, audio_file_path)
col.update_one({'audio_downloaded': entry['audio_downloaded']}, {'$set': {'audio_downloaded': True}})
col.update_one({'audio_file': entry['audio_file']}, {"$set": {"audio_file": audio_file_path}})
Upvotes: 0
Views: 559
Reputation: 8834
The filter on your update_one()
statement will update the first matching record decided by the database, unrelated to the entry
record.
Replace your last two lines with following which will match on the unique _id
field:
col.update_one({'_id': entry.get('_id')}, {'$set': {'audio_downloaded': True, "audio_file": audio_file_path}})
Upvotes: 1