# abandoned because merges with pre_process.py
import pymongo
import requests
#import pandas
from bson.json_util import dumps

MONGO_URI = '192.168.3.11:27017'
MONGO_DB = 'rent'
MONGO_USER = 'admin'
MONGO_PW = '<PASSWORD>'

client = None
collection = None

def get_collection():
    global collection
    global client
    client = pymongo.MongoClient(MONGO_URI, username=MONGO_USER, password=MONGO_PW)
    db = client[MONGO_DB]
    collection = db['House']

def add_lnglat():
    houses = list(collection.find())
    for house in houses:
        if 'longitude' in house:
            collection.update({'link': house['link']}, {'$set': {'lnglat': [house['longitude'], house['latitude']]}})

def export_json():
    cursor = collection.find({})
    with open('collection.json', 'w', encoding='utf-8') as file:
        i = 0
        for document in cursor:
            if i != 0:
                file.write('||')
            del document["lnglat"]
            del document["_id"]
            document["houseNum"] = len(document["houseList"])
            del document["houseList"]
            s = dumps(document, indent=1, ensure_ascii=False)#.encode('utf8')
            file.write(str(s))
            i += 1

if __name__ == '__main__':
    # get_collection()
    # print('Processing...')
    # add_lnglat()
    # print('Done.')
    get_collection()
    export_json()
# abandoned because merges with pre_process.py
import pymongo
import requests
#import pandas
from bson.json_util import dumps

MONGO_URI = '192.168.3.11:27017'
MONGO_DB = 'rent'
MONGO_USER = 'admin'
MONGO_PW = '<PASSWORD>'

client = None
collection = None

def get_collection():
    global collection
    global client
    client = pymongo.MongoClient(MONGO_URI, username=MONGO_USER, password=MONGO_PW)
    db = client[MONGO_DB]
    collection = db['House']

def add_lnglat():
    houses = list(collection.find())
    for house in houses:
        if 'longitude' in house:
            collection.update({'link': house['link']}, {'$set': {'lnglat': [house['longitude'], house['latitude']]}})

def export_json():
    cursor = collection.find({})
    with open('collection.json', 'w', encoding='utf-8') as file:
        i = 0
        for document in cursor:
            if i != 0:
                file.write('||')
            del document["lnglat"]
            del document["_id"]
            document["houseNum"] = len(document["houseList"])
            del document["houseList"]
            s = dumps(document, indent=1, ensure_ascii=False)#.encode('utf8')
            file.write(str(s))
            i += 1

if __name__ == '__main__':
    # get_collection()
    # print('Processing...')
    # add_lnglat()
    # print('Done.')
    get_collection()
    export_json()
# abandoned because merges with pre_process.py #import pandas #.encode('utf8') # get_collection() # print('Processing...') # add_lnglat() # print('Done.')