-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdbSetup.js
More file actions
188 lines (167 loc) · 5.13 KB
/
dbSetup.js
File metadata and controls
188 lines (167 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require('dotenv').config();
var express = require('express');
var db = require('./models');
var request = require('request');
var cheerio = require('cheerio');
var router = express.Router();
// TO DO:
// Make these dryer, use async to make db easier to setup in one go
// STEP 1 FOR SCRAPING PLANT CARE DATA
// Remove comments from below to scrape all plant care rankings from UGA and input into db
// TO DO: Need more hooks here to fix some typos and overly long names from original data source
// request('http://extension.uga.edu/publications/detail.html?number=B1318', function(error, response, data){
// var $ = cheerio.load(data);
// for (var i = 4; i <= 231; i++) {
// var plantRow = 'body > main > div > div.large-8.columns.pub > table:nth-child(179) > tbody > tr:nth-child(';
// var botanicalName = $(plantRow + i +') > td:nth-child(1)').text();
// var commonName = $(plantRow + i +') > td:nth-child(2)').text();
// var light = $(plantRow + i +') > td:nth-child(3)').text();
// var temperature = $(plantRow + i +') > td:nth-child(4)').text();
// var humidity = $(plantRow + i +') > td:nth-child(5)').text();
// var water = $(plantRow + i +') > td:nth-child(6)').text();
// var soil = $(plantRow + i +') > td:nth-child(7)').text();
// db.plant.findOrCreate({
// name: commonName,
// botanicalName: botanicalName,
// light: light,
// temperature: temperature,
// humidity: humidity,
// water: water,
// soil: soil
// });
// }
// });
// STEP 2 - FOR INPUTTING IMAGES
// Remove comments from below and run this
// May want to run for botanicalName and name to get maximum results
// for (var i = 1; i <= 227; i++) {
// db.plant.findOne({
// where: {id: i}
// }).then(function(plant){
// request('https://en.wikipedia.org/wiki/' + plant.botanicalName, function(error, response, data){
// var $ = cheerio.load(data);
// var imageSrc = $('#mw-content-text > div > table.infobox.biota > tbody > tr:nth-child(2) > td > a > img').attr('src');
// if (imageSrc && !plant.imageUrl){
// plant.imageUrl = 'http:' + imageSrc;
// plant.save();
// console.log(plant.name,"image url is",plant.imageUrl);
// }
// });
// });
// }
// The below was used when I had weird dupes in the db
// for (var i = 229; i <= 456; i++) {
// var currentPlantId = i;
// db.plant.destroy({
// where: { id: currentPlantId }
// }).then(function(deleted){
// console.log("Plant deleted",deleted);
// });
// }
// STEP 3 - FOR CREATING TAGS
// Do this AFTER creating plant database
// Remove comments and run the below once to generate initial tags
// db.tag.findOrCreate({
// where: {
// content: 'palm'
// }
// });
// db.tag.findOrCreate({
// where: {
// content: 'cactus & succulent'
// }
// });
// db.tag.findOrCreate({
// where: {
// content: 'flowering'
// }
// });
// db.tag.findOrCreate({
// where: {
// content: 'ivy'
// }
// });
// db.tag.findOrCreate({
// where: {
// content: 'fern'
// }
// });
// db.tag.findOrCreate({
// where: {
// content: 'orchid'
// }
// });
// db.tag.findOrCreate({
// where: {
// content: 'foliage'
// }
// });
// db.tag.findOrCreate({
// where: {
// content: 'bromeliad'
// }
// });
// STEP 4 - Add tags to plants
// Remove comments from below to loop through
// Since currently you have to make manual changes in this function
// var addTags = function(tagName) {
// db.plant.findAll().then(function(plant){
// plant.forEach(function(plant){
// var plantName = JSON.stringify(plant.name).toLowerCase();
// var plantId = plant.id;
// var addPlantTag = plantName.includes(tagName);
// if (addPlantTag){
// console.log(plant.name,'includes'+ tagName +'and ID is',plantId);
// db.plant.findOne({
// where: { id: plantId }
// }).then(function(plant, found){
// db.tag.findOrCreate({
// where: { content: tagName }
// }).spread(function(tag, found){
// plant.addTag(tag);
// console.log(tag,'added to',plant);
// });
// });
// }
// });
// });
// }
// addTags('palm');
// addTags('ivy');
// addTags('fern');
// addTags('orchid');
// addTags('bromeliad');
// STEP 5 - Use soil types to loop through and add more tags
// TO DO - Make this more dry and reusable! Same as step 2 where you need to adjust manually
// Flowering = scale 1, id = 2
// Foliage = scale 2, id = 9
// Bromeliads = scale 3, id = 11
// Orchids = scale 4, id = 8
// Succulents & cactus = scale 5, id = 10
// Ferns = scale 6, id = 4
// var soilType = "6";
// var plantsArr = [];
// db.plant.findAll({where: { soil: soilType }}).then(function(data){
// for (var i = 1; i < data.length; i++) {
// console.log(data[i].id);
// var currentPlantId = data[i].id;
// plantsArr.push(currentPlantId);
// }
// console.log(plantsArr);
// for (var i = 0; i <= plantsArr.length; i++) {
// db.plant.findOrCreate({
// where: {
// id: plantsArr[i]
// }
// }).spread(function(plant, created) {
// db.tag.findOrCreate({
// where: { id: 4 }
// }).spread(function(tag, created) {
// plant.addTag(tag).then(function(tag) {
// console.log(tag, "added to", plant);
// });
// });
// });
// }
// });
module.exports = router;