Skip to content

Instantly share code, notes, and snippets.

@zeisss
Created July 31, 2009 21:13
Show Gist options
  • Save zeisss/159454 to your computer and use it in GitHub Desktop.
Save zeisss/159454 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os.path
import os
import sys
import datetime
class AtomWriter:
def __init__ (self, title, link = "http://localhost"):
self.title = title
self.link = link
#self.fp = open(file, 'w')
self.write_header()
def write(self, text):
print text,
def write_header(self):
self.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<feed xmlns=\"http://www.w3.org/2005/Atom\">\n" +
" <title>" + self.title + "</title>\n" +
" <link href=\"" + self.link + "\"/>\n" +
" <updated>" + datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S%ZZ") + "</updated>\n" +
" <author><name>AtomWriter</name></author>\n" +
" <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>\n" +
"\n")
def write_footer(self):
self.write("</feed>\n")
def add_item ( self, id, title, link, updated, text, category = False ):
self.write(" <entry>\n" +
" <title>" + title + "</title>\n");
self.write(" <id>" + id + "</id>\n" +
" <updated>" +
datetime.datetime.fromtimestamp(updated).strftime( "%Y-%m-%dT%H:%M:%S%ZZ") +
"</updated>\n")
if link:
self.write(" <link href=\"" + link + "\"/>\n");
if text:
self.write (" <content type=\"html\">" + text + "</content>\n")
if category:
self.write (" <category><term>%s</term></category>\n" % category)
self.write(" </entry>\n")
def path2link(file):
"""
Maps the file path to an url, that can be used in the browser.
"""
if file.startswith("/public/"):
return "http://moon/public/" + file[8:]
else:
return "file://%s" % file
def guess_category (file):
"""
Guess the category based on the file path.
We store out stuff in /public/<category>/ ...
"""
if file.endswith(".app"):
return "Application"
if file.startswith( "/public/Videos/"):
return "Video"
elif file.startswith("/public/Musik/"):
return "Music"
elif file.startswith("/public/Buecher/"):
return "Book"
elif file.startswith("/public/Images/"):
return "Image"
elif file.startswith("/public/Bilder/"):
return "Picture"
return "Other"
def main(args=False):
if not args:
args = sys.argv
# Store all files under /public in the entries list
entries = []
for (dirpath, dirnames, filenames) in os.walk("/public"):
# clean the dirname + filename lists
i = 0
for x in dirnames:
if x[0] == ".":
del dirnames[i]
i = i + 1
i = 0
for x in filenames:
if x[0] == ".":
del filenames[i]
i = i + 1
# If the current folder is DVD Rip
if dirnames == ["VIDEO_TS"]:
for x in dirnames:
dirnames.remove(x)
stat = os.stat(dirpath)
file = dirpath.split("/")[-1]
entries.append((dirpath, file, path2link(dirpath), stat.st_mtime))
# The rest are one entry per file
else:
for x in filenames:
file = "%s/%s" % (dirpath, x)
stat = os.stat(file)
entries.append((dirpath, x, path2link(file), stat.st_mtime))
# Now sort it in reverse order
def entry_comparator(x,y):
return int( x[3] - y[3] )
entries.sort(entry_comparator, reverse=True)
# Write the entries to the output
feed = AtomWriter("Dateien auf Moon", "http://moon/")
for (path, file, link, mtime) in entries:
category = guess_category("%s/%s" % (path,file))
content = "Kategory: %s<br />Pfad: %s<br />Name: %s<br />" % (category, path, file)
if (category == "Picture" or
file.lower().endswith(".jpg") or
file.lower().endswith(".png")):
content += "<img src=\"%s\" />" % link
# Add the entry
feed.add_item(
file.replace(" ", "_"),
"%s - %s" % (file, category),
link,
mtime,
content,
category)
feed.write_footer()
if __name__ == "__main__":
main()
@zeisss
Copy link
Author

zeisss commented Apr 12, 2010

An erlang version of this script for 24h deployment is available at http://github.com/zeisss/ewalker/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment