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 ```yaml
# user app credentials # user app credentials
credentials: credentials:
client_id: your app-client id # change this client_id: your app-client id # change this
client_secret: your app-client secret # and change this client_secret: your app-client secret # change this
# required extension of the file to be downloaded # required extension of the file to be downloaded
image-extensions: image-extensions:
- png - png
- jpg - jpg
- jpeg - jpeg
min-size: 5 # minimum size in kilobytes
``` ```
## Running ## Running

@ -7,4 +7,6 @@ credentials:
image-extensions: image-extensions:
- png - png
- jpg - 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 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 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): def assert_dir_exist(dirpath):
@ -32,17 +33,29 @@ def download_file(url: str, dest: str):
""" """
f = open(dest, "wb") f = open(dest, "wb")
req = urlreq.Request(url) req = urlreq.Request(url)
success = False
try: try:
image = urlreq.urlopen(req) image = urlreq.urlopen(req)
f.write(image.read()) f.write(image.read())
f.close() success = True
return True
except ConnectionError: except ConnectionError:
print('\r[-] Connection Error \r') print('\r[-] Connection Error \r')
return False
except urlreq.HTTPError as err: except urlreq.HTTPError as err:
print('\r[-] HTTPError for %s: %s \r' % (url, 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: class ProgressBar:
@ -193,6 +206,9 @@ def main():
if 'image-extensions' in settings: if 'image-extensions' in settings:
global img_ext global img_ext
img_ext = settings['image-extensions'] img_ext = settings['image-extensions']
if 'min-size' in settings:
global min_size
min_size = int(settings['min-size'])
credentials = settings['credentials'] credentials = settings['credentials']
client = praw.Reddit( client = praw.Reddit(
client_id=credentials['client_id'], client_id=credentials['client_id'],

Loading…
Cancel
Save