-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimageSaver.py
63 lines (51 loc) · 2.04 KB
/
imageSaver.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
#import scrapy
import urllib
import datetime
import os
def getURLSFromFile(filename):
f = open(filename, 'r')
urls = []
nonRelevantURLs = 0
#no .gifv support yet (four symbols...)
filters = ['.jpg', '.gif', '.png']
blacklist = ['.gifv']
for line in f:
isRelevant = False
for currentFilter in filters:
if currentFilter in line:
isRelevant = True
break
#Filter out blacklisted URLs (unsupported formats etc.)
for currentFilter in blacklist:
if currentFilter in line:
isRelevant = False
break
if isRelevant:
urls.append(line[:-1]) #trim off the newline
else:
nonRelevantURLs += 1
print 'Filtered ' + str(nonRelevantURLs) + ' URLs that didn\'t contain images'
return urls
def _saveAllImagesToDir(urls, directory, soft_retrieve = True):
count = 0
imagesToSave = len(urls)
for url in urls:
if not soft_retrieve:
urllib.urlretrieve(url, directory + '/img' + str(count) + url[-4:])
count += 1
print '[' + str(int((float(count) / float(imagesToSave)) * 100)) + '%] ' + url + ' saved to "' + directory + '/img' + str(count) + url[-4:] + '"'
def _makeDirIfNonexistant(directory):
if not os.path.exists(directory):
os.makedirs(directory)
#note that you must explicitly set soft_retrieve to False to actually get the images
def saveAllImages(soft_retrieve_imgs=True):
saved_urls = getURLSFromFile('savedURLS.txt')
liked_urls = getURLSFromFile('likedURLS.txt')
timestamp = datetime.datetime.now().strftime('%m-%d_%H-%-M-%S')
count = 0
savedDirectory = 'ImagesSaved__' + timestamp
likedDirectory = 'ImagesLiked__' + timestamp
_makeDirIfNonexistant(savedDirectory)
_makeDirIfNonexistant(likedDirectory)
_saveAllImagesToDir(saved_urls, savedDirectory, soft_retrieve = soft_retrieve_imgs)
_saveAllImagesToDir(liked_urls, likedDirectory, soft_retrieve = soft_retrieve_imgs)