Last active
July 5, 2016 09:23
-
-
Save nickbalch/83e0e172af1d0246e231e6fb749c5027 to your computer and use it in GitHub Desktop.
Python scripts for generating RSS feed of images in a directory. Resulting file can be served up by any webserver to (for example) a LCD picture frame that can accept RSS feeds. Adapted from http://www.stuffaboutcode.com/2012/09/python-create-rss-podcast-of-mp3-files.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import libraries | |
import os | |
import sys | |
import datetime | |
import time | |
import random | |
# import constants from stat library | |
from stat import * # ST_SIZE ST_MTIME | |
# format date method | |
def formatDate(dt): | |
return dt.strftime("%a, %d %b %Y %H:%M:%S +0000") | |
# get the item/@type based on file extension | |
def getItemType(fileExtension): | |
if fileExtension == "jpg": | |
mediaType = "image/jpeg" | |
## if fileExtension == "aac": | |
## mediaType = "audio/mpeg" | |
elif fileExtension == "mp4": | |
mediaType = "video/mpeg" | |
else: | |
mediaType = "audio/mpeg" | |
return mediaType | |
# constants | |
# the podcast name | |
rssTitle = "Image Stream" | |
# the podcast description | |
rssDescription = "Image Stream" | |
# the url where the podcast items will be hosted | |
rssSiteURL = "http://x.x.x.x:pp" | |
# the url of the folder where the items will be stored | |
rssItemURL = rssSiteURL + "/frame/flickr" | |
# the url to the podcast html file | |
rssLink = rssSiteURL + "/index.html" | |
# url to the podcast image | |
rssImageUrl = rssSiteURL + "/logo.jpg" | |
# the time to live (in minutes) | |
rssTtl = "60" | |
# contact details of the web master | |
rssWebMaster = "[email protected]" | |
#record datetime started | |
now = datetime.datetime.now() | |
# command line options | |
# - python createRSFeed.py /path/to/podcast/files /path/to/output/rss | |
# directory passed in | |
rootdir = sys.argv[1] | |
# output RSS filename | |
outputFilename = sys.argv[2] | |
# Main program | |
# open rss file | |
outputFile = open(outputFilename, "w") | |
# write rss header | |
outputFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n") | |
outputFile.write("<rss version=\"2.0\">\n") | |
outputFile.write("<channel>\n") | |
outputFile.write("<title>" + rssTitle + "</title>\n") | |
outputFile.write("<description>" + rssDescription + "</description>\n") | |
outputFile.write("<link>" + rssLink + "</link>\n") | |
outputFile.write("<ttl>" + rssTtl + "</ttl>\n") | |
outputFile.write("<image><url>" + rssImageUrl + "</url><title>" + rssTitle + "</title><link>" + rssLink + "</link></image>\n") | |
outputFile.write("<copyright>mart 2012</copyright>\n") | |
outputFile.write("<lastBuildDate>" + formatDate(now) + "</lastBuildDate>\n") | |
outputFile.write("<pubDate>" + formatDate(now) + "</pubDate>\n") | |
outputFile.write("<webMaster>" + rssWebMaster + "</webMaster>\n") | |
# walk through all files and subfolders | |
for path, subFolders, files in os.walk(rootdir): | |
print len(files) | |
# limit to 250 images to stay within possible limitations of target picture frame | |
random_list = random.sample(files,250) | |
for file in random_list: | |
# split the file based on "." we use the first part as the title and the extension to work out the media type | |
fileNameBits = file.split(".") | |
# get the full path of the file | |
fullPath = os.path.join(path, file) | |
# get the stats for the file | |
fileStat = os.stat(fullPath) | |
# find the path relative to the starting folder, e.g. /subFolder/file | |
relativePath = fullPath[len(rootdir):] | |
# write rss item | |
outputFile.write("<item>\n") | |
outputFile.write("<title>" + fileNameBits[0].replace("_", " ") + "</title>\n") | |
outputFile.write("<description>A description</description>\n") | |
outputFile.write("<link>" + rssItemURL + relativePath + "</link>\n") | |
outputFile.write("<guid>" + rssItemURL + relativePath + "</guid>\n") | |
outputFile.write("<pubDate>" + formatDate(datetime.datetime.fromtimestamp(fileStat[ST_MTIME])) + "</pubDate>\n") | |
outputFile.write("<enclosure url=\"" + rssItemURL + relativePath + "\" length=\"" + str(fileStat[ST_SIZE]) + "\" type=\"" + getItemType(fileNameBits[len(fileNameBits)-1]) + "\" />\n") | |
outputFile.write("</item>\n") | |
# write rss footer | |
outputFile.write("</channel>\n") | |
outputFile.write("</rss>") | |
outputFile.close() | |
print "complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
todo