# find users with lots of restaurante reviews
# in this case, find one user to start with,
# for each restaurant
# then, figure out how to do it for all users that have a certain number of reviews.



'''
 db.users.find({user_id: "G-uVT9JxNMbrPApvuVO6pg" }  )


  db.users.find({review_count:{$gt:10000}})



  db.academic_reviews.find( {user_id: "G-uVT9JxNMbrPApvuVO6pg"})
'''

# generate the model on 80% of the reviews


# for the last 20% of reviews, find all reviews not from that user




#############

from pymongo import MongoClient
from datetime import datetime
import json
import pdb
import csv

'''
create a list of all users that have reviews over 1000
'''
conn = MongoClient()

db = conn.get_database('yelpbiz')


c = db.get_collection('academic_reviews')
u = db.get_collection('users')



biguser = []

for obj in u.find({'review_count':{'$gt':30}}):
    print(obj['user_id'])
    biguser.append(obj['user_id'])








'''
get the business ids of the places they review
'''



for user in biguser:
    ulist = []
    for obj in c.find({'$and':[{'user_id':user,'stars':{'$gte':4}}]):
        ulist.append(obj)
        userreview.update({user:ulist})
# this throws an error because the first key is a bson object
# gotta fix this

keys =userreview.keys()

for key in keys:
    for review in userreview[key]:
        if '_id' in review: del review['_id']


with open('filter_reviews.json', 'w') as outfile:
    json.dump(userreview, outfile)






'''
next step - build a list of reviews for each business
'''

'''
then build a "generative " data set from each user and a test set
'''


'''
scrap code


conn = MongoClient()

print(conn.database_names())

db = conn.get_database('yelpbiz')

print(db.collection_names())


c = db.get_collection('academic_reviews')
u = db.get_collection('users')


states =c.distinct('state')
print(states)
test = db.academic_reviews.find_one()
print(test)

guser =db.academic_reviews.find({'user_id': 'G-uVT9JxNMbrPApvuVO6pg'})
print(guser)

print(db.academic_reviews.index_information())




for obj in c.find( ):
        try:
            if len(reviews_dict[obj['state']]) > num_reviews:
                continue
        except KeyError:
            pass
        if datetime.strptime(obj['date'][0:4], '') >= threshold_year:
            del obj['_id']
            try:
                reviews_dict[obj['state']].append(obj)
            except KeyError:
                reviews_dict[obj['state']]=[obj]
        else:
            pass



#pdb.set_trace()
'''

@@ -984,17 +984,32 @@
 ew%0A'''%0A%0A
+userreview = %7B%7D
 %0A
-
 %0Afor use
@@ -1393,12 +1393,10 @@
 en('
-filt
+us
 er_r
@@ -1400,17 +1400,27 @@
 r_review
-s
+_dictionary
 .json', 
@@ -1471,24 +1471,439 @@
 utfile)%0A%0A%0A%0A%0A
+biznames =%5B%5D%0A%0Afor key in keys:%0A    for review in userreview%5Bkey%5D:%0A       biznames.append(review%5B'business_id'%5D)%0A%0Arestreview = %7B%7D%0A%0Afor restaurant in biznames:%0A    rlist = %5B%5D%0A    for obj in c.find(%7B'$and':%5B%7B'user_id':restaurant,'stars':%7B'$gte':4%7D%7D%5D):%0A        rlist.append(obj)%0A        restreview.update(restaurant:rlist%7D)%0A%0A%0Awith open('rest_review_dictionary.json', 'w') as outfile:%0A    json.dump(restreview, outfile)%0A
 %0A%0A'''%0Anext s

add for loop to create restuarant json review file