-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUpload.py
316 lines (290 loc) · 11.3 KB
/
FileUpload.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Take data and make into object
# TAKE OBJECT AND PUT IN MONGODB
# Written by Michael Kaufman 2017 c3po Independent Study
import json
import bson
from bson import json_util
#functions
def toJson(data):
"""Convert Mongo object(s) to JSON"""
return json.dumps(data, default=json_util.default)
def RecoverandLog():
return 0
#functions
def toJson(data):
"""Convert Mongo object(s) to JSON"""
return json.dumps(data, default=json_util.default)
# DESIGNED FOR A LOCAL FILE
# USED FOR LOCAL FILE DATA - RETURNS FILE LINES
def read_ids(fileName):
fileLines = read_txt_to_lines(fileName)
dataLines = []
finalLines = []
# Format data for python.
i=0
for line in fileLines:
l = line.strip("\n").replace(" ", "").split('\t')
dataLines.append(l)
finalLines= dataLines[0]
i+=1
return finalLines
# LIBRARY FOR READING CSV FROM URL
def get_matrix(CSV_URL):
csv_url = CSV_URL
import requests # pypenv install requests
import csv # To read CSV
with requests.Session() as s:
download = s.get(CSV_URL)
decoded_content = download.content.decode('utf-8')
dictObj = csv.DictReader(decoded_content.splitlines(), delimiter=',')
return dictObj
def get_id(CSV_URL):
csv_url = CSV_URL
import requests # pypenv install requests -wrong command but close google it
import csv # To read CSV
with requests.Session() as s:
download = s.get(CSV_URL)
decoded_content = download.content.decode('utf-8')
d = csv.reader(decoded_content.splitlines(), delimiter=',')
my_list = list(d)
i=0
id_ = []
for line in range(127):
id_.append(my_list[0][i])
i+=1
return (id_)
# DESIGNED FOR A LOCAL FILE
# USED FOR LOCAL FILE DATA - RETURNS FILE LINES
def read_txt_to_lines(fileName):
with open(fileName, 'rU') as f:
import csv
fileLines = f.readlines()
dataLines = []
i=0
for line in fileLines:
if i==0:
i+=1
else:
l = line.strip().split('\t')
i+=1
dataLines.append(l)
for region in dataLines:
i=0
for reg in region:
region[i]=region[i].strip()
i+=1
return dataLines
def get_data(myString):
# MONGO CLIENT
# CONSTANTS FOR READING CSV FROM URL
textFileName= "H2D2list_376.txt"
CSV_URL = 'https://irsa.ipac.caltech.edu/SCS?table=fp_psc&RA=180.0&DEC=0.0&SR=0.05&format=csv'
headers = get_id(CSV_URL)
dictData = get_matrix(CSV_URL)
MONGO_URL= "localhost"
MONGO_PORT = 27017
# CONSTANTS FOR READING CSV FROM URL
regions = read_txt_to_lines('/Users/Michael/Desktop/School/c3po_final/Gatoraid Server/temp/'+myString)
# Initializing Mongo
from pymongo import Connection, MongoClient
print "Connecting to DB: ", MONGO_URL,MONGO_PORT, "\n"
client = MongoClient(MONGO_URL,MONGO_PORT) # 27017 is the default port number for mongod
db = client.test
col = db.hello
# Make a list of URL's for Gator
CSV_URL = 'https://irsa.ipac.caltech.edu/SCS?table=fp_psc&RA=180.0&DEC=0.0&SR=0.05&format=csv'
nameslist = []
# Enter all Regions into DB
for region in regions:
coords = region[4].strip()
nameslist.append(region[1])
coords = coords.strip().split()
region.pop(4)
region.insert(4, coords[0])
region.insert(5, coords[1])
import sys
# Make URL LIST
URLlist = []
for region in regions:
if sys.getsizeof(region[1])<0:
URLlist.append('https://irsa.ipac.caltech.edu/SCS?table=fp_psc&RA='+region[4]+'&DEC='+region[5]+'&SR=0.05&format=csv')
else:
print "Already in DB"
# # UPLOAD TO MONGO
region_id = []
i=0
for region in regions:
if sys.getsizeof(region[1])<0:
region_id.append(col.insert({
"name": region[1],
"Identifier": region[2],
"Type": region[3],
"RA": region[4],
"DEC": region[5],
"MagU": region[6],
"MagB": region[7],
"MagV": region[8],
"MagR": region[9],
"MagI": region[10],
"spec": region[11],
"bib": region[11],
"not": region[12],
"GatorObjects": []
}) )
i+=1
else:
print "Already in DB"
# # Create a list of URL's and use it to get all the stars in each Region
headers = []
headers = get_id(CSV_URL)
count =0
i=0
for URL in URLlist:
# for URL in URLlist:
dictData = get_matrix(URL)
add_id = []
count1=0
count2 =0
gatorObjects = []
for d in dictData:
try:
gatorObjects.append({
headers[0]:d[headers[0]],
headers[1]:d[headers[1]],
headers[2]:d[headers[2]],
headers[3]:d[headers[3]],
headers[4]:d[headers[4]],
headers[5]:d[headers[5]],
headers[6]:d[headers[6]],
headers[7]:d[headers[7]],
headers[8]:d[headers[8]],
headers[9]:d[headers[9]],
headers[10]:d[headers[10]],
headers[11]:d[headers[11]],
headers[12]:d[headers[12]],
headers[13]:d[headers[13]],
headers[14]:d[headers[14]],
headers[15]:d[headers[15]],
headers[16]:d[headers[16]],
headers[17]:d[headers[17]],
headers[18]:d[headers[18]],
headers[19]:d[headers[19]],
headers[20]:d[headers[20]],
headers[21]:d[headers[21]],
headers[22]:d[headers[22]],
headers[23]:d[headers[23]],
headers[24]:d[headers[24]],
headers[25]:d[headers[25]],
headers[26]:d[headers[26]],
headers[27]:d[headers[27]],
headers[28]:d[headers[28]],
headers[29]:d[headers[29]],
headers[30]:d[headers[30]],
headers[31]:d[headers[31]],
headers[32]:d[headers[32]],
headers[33]:d[headers[33]],
headers[34]:d[headers[34]],
headers[35]:d[headers[35]],
headers[36]:d[headers[36]],
headers[37]:d[headers[37]],
headers[38]:d[headers[38]],
headers[39]:d[headers[39]],
headers[40]:d[headers[40]],
headers[41]:d[headers[41]],
headers[42]:d[headers[42]],
headers[43]:d[headers[43]],
headers[44]:d[headers[44]],
headers[45]:d[headers[45]],
headers[46]:d[headers[46]],
headers[47]:d[headers[47]],
headers[48]:d[headers[48]],
headers[49]:d[headers[49]],
headers[50]:d[headers[50]],
headers[51]:d[headers[51]],
headers[52]:d[headers[52]],
headers[53]:d[headers[53]],
headers[54]:d[headers[54]],
headers[55]:d[headers[55]],
headers[56]:d[headers[56]],
headers[57]:d[headers[57]],
headers[58]:d[headers[58]],
headers[59]:d[headers[59]],
headers[60]:d[headers[60]],
headers[61]:d[headers[61]],
headers[62]:d[headers[62]],
headers[63]:d[headers[63]],
headers[64]:d[headers[64]],
headers[65]:d[headers[65]],
headers[66]:d[headers[66]],
headers[67]:d[headers[67]],
headers[68]:d[headers[68]],
headers[69]:d[headers[69]],
headers[70]:d[headers[70]],
headers[71]:d[headers[71]],
headers[72]:d[headers[72]],
headers[73]:d[headers[73]],
headers[74]:d[headers[74]],
headers[75]:d[headers[75]],
headers[76]:d[headers[76]],
headers[77]:d[headers[77]],
headers[78]:d[headers[78]],
headers[79]:d[headers[79]],
headers[81]:d[headers[81]],
headers[82]:d[headers[82]],
headers[83]:d[headers[83]],
headers[84]:d[headers[84]],
headers[85]:d[headers[85]],
headers[86]:d[headers[86]],
headers[87]:d[headers[87]],
headers[88]:d[headers[88]],
headers[89]:d[headers[89]],
headers[90]:d[headers[90]],
headers[91]:d[headers[91]],
headers[92]:d[headers[92]],
headers[93]:d[headers[93]],
headers[94]:d[headers[94]],
headers[95]:d[headers[95]],
headers[96]:d[headers[96]],
headers[97]:d[headers[97]],
headers[98]:d[headers[98]],
headers[99]:d[headers[99]],
headers[100]:d[headers[100]],
headers[101]:d[headers[101]],
headers[102]:d[headers[102]],
headers[103]:d[headers[103]],
headers[104]:d[headers[104]],
headers[105]:d[headers[105]],
headers[106]:d[headers[106]],
headers[107]:d[headers[107]],
headers[108]:d[headers[108]],
headers[109]:d[headers[109]],
headers[110]:d[headers[110]],
headers[111]:d[headers[111]],
headers[112]:d[headers[112]],
headers[113]:d[headers[113]],
headers[114]:d[headers[114]],
headers[115]:d[headers[115]],
headers[116]:d[headers[116]],
headers[117]:d[headers[117]],
headers[118]:d[headers[118]],
headers[119]:d[headers[119]],
headers[120]:d[headers[120]],
headers[121]:d[headers[121]],
headers[122]:d[headers[122]],
headers[123]:d[headers[123]],
headers[124]:d[headers[124]],
headers[125]:d[headers[125]],
headers[126]:d[headers[126]]
})
except:
print "Error at ",region_id[i]
col.update(
{'_id':bson.ObjectId(oid=str(region_id[i]))},
{
"$set":{"GatorObjects":gatorObjects
}})
print "Working Great at ",region_id[i]
count +=1
i+=1
print "Total objects inserted into DB: "
print count