-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbulk_extract.py
53 lines (45 loc) · 1.39 KB
/
bulk_extract.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
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import json
import os
import json
from datetime import datetime
import urllib
import os
import sys
dir = sys.argv[1]
users = {}
chunksize = 1000
def pullImages(users):
for id in users.keys():
print id
img_url = users[id]
ext = img_url.split('/')[-1].split('.')[1]
if os.path.isfile(dir + '/' + id + '.' + ext) == False:
try:
urllib.urlretrieve(img_url, dir + '/' + id + '.' + ext)
except IOError:
print 'Error...moving on'
else:
print 'image exists'
def addUsers(response):
for i in response["hits"]["hits"]:
user = i["_id"]
if not user in users:
users[user] = i["fields"]["images.thumbnail.url"][0]
es = Elasticsearch(['http://10.1.94.103:9200/'])
query={"size":chunksize,"fields":["_id","images.thumbnail.url"], "query" : {"match_all" : {}}}
scanResp= es.search(index="instagram_remap", doc_type=dir, body=query, search_type="scan", scroll="10m")
scrollId= scanResp['_scroll_id']
response= es.scroll(scroll_id=scrollId, scroll= "10m")
addUsers(response)
count = len(response["hits"]["hits"])
scrollId = response['_scroll_id']
print count
while response["hits"]["hits"]:
response= es.scroll(scroll_id=scrollId, scroll= "10m")
addUsers(response)
scrollId = response['_scroll_id']
count += len(response["hits"]["hits"])
print count
pullImages(users)