Skip to content

Instantly share code, notes, and snippets.

@Velrok
Forked from zeisss/files2atom.py
Created March 5, 2011 20:34
Show Gist options
  • Select an option

  • Save Velrok/856692 to your computer and use it in GitHub Desktop.

Select an option

Save Velrok/856692 to your computer and use it in GitHub Desktop.
Exports file changes as atom feed.
#!/usr/bin/python
import os.path
import os
import sys
import anydbm
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, title, link, updated, text, category = False ):
self.write(" <entry>\n" +
" <title>" + title + "</title>\n");
self.write(" <id>" + title + "_" + str(updated) + "</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 (" <summary>" + text + "</summary>\n")
if category:
self.write (" <category><term>%s</term></category>\n" % category)
self.write(" </entry>\n")
def path2link(file):
if file[0:15] == "/public/Videos/":
return "http://moon/Videos/" + file[15:]
else:
return file
def guess_category (file):
if file.endswith(".app"):
return "Application"
if file.startswith( "/public/Videos/"):
return "Video"
elif file.startswith("/public/Musik/"):
return "Music"
elif file.startswith("/public/B\xc3cher/"):
return "Books"
elif file.startswith("/public/Images/"):
return "Images"
elif file.startswith("/public/Bilder/"):
return "Images"
return "Other"
def entry_comparator(x,y):
return int( x[2] - y[2] )
def main(args=False):
if not args:
args = sys.argv
entries = []
for (dirpath, dirnames, filenames) in os.walk("/public"):
i = 0
for x in dirnames:
if x[0] == ".":
#print " Skipping %s" % x
del dirnames[i]
i = i + 1
i = 0
for x in filenames:
if x[0] == ".":
del filenames[i]
i = i + 1
if dirnames == ["VIDEO_TS"]:
for x in dirnames:
dirnames.remove(x)
stat = os.stat(dirpath)
entries.append((dirpath, path2link(dirpath), stat.st_mtime, "DVD: %s" % dirpath, "DVD"))
elif dirpath.endswith(".app"):
stat = os.stat(dirpath)
entries.append((dirpath, path2link(dirpath), stat.st_mtime, "Application (Mac): %s" % dirpath, "Application"))
else:
for x in filenames:
file = "%s/%s" % (dirpath, x)
stat = os.stat(file)
entries.append((x, path2link(file), stat.st_mtime, "Other: %s" % file, guess_category(file)))
entries.sort(entry_comparator, reverse=True)
feed = AtomWriter("Dateien auf Moon", "http://moon/")
for (title,link, mtime, content, category) in entries:
feed.add_item(title, link, mtime, content, category)
feed.write_footer()
if __name__ == "__main__":
main()
@Velrok
Copy link
Copy Markdown
Author

Velrok commented Jul 28, 2012

Exports file changes as atom feed.

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