Skip to content

Instantly share code, notes, and snippets.

@Bhupesh-V
Last active March 17, 2021 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bhupesh-V/d7ac30d5d689df00ef5310f2dbff2d01 to your computer and use it in GitHub Desktop.
Save Bhupesh-V/d7ac30d5d689df00ef5310f2dbff2d01 to your computer and use it in GitHub Desktop.
Generate a RSS Feed of Recent files in a git repository

Generate a RSS Feed of Recent files in a git repository

Please read here to understand how the feed is generated

#!/usr/bin/env python3
# Script to generate a feed of recently committed files in a git repository
# TODO: Add Commit Author #
import subprocess as sp
import pathlib
import re, os
import datetime
from dateutil.parser import parse
HEAD = """<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
"""
FOOTER = """</channel>
</rss>
"""
# Assuming your current working dir is the repo
repo_name = os.path.basename(os.getcwd())
current_date = datetime.datetime.now().strftime("%a, %d %b %Y")
def get_recent_files():
cmd = "git log --no-color -n 10 --date=rfc --diff-filter=A --name-status --pretty='%ad'"
result = sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
out, err = result.communicate()
clean_output = out.decode("utf-8").replace("A\t", "").split("\n")
clean_output = list(filter(lambda x: x != "", clean_output))
files = []
for item in clean_output:
if is_valid(item):
date = item
elif pathlib.Path(item).exists():
entry = item, date
files.append(entry)
return files
def is_valid(date):
try:
if isinstance(parse(date), datetime.datetime):
return True
except ValueError:
return False
def get_repo_link():
repo_origin = "git config --get remote.origin.url"
result = sp.Popen(repo_origin, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
result, err = result.communicate()
return result.decode("utf-8")
if __name__ == "__main__":
files = get_recent_files()
with open("feed.xml", "w") as feed:
feed.write(HEAD)
feed.write(
f"""<title>{repo_name}.git</title>\n<link>{get_repo_link()}</link>\n"""
)
feed.write(
f"""<description>Recently committed files in {repo_name}</description>\n"""
)
feed.write(f"""<lastBuildDate>{current_date}</lastBuildDate>""")
for item in files:
feed.write("""<item>\n""")
feed.write(f"""<title>{item[0]}</title>\n""")
feed.write(f"""<pubDate>{item[1]}</pubDate>\n""")
feed.write("""</item>\n""")
feed.write(FOOTER)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment