Added min-size filter

- added min-size setting (default 5kb) ignore images with a size tinier than the minimum
pull/6/head
Trivernis 6 years ago
parent 63847390f1
commit 01d9c1381a

@ -23,14 +23,16 @@ You can copy the `default-config.yaml` file to the `config.yaml` file and change
```yaml
# user app credentials
credentials:
client_id: your app-client id # change this
client_secret: your app-client secret # and change this
client_id: your app-client id # change this
client_secret: your app-client secret # change this
# required extension of the file to be downloaded
image-extensions:
- png
- jpg
- jpeg
min-size: 5 # minimum size in kilobytes
```
## Running

@ -7,4 +7,6 @@ credentials:
image-extensions:
- png
- jpg
- jpeg
- jpeg
min-size: 5 # minimum size in kilobytes

@ -11,6 +11,7 @@ import urllib.request as urlreq
user_agent = 'python:riddle:3.0 (by u/Trivernis)' # the reddit api user-agent
img_ext = ['jpg', 'jpeg', 'png'] # default used extensions to filter for images
min_size = 5 # minimum size in kilobytes. changeable in settings
def assert_dir_exist(dirpath):
@ -32,17 +33,29 @@ def download_file(url: str, dest: str):
"""
f = open(dest, "wb")
req = urlreq.Request(url)
success = False
try:
image = urlreq.urlopen(req)
f.write(image.read())
f.close()
return True
success = True
except ConnectionError:
print('\r[-] Connection Error \r')
return False
except urlreq.HTTPError as err:
print('\r[-] HTTPError for %s: %s \r' % (url, err))
return False
except urlreq.URLError as err:
print('\r[-] URLError for %s: %s \r' % (url, err))
f.close()
try:
file_size = round(os.path.getsize(dest) / 1000)
if not success:
os.remove(dest)
elif file_size < min_size:
os.remove(dest)
success = False
print('\r[-] Removed %s: Too small (%s kb)\r' % (dest, file_size))
except IOError as err:
print('\r[-] Error when removing file %s: %s' % (dest, err))
return success
class ProgressBar:
@ -193,6 +206,9 @@ def main():
if 'image-extensions' in settings:
global img_ext
img_ext = settings['image-extensions']
if 'min-size' in settings:
global min_size
min_size = int(settings['min-size'])
credentials = settings['credentials']
client = praw.Reddit(
client_id=credentials['client_id'],

Loading…
Cancel
Save