forked from neuralinfo/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3_1_2.py
More file actions
executable file
·49 lines (34 loc) · 901 Bytes
/
Assignment3_1_2.py
File metadata and controls
executable file
·49 lines (34 loc) · 901 Bytes
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# David Paculdo
# W205
# Assignment 3
from boto.s3.connection import S3Connection
from boto.s3.key import Key
import os
import pymongo
#Amazon AWS variables
AWS_KEY=os.environ.get("AWS_ACCESS_KEY")
AWS_SECRET=os.environ.get("AWS_SECRET_KEY")
#Connection to AWS
conn = S3Connection(AWS_KEY, AWS_SECRET)
#bucket must already be created
bucket = conn.get_bucket("w205-assignment-2-dpaculdo")
tmpfile="temp_from_s3"
k=Key(bucket)
k.key="microsoft_OR_mojang_2015-02-07_2015-02-14_tweets_0.txt"
k.get_contents_to_filename(tmpfile)
my_file=open(tmpfile,"r")
#mongodb variables
db_name="db_tweets"
coll="tweets"
#mongodb connection
conn=pymongo.MongoClient()
db=conn[db_name]
collection=db[coll]
#Insert into mongodb. Replace "\n" with space.
for line in my_file:
collection.insert({"tweet":line.replace("\n"," ")})
#Clean up
os.remove(tmpfile)