From f32ff66d2f78157a1e69e2600792bc4e50f85254 Mon Sep 17 00:00:00 2001 From: mjjo53 Date: Fri, 4 May 2018 21:39:32 +0900 Subject: [PATCH] =?UTF-8?q?=EB=94=94=EC=8A=A4=ED=81=AC=20=EC=9A=A9?= =?UTF-8?q?=EB=9F=89=20=ED=99=95=EC=9D=B8=20=EB=B0=8F=20=EA=B8=B0=ED=83=80?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Crawler/ProxyHandler.py | 9 +++++---- Crawler/Setting.py | 5 ++++- Crawler/Util.py | 9 +++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 Crawler/Util.py diff --git a/Crawler/ProxyHandler.py b/Crawler/ProxyHandler.py index 5281e6b..075ce63 100755 --- a/Crawler/ProxyHandler.py +++ b/Crawler/ProxyHandler.py @@ -6,7 +6,7 @@ import concurrent.futures import time from .Logger import Logger - +from .Util import Util class ProxyHandler: PROXY_FILE_NAME = 'temp/proxy.bin' @@ -107,8 +107,9 @@ class ProxyHandler: alive_proxies = [proxy for proxy in proxies if proxy['alive']] Logger.log('proxies checking end: available : {}'.format(len(alive_proxies))) - with open(self.PROXY_FILE_NAME, 'wb') as f: - pickle.dump(alive_proxies, f) + if Util.get_free_space() >= 1024: + with open(self.PROXY_FILE_NAME, 'wb') as f: + pickle.dump(alive_proxies, f) return alive_proxies @@ -136,7 +137,7 @@ class ProxyHandler: def set_proxy_dead(self, proxy): proxy['alive'] = False for proxy in self.proxies: - if proxy['alive']: + if proxy['alive'] and Util.get_free_space() >= 1024: with open(self.PROXY_FILE_NAME, 'wb') as f: pickle.dump(self.proxies, f) return diff --git a/Crawler/Setting.py b/Crawler/Setting.py index 7dca1f3..dbceb02 100755 --- a/Crawler/Setting.py +++ b/Crawler/Setting.py @@ -2,7 +2,7 @@ import yaml import os from .Logger import Logger - +from .Util import Util class Setting: SETTING_FILE = 'conf/settings.yml' @@ -117,6 +117,9 @@ class Setting: yaml.dump(downloaded_ex, outfile, allow_unicode=True) def save(self): + if Util.get_free_space() < 1024: + return + with open(self.DOWNLOADED_FILE, 'w', encoding='utf-8') as outfile: yaml.dump(self.downloaded, outfile, allow_unicode=True) pass diff --git a/Crawler/Util.py b/Crawler/Util.py new file mode 100644 index 0000000..115209f --- /dev/null +++ b/Crawler/Util.py @@ -0,0 +1,9 @@ +import subprocess + +class Util: + @staticmethod + def get_free_space(): + df = subprocess.Popen(["df", "/"], stdout=subprocess.PIPE) + output = df.communicate()[0] + device, size, used, available, percent, mountpoint = str(output).split("\\n")[1].split() + return int(available)