# -*- coding: utf-8 -*-
# @Time : 2024/1/15 14:37
# @Author : hjcui
# @Site :
# @File : mongodb.py
# @Software: PyCharm
import time,os
import datetime
import pymongo
from collections import Counter
from pymongo import MongoClient
import requests
dbName = 'target_507'
tele_dir = r'.\telephone'
# 查询120个集合
for i in range(0,120):
num = '%04d' % i
table = 'speaker_{}'.format(num)
mongo = MongoClient("mongodb://172.5.20.4:27017/")
database = mongo[dbName]
collection = database[table]
query = {}
# 查询列表长度大于2的记录
query["$where"] = u"this.languageList.length > 2"
query["createTime"] = {
u"$gt": 1705593600
}
# 根据文档中的字段查询,projection在python脚本中的用法
projection = {}
projection["telNum"] = 1.0
projection["audioMetaList"] = 1.0
projection["createTime"] = 1.0
projection["languageList"] = 1.0
sort = [("createTime", 1)]
cursor = collection.find(query, projection=projection, sort=sort).limit(1000)
for metalist in cursor:
root = metalist['audioMetaList']
for num in range(len(root)):
print(metalist['telNum'],metalist['createTime'],root[num]['filepath'])
link = root[num]['filepath']
filename = os.path.basename(root[num]['filepath'])
tele_num = metalist['telNum']
storage_path = os.path.join(tele_dir,tele_num)
response = requests.get(url=link)
if not os.path.exists(storage_path):
os.makedirs(storage_path)
with open(os.path.join(storage_path,filename),'wb') as ff:
ff.write(response.content)