-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.py
executable file
·159 lines (126 loc) · 5.96 KB
/
fetch.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
# BSD License:
# ErrFilter: Exploratory Error Analysis
# Copyright © 2019, Battelle Memorial Institute
# All rights reserved.
# 1. Battelle Memorial Institute (hereinafter Battelle) hereby grants permission
# to any person or entity lawfully obtaining a copy of this software and
# associated documentation files (hereinafter “the Software”) to redistribute
# and use the Software in source and binary forms, with or without
# modification. Such person or entity may use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and may permit
# others to do so, subject to the following conditions:
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimers.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Other than as used herein, neither the name Battelle Memorial Institute
# or Battelle may be used in any form whatsoever without the express
# written consent of Battelle.
# 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSEARE DISCLAIMED. IN NO EVENT SHALL BATTELLE OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
import json, base64, argparse, time
from PIL import Image
from io import BytesIO
import requests
from twython import Twython
from pymongo import MongoClient
import pandas as pd
def imgToBase64(img, format):
buffered = Image.open(BytesIO(img))
encodingBuff = BytesIO()
buffered.save(encodingBuff, format=format)
s = base64.b64encode(encodingBuff.getvalue()).decode('ascii')
return f'data:image/{format};base64,{s}'
def base64ToImg(s):
start = s.find(',') + 1
imgdata = base64.b64decode(str(s[start:]))
return Image.open(BytesIO(imgdata))
def download_images(image_url):
"""
Downloads image content given by URL
:param image_url: URL for image. Ex: https://twitter.com/Cur89henvfpavnfka.jpg
:return: image data
"""
try: # Try for valid link
img_data = requests.get(image_url).content
if img_data: # Check if content is returned from request.get (some images get removed)
return img_data
else:
print("Image does not exist")
return None
except:
print("Invalid link")
return None
# document format:
# { '_id' : 'tag:search.twitter.com,2005:77863...',
# 'body' : 'The content of the tweet',
# 'src' : 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgG...' }
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--twitter', default='./credentials.json', help='Twitter credentials file (JSON)')
parser.add_argument('-i', '--input', default='./data/small-ensemble.json', help='Path to data file to load')
parser.add_argument('-m', '--mongo', default='{}', help='MongoDB conection information--defaults to localhost:27017')
parser.add_argument('-s', '--sleep', default=1.0, type=float, help='Seconds between API requests')
parser.add_argument('-x', '--drop', action='store_true', help='Drop data collection before fetching')
args = parser.parse_args()
print(args)
client = MongoClient(**json.loads(args.mongo))
db = client.fpa_deception
collection = db.data
with open(args.input) as fp:
if args.drop:
print(f'Clearing all instance data...')
collection.delete_many({})
# load the model from disk
model = json.load(fp)
# fetch the data from the model's index and add it to the database
to_fetch = model['index']
has_src = {}
has_body = {}
with open(args.twitter) as fp:
tweet = Twython(**json.load(fp))
for i, _id in enumerate(to_fetch):
has_body[_id] = 'N'
has_src[_id] = 'N'
print(f'{i}/{len(to_fetch)}', end=' ')
doc = { '_id': _id}
try:
new_tweet = tweet.show_status(id=_id)
doc['body'] = new_tweet['text']
image_url = new_tweet["entities"]["media"][0]["media_url_https"]
img_data = download_images(image_url)
if img_data:
doc['src'] = imgToBase64(img_data, format="jpeg")
has_src[_id] = 'Y'
has_body[_id] = 'Y'
print('OK')
except Exception as e:
doc['body'] = str(e)
print(e)
collection.insert_one(doc)
time.sleep(args.sleep)
# if i > 15:
# break
# add the model to the database
_id = model['_id']
del model['_id']
model = pd.concat((pd.DataFrame(**model),
pd.Series(has_body, name='has_body'),
pd.Series(has_src, name='has_image')),
axis=1, sort=True)\
.fillna('N')\
.to_dict(orient='split')
model['_id'] = _id
db.models.delete_one({'_id': _id})
db.models.insert_one(model)
print('Done.')