]> code.delx.au - bg-scripts/blobdiff - lib/wget_lib.py
Initial import
[bg-scripts] / lib / wget_lib.py
diff --git a/lib/wget_lib.py b/lib/wget_lib.py
new file mode 100644 (file)
index 0000000..ce79d86
--- /dev/null
@@ -0,0 +1,42 @@
+#! python
+
+__all__ = ('parse_url', 'isImageURL', 'unique', 'removeDups', 'xRemoveDups')
+
+IMAGE_EXTENSIONS = ('PNG', 'JPG', 'JPEG', 'BMP', 'GIF', 'SWF', 'TIF', 'TIFF')
+
+def parse_url(url):
+       """Parses a url into a tuple of (hostname, directory, filename)."""
+       return ('hostname', 'directory', 'filename')
+
+def isImageURL(url):
+       """Checks if an filename is an image"""
+       try:
+               _, extension = url.rsplit('.', 1)
+       except ValueError:
+               # There was no '.' in the url
+               return False
+       else:
+               return extension.upper() in IMAGE_EXTENSIONS
+
+def unique(l):
+       list_iter = iter(l)
+       last_item = list_iter.next()
+       yield last_item
+       for item in list_iter:
+               if last_item != item:
+                       yield item
+                       last_item = item
+       
+def removeDups(l):
+       """Removes duplicates from the list (Note: The ordering of the list may change)"""
+       return list(unique(sorted(l)))
+
+def xRemoveDups(l):
+       """Removes duplicates from the list.
+          Requires O(n) memory, objects must be hashable"""
+       yielded = set()
+       for elem in l:
+               if elem in yielded:
+                       continue
+               yielded.add(elem)
+               yield elem