-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:lAngelP/uCode2018
- Loading branch information
Showing
8 changed files
with
10,351 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,170 @@ | ||
from pymongo import MongoClient | ||
import os | ||
import json | ||
def connect(): | ||
client = MongoClient('mongodb://root:toor@localhost:27017/') | ||
db = client.ucode2018 | ||
return db | ||
|
||
def insertPerson_Localization(db, location, nick, name): | ||
location_id = db.lugares.find_one_and_update( | ||
{"sitio": location}, | ||
{"$set": {"sitio": location}}, | ||
upsert=True, returnNewDocument=True | ||
) | ||
persona_id = db.personas.find_one_and_update( | ||
{"nick": nick}, | ||
|
||
def insert_Localizacion(db, lat, long): | ||
location_id = None | ||
while not location_id: | ||
location_id = db.lugares.find_and_modify( | ||
{"sitio": {"lat": lat, "long":long}}, | ||
{"$set": {"sitio": {"lat": lat, "long":long}}, | ||
}, | ||
upsert=True, returnNewDocument=True, safe=True | ||
) | ||
return location_id | ||
|
||
def insert_Localizacion_persona(db,location_id, persona_id): | ||
db.personas.update( | ||
{"_id": persona_id}, | ||
{"$set": { | ||
"nick": nick, | ||
"name": name, | ||
"location": str(location_id['_id'])}}, | ||
upsert=True, returnNewDocument=True | ||
"location": location_id}}, | ||
) | ||
db.localizacion.update( | ||
{"_id": str(location_id['_id'])}, | ||
db.personas.update( | ||
{"_id": persona_id}, | ||
{"$addToSet": { | ||
"population": nick}}, | ||
"location": {"$each": location_id}}}, | ||
) | ||
return location_id, persona_id | ||
|
||
def insertar_gusto(db, like, hashtags, persona_id): | ||
gusto_id = db.gusto.find_one_and_update( | ||
{"name": like}, | ||
def insert_Persona(db, nick, name, profile_img, followers, friends): | ||
persona_id = None | ||
while not persona_id: | ||
persona_id = db.personas.find_and_modify( | ||
{"nick": nick}, | ||
{"$set": { | ||
"nick": nick, | ||
"name": name, | ||
"sentinent": 0, | ||
"profile_img":profile_img, | ||
"followers":followers, | ||
"friends":friends | ||
}}, | ||
upsert=True, returnNewDocument=True, safe=True | ||
) | ||
|
||
return persona_id | ||
|
||
def insertar_gusto(db, like, hashtags): | ||
gusto_id = None | ||
while not gusto_id: | ||
gusto_id = db.gusto.find_and_modify( | ||
{"name": like}, | ||
{"$addToSet": { | ||
'hashtags': {'$each': hashtags}, | ||
}, | ||
"$set": { | ||
"name": like}}, | ||
upsert=True, returnNewDocument=True, safe=True | ||
) | ||
return gusto_id | ||
|
||
def insertar_persona_gusto(db, gusto_id, persona_id, sentient): | ||
db.gustos.update( | ||
{"_id": gusto_id}, | ||
{"$push": { | ||
"people": persona_id}} | ||
) | ||
result = db.personas.find_one({"_id": persona_id}) | ||
|
||
db.personas.update( | ||
{"_id": persona_id}, | ||
{"$addToSet": { | ||
'hashtags': hashtags, | ||
'followers': str(persona_id['_id'])}, | ||
"$set": { | ||
"name": like}}, | ||
upsert=True, returnNewDocument=True | ||
"sentient": result['sentinent']+sentient, | ||
"like": gusto_id}} | ||
) | ||
return gusto_id | ||
|
||
def insertar_evento(db, event, gusto_id,date, lugar_id): | ||
|
||
evento_id = db.eventos.find_one_and_update( | ||
def insertar_evento(db, event, gusto_ids,date, lugar_id): | ||
|
||
evento_id = db.eventos.find_and_modify( | ||
{"name": event}, | ||
{"$addToSet": { | ||
'like': [str(gusto['_id']) for gusto in gusto_id]}, | ||
'like': {"$each": [gusto_ids]}}, | ||
"$set": { | ||
"name": event, | ||
"date": date, | ||
"lugar": str(lugar_id['_id'])}}, | ||
"lugar": lugar_id}}, | ||
upsert=True, returnNewDocument=True | ||
) | ||
db.localizacion.update( | ||
{"_id": str(lugar_id['_id'])}, | ||
{"_id": lugar_id}, | ||
{"$addToSet": { | ||
"events": str(evento_id['_id'])}}, | ||
"events": {"$each":[evento_id]}}}, | ||
) | ||
for gusto in gusto_id: | ||
print(gusto) | ||
for gusto in gusto_ids: | ||
#print(gusto) | ||
db.gustos.update( | ||
{"_id": str(gusto['_id'])}, | ||
{"_id": gusto}, | ||
{"$addToSet": { | ||
"events": str(evento_id['_id'])}}, | ||
"events": {"$each":[evento_id]}}}, | ||
) | ||
return evento_id | ||
|
||
|
||
def add_persona_evento(db, persona_id, evento_id): | ||
db.eventos.update( | ||
{"_id": str(evento_id['_id'])}, | ||
{"_id": evento_id}, | ||
{"$addToSet": { | ||
'people': str(persona_id['_id'])}}, | ||
'people': {"$each":[persona_id]}}}, | ||
upsert=True) | ||
|
||
db.personas.update( | ||
{"_id": str(persona_id['_id'])}, | ||
{"_id": persona_id}, | ||
{"$addToSet": { | ||
"events": str(evento_id['_id'])}}, | ||
"events": {"$each":[evento_id]}}}, | ||
) | ||
|
||
|
||
def add_imagen(db, img, gusto_id): | ||
gusto_id = db.gusto.update( | ||
{"_id": str(gusto_id['_id'])}, | ||
{"_id": str(gusto_id)}, | ||
{"$addToSet": { | ||
'img': img}}, | ||
'img': {"$each":[img]}}}, | ||
upsert=True) | ||
|
||
|
||
def load_data(path, db): | ||
with open(path, 'r', encoding="utf8") as file: | ||
data = json.load(file) | ||
like = data["team"].split('.')[0] | ||
print(like) | ||
for user in data["data"]: | ||
|
||
nick = user["user"]["name"] | ||
name = user["user"]["nick"] | ||
profile_img = user["user"]["profile_img"] | ||
followers = user["user"]["followers"] | ||
friends = user["user"]["friends"] | ||
hashtag = user["hashtags"] | ||
sentient = user["sentient"] | ||
persona_id = insert_Persona(db, nick, name, profile_img, followers, friends) | ||
#print(persona_id) | ||
if user["user"]["location"] != {}: | ||
lat = user["user"]["location"]["lat"] | ||
long = user["user"]["location"]["long"] | ||
location_id = insert_Localizacion(db, lat, long) | ||
insert_Localizacion_persona(db, [location_id['_id']], persona_id['_id']) | ||
|
||
gusto_id = insertar_gusto(db,like,hashtag) | ||
#print(gusto_id) | ||
insertar_persona_gusto(db,gusto_id['_id'],persona_id['_id'],sentient) | ||
|
||
def insert_location_from_data(path, db): | ||
with open(path, 'r', encoding="utf8") as file: | ||
data = json.load(file) | ||
for user in data["data"]: | ||
lat = user["user"]["location"]["lat"] | ||
long = user["user"]["location"]["long"] | ||
nick = user["user"]["name"] | ||
user_id = db.personas.find_one({"nick": nick}) | ||
location_id = insert_Localizacion(db, lat, long) | ||
insert_Localizacion_persona(db, [location_id['_id']], user_id['_id']) | ||
|
||
if __name__ == "__main__": | ||
db = connect() | ||
with open('../test-search/data/NBA/BrooklynNets.json', 'r') as file: | ||
data = json.load(file) | ||
location = 'testing_location' | ||
nick = 'testing_nick' | ||
name = 'testing_name' | ||
like = 'testing_gusto' | ||
date = '20181102' | ||
event = 'test_event' | ||
id = 'id_test' | ||
hashtag = ['#hashtag', '#hash2'] | ||
location, persona = insertPerson_Localization(db,location, nick, name) | ||
gusto = insertar_gusto(db, like, hashtag, persona) | ||
insertar_evento(db,event, [gusto] ,date, location) | ||
files = os.listdir('..\\DataAnalyser\\output\\') | ||
for file in files: | ||
load_data('../DataAnalyser/output/'+file, db) |
Oops, something went wrong.