summaryrefslogtreecommitdiffstats
path: root/sorter.py
diff options
context:
space:
mode:
authorGravatar ryuslash2010-03-19 00:18:04 +0100
committerGravatar ryuslash2010-03-19 00:18:04 +0100
commit7f8dfa1d30583dd1d8c40c6cd1c079d2a722c9df (patch)
tree12564b5ac501f2a35b660d7f5bd7461581f3f6ae /sorter.py
parent4a9cc7e2b608332f3d41aa47f5f1a893a5eab529 (diff)
download4grab-7f8dfa1d30583dd1d8c40c6cd1c079d2a722c9df.tar.gz
4grab-7f8dfa1d30583dd1d8c40c6cd1c079d2a722c9df.zip
Sorting, multi category, multi resolution
After a file has been downloaded a callback function can now be called. The callback function I call checks to see if the resolution of the image appears in the collection of resolutions that has been entered in the configuration file and deletes/moves accordingly. If a file can not be read (which I have noticed happens sometimes), it is removed, not copied and not archived so that it can be retried later. 4grab got a new command-line option, -s --sorter, to sort out old images, running python sorter.py has the same effect, but this seemed pretties. theoretically multiple categories could now be entered into the configuration file seperated by ',', but this hasn't been tested yet. mutliple resolutions could be entered into the configuration file, seperated by ',' like so: 1680x1050,1920x1200. Configuration now checks to see if all the necessary properties are available in the configuration file, if one is missing, it tries to create it.
Diffstat (limited to 'sorter.py')
-rw-r--r--sorter.py110
1 files changed, 110 insertions, 0 deletions
diff --git a/sorter.py b/sorter.py
new file mode 100644
index 0000000..482e343
--- /dev/null
+++ b/sorter.py
@@ -0,0 +1,110 @@
+######################################################################
+# Copyright 2009, 2010 ryuslash
+#
+# This file is part of 4grab.
+#
+# 4grab is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 4grab is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with 4grab. If not, see <http://www.gnu.org/licenses/>.
+######################################################################
+
+import config
+import Image
+import shutil
+import os
+
+def dummy_option_creator(value1, value2): pass
+config._optioncreator = dummy_option_creator
+
+class Sorter:
+
+ def __init__(self):
+ self.conf = config.Configuration()
+ self.resolutions = self.conf.get_resolutions()
+
+ def act(self, filename):
+ download_base = self.conf.get_download_location()
+
+ if self.check_filename(filename):
+ image = None
+ try:
+ image = Image.open(os.path.join(download_base,
+ filename))
+ except IOError:
+ print "Cannot read image file %s, might be broken" \
+ % filename
+
+ if not image == None and self.archive_check(filename):
+ for resolution in self.resolutions:
+ resolution = resolution.split('x')
+ foldername = "%s-%s" % (resolution[0],
+ resolution[1])
+ folderpath = os.path.join(download_base,
+ foldername)
+
+ if str(image.size[0]) == resolution[0] and \
+ str(image.size[1]) == resolution[1]:
+ if not os.path.exists(folderpath):
+ os.makedirs(folderpath)
+ #print "creating", folderpath
+
+ self.copy(filename, folderpath)
+ break
+
+ self.archive(filename)
+ self.remove(filename)
+
+ def copy(self, filename, destpath):
+ download_base = self.conf.get_download_location()
+ source = os.path.join(download_base,
+ os.path.basename(filename))
+ dest = os.path.join(destpath,
+ os.path.basename(filename))
+ if source != dest:
+ shutil.copy(source, dest)
+ else:
+ print "\nHow can this even happen?! Copying", source, "to", dest
+ #print "\nParameters are", filename, "and", destpath
+
+ def archive(self, filename):
+ download_base = self.conf.get_download_location()
+ location = self.conf.get_archive_location()
+ if not os.path.exists(location):
+ os.makedirs(location)
+
+ dest = os.path.join(location, filename)
+ f = open(dest, "w")
+ file.close(f)
+
+
+ def archive_check(self, filename):
+ archive_path = self.conf.get_archive_location()
+ fullname = os.path.join(archive_path, filename)
+ return os.path.exists(fullname)
+
+ def check_filename(self, filename):
+ ext = os.path.splitext(filename)[1]
+ return ext == ".jpg" or \
+ ext == ".png" or \
+ ext == ".gif"
+
+ def remove(self, filename):
+ download_base = self.conf.get_download_location()
+ source = os.path.join(download_base, filename)
+ os.remove(source)
+
+if __name__ == "__main__":
+ conf = config.Configuration()
+ download_base = conf.get_download_location()
+ sorter = Sorter()
+ for item in os.listdir(download_base):
+ sorter.act(item)