def seed_tables(): # User for i in range(10): user = User() user.id = i user.displayname = f"testuser{i}" user.email = f"TestEmail{i}@test.com" user.username = f"TestUserName{i}" user.password = bcrypt.generate_password_hash("testpassword").decode("utf-8") db.session.add(user) db.session.commit() print("Seeded Table: user") # Artist artists = [] for i in range(10): artist = Artist() artist.name = f"TestArtistName{i}" db.session.add(artist) artists.append(artist) db.session.commit() print("Seeded Table: artist") # Image for i in range(20): image = Image() image.url = f"TestURL{i}" image.height = choice(range(600, 1200)) image.width = choice(range(600, 1200)) db.session.add(image) db.session.commit() print("Seeded Table: images") def moods_table_creation(): moods = Moods() moods.amusement = choice(range(1, 101)) moods.joy = choice(range(1, 101)) moods.beauty = choice(range(1, 101)) moods.relaxation = choice(range(1, 101)) moods.sadness = choice(range(1, 101)) moods.dreaminess = choice(range(1, 101)) moods.triumph = choice(range(1, 101)) moods.anxiety = choice(range(1, 101)) moods.scariness = choice(range(1, 101)) moods.annoyance = choice(range(1, 101)) moods.defiance = choice(range(1, 101)) moods.feelingpumped = choice(range(1, 101)) db.session.add(moods) db.session.commit() return moods # Tracks for i in range(30): track = Tracks() track.trackname = f"TestTrackName{i}" track.artist_id = choice(artists).id track.moods_id = moods_table_creation().id track.trackurl = f"TestTrackURL{i}" db.session.add(track) db.session.commit() print("Seeded Table: tracks")
def organize(database, workdir, output, size): """ (str, str, str, str) -> Bool Method used to execute organization of images """ filehandler = open(output, "a") logging.info("CONNECTING DATABASE {}".format(database)) conn = sqlite3.connect(database) cursor = conn.cursor() plants = [] scriptPlants = "" scriptDiseases = "" scriptImages = "" for row in cursor.execute( "SELECT id, crop_common_name, crop_scientific_name, disease_common_name, disease_scientific_name, url, description, metadata FROM ANNOTATIONS;" ): oldAnnotation = OldAnnotation(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]) if (oldAnnotation.url == "" or oldAnnotation.cropScientificName == ""): continue indexPlant = searchPlantByScientificName(plants, oldAnnotation.cropCommonName) plant = Plant() if (indexPlant == -1): plant = Plant(scientificName=oldAnnotation.cropScientificName, commonName=oldAnnotation.cropCommonName, diseases=[]) logging.info("CREATING {}".format(plant.commonName).replace( " ", "_").replace(";", "").replace("(", "_").replace( ")", "_").replace("<i>", "").replace("</i>", "")) os.system("mkdir -p " + workdir + "/" + plant.commonName.replace( " ", "_").replace(";", "").replace("(", "_").replace( ")", "_").replace("<i>", "").replace("</i>", "")) #filehandler.write("INSERT INTO PLANTS(scientific_name, common_name) VALUES ('{}', '{}')\n".format(plant.scientificName, plant.commonName)) scriptPlants += "INSERT INTO PLANTS(scientific_name, common_name) VALUES ('{}', '{}');\n".format( plant.scientificName, plant.commonName) else: logging.info("index: {} - plant: {}".format( str(indexPlant), plants[indexPlant].scientificName)) plant = plants[indexPlant] disease = Disease(plant=plant, commonName=oldAnnotation.diseaseCommonName, scientificName=oldAnnotation.diseaseScientificName) if (oldAnnotation.diseaseCommonName == "" or oldAnnotation.diseaseScientificName == "" or "Healthy" in oldAnnotation.description or "healthy" in oldAnnotation.description): disease.scientificName = "healthy" disease.commonName = "healthy" indexDisease = searchDiseaseByScientificName(disease.plant, disease.scientificName) logging.info("DISEASE: {} - {} - {}".format(disease.scientificName, plant.scientificName, indexDisease)) if (indexDisease == -1): logging.info("CREATING {}/{}".format( plant.commonName.replace(" ", "_").replace(";", "").replace( "(", "_").replace(")", "_").replace("<i>", "").replace("</i>", ""), disease.scientificName.replace(" ", "_").replace( ";", "").replace("(", "_").replace(")", "_").replace( "<i>", "").replace("</i>", ""))) os.system("mkdir -p " + workdir + "/" + plant.commonName.replace( " ", "_").replace(";", "").replace("(", "_").replace( ")", "_").replace("<i>", "").replace("</i>", "") + "/" + disease.scientificName.replace(" ", "_").replace( ";", "").replace("(", "_").replace(")", "_").replace( "<i>", "").replace("</i>", "")) scriptDiseases += "INSERT INTO DISEASES(id_plant, scientific_name, common_name) VALUES ((SELECT id FROM PLANTS WHERE scientific_name = '{}' LIMIT 1),'{}', '{}');\n".format( plant.scientificName, disease.scientificName, disease.commonName) #filehandler.write("INSERT INTO DISEASES(id, scientific_name, common_name) VALUES ((SELECT id FROM PLANTS WHERE scientific_name = '{}' LIMIT 1),'{}', '{}')\n".format(disease.plant.scientificName, disease.scientificName, disease.commonName)) else: disease = plant.diseases[indexDisease] image = Image(disease=disease, url=oldAnnotation.url.replace("<i>", "").replace("</i>", ""), description=oldAnnotation.description, source=oldAnnotation.metadata) regex = re.compile("[\w]+/[\w,;\-]*\/([\w\.;\-,]+)+") logging.info(image.url) if (not regex.match(image.url) == None): logging.info(image.url) image.url = regex.match(image.url).group(1) if ("large" in image.url): continue else: logging.info(image.url) continue if ("large" in disease.scientificName): continue logging.info("CREATING {}/{}/{} ".format( plant.commonName.replace(" ", "_"), disease.scientificName.replace(" ", "_"), image.url.replace(" ", "_"))) os.makedirs(workdir + "/" + plant.commonName.replace(" ", "_").replace( ";", "").replace("(", "_").replace(")", "_").replace( "<i>", "").replace("</i>", "") + "/" + disease.scientificName.replace(" ", "_").replace( ";", "").replace("(", "_").replace(")", "_").replace( "<i>", "").replace("</i>", "") + "/", exist_ok=True) dir1 = workdir + "/" + plant.commonName.replace(" ", "_").replace( "(", "_").replace(")", "_").replace("<i>", "").replace( "</i>", "") + "/" + image.url dir2 = workdir + "/" + plant.commonName.replace(" ", "_").replace( ";", "").replace( "(", "_").replace(")", "_").replace("<i>", "").replace( "</i>", "") + "/" + disease.scientificName.replace( " ", "_").replace(";", "").replace("(", "_").replace( ")", "_").replace("<i>", "").replace( "</i>", "") + "/" + image.url logging.info(dir1) logging.info(dir2) if ("large" in dir2): continue try: shutil.copyfile(dir1, dir2) except FileNotFoundError: continue #filehandler.write("INSERT INTO IMAGES(id_disease, url, description, source) VALUES ((SELECT id FROM DISEASES WHERE scientific_name = '{}' AND id_plant = (SELECT id FROM PLANTS WHERE scientific_name = '{}' LIMIT 1) LIMIT 1), '{}', '{}', '{}')\n".format(image.disease.scientificName, image.disease.plant.scientificName, image.url, image.description, image.source)) scriptImages += "INSERT INTO IMAGES(id_disease, url, description, source, size) VALUES ((SELECT id FROM DISEASES WHERE scientific_name = '{}' AND id_plant = (SELECT id FROM PLANTS WHERE scientific_name = '{}' LIMIT 1) LIMIT 1), '{}', '{}', '{}', (SELECT id FROM TYPES WHERE value='{}' AND description='image-size' LIMIT 1));\n".format( disease.scientificName, plant.scientificName, image.url, image.description, image.source, size) disease.images.append(image) if (indexDisease == -1): plant.diseases.append(disease) else: plant.diseases[indexDisease] = disease if (indexPlant == -1): plants.append(plant) else: plants[indexPlant] = plant filehandler.write(scriptPlants) filehandler.write(scriptDiseases) filehandler.write(scriptImages) filehandler.close() return True