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.

Revisions

  1. Velrok revised this gist Jul 28, 2012. No changes.
  2. @zeisss zeisss revised this gist Aug 1, 2009. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions files2atom.py
    Original file line number Diff line number Diff line change
    @@ -29,11 +29,11 @@ def write_header(self):
    def write_footer(self):
    self.write("</feed>\n")

    def add_item ( self, title, link, updated, text, category = False ):
    def add_item ( self, id, title, link, updated, text, category = False ):
    self.write(" <entry>\n" +
    " <title>" + title + "</title>\n");

    self.write(" <id>" + title + "_" + str(updated) + "</id>\n" +
    self.write(" <id>" + id + "</id>\n" +
    " <updated>" +
    datetime.datetime.fromtimestamp(updated).strftime( "%Y-%m-%dT%H:%M:%S%ZZ") +
    "</updated>\n")
    @@ -138,6 +138,7 @@ def entry_comparator(x,y):

    # Add the entry
    feed.add_item(
    file.replace(" ", "_"),
    "%s - %s" % (file, category),
    link,
    mtime,
  3. @zeisss zeisss revised this gist Jul 31, 2009. 1 changed file with 49 additions and 20 deletions.
    69 changes: 49 additions & 20 deletions files2atom.py
    Original file line number Diff line number Diff line change
    @@ -42,48 +42,54 @@ def add_item ( self, title, link, updated, text, category = False ):
    self.write(" <link href=\"" + link + "\"/>\n");

    if text:
    self.write (" <summary>" + text + "</summary>\n")
    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):
    if file[0:15] == "/public/Videos/":
    return "http://moon/Videos/" + file[15:]
    """
    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
    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/B\xc3cher/"):
    return "Books"
    elif file.startswith("/public/Buecher/"):
    return "Book"
    elif file.startswith("/public/Images/"):
    return "Images"
    return "Image"
    elif file.startswith("/public/Bilder/"):
    return "Images"
    return "Picture"
    return "Other"
    def entry_comparator(x,y):
    return int( x[2] - y[2] )


    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] == ".":
    #print " Skipping %s" % x
    if x[0] == ".":
    del dirnames[i]
    i = i + 1

    @@ -94,26 +100,49 @@ def main(args=False):
    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, 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"))
    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((x, path2link(file), stat.st_mtime, "Other: %s" % file, guess_category(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 (title,link, mtime, content, category) in entries:
    feed.add_item(title, link, mtime, content, category)
    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(
    "%s - %s" % (file, category),
    link,
    mtime,
    content,
    category)
    feed.write_footer()

    if __name__ == "__main__":
  4. @invalid-email-address Anonymous revised this gist Jul 31, 2009. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion files2atom.py
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@
    import os.path
    import os
    import sys
    import anydbm
    import datetime

    class AtomWriter:
  5. @invalid-email-address Anonymous created this gist Jul 31, 2009.
    121 changes: 121 additions & 0 deletions files2atom.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    #!/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()