84 lines
2.2 KiB
Python
84 lines
2.2 KiB
Python
#!/usr/bin/python3
|
|
|
|
from utils import *
|
|
from download_page import download
|
|
|
|
# portable code to get filename
|
|
import os
|
|
platform = os.path.basename(__file__)
|
|
if platform.endswith(".py"):
|
|
platform = platform[:(-3)]
|
|
|
|
def extract_from_url (url, verbosity):
|
|
# split into domain and path
|
|
index = url.find("/", url.find("//")+2)
|
|
domain = url[:index]
|
|
path = url[index:]
|
|
|
|
# get owner/organization name
|
|
index = path.find("/",1)
|
|
if index < 0:
|
|
# we could just return None here, as this means no project
|
|
org = path[1:]
|
|
else:
|
|
org = path[1:index]
|
|
if org == "":
|
|
return None
|
|
|
|
# get project name
|
|
index2 = path.find("/",index+1)
|
|
if index2 < 0:
|
|
project = path[index+1:]
|
|
else:
|
|
project = path[index+1:index2]
|
|
if project == "":
|
|
return None
|
|
|
|
# get page category
|
|
index3 = path.find("/",index2+1)
|
|
if index3 < 0:
|
|
category = path[index2+1:]
|
|
else:
|
|
category = path[index2+1:index3]
|
|
|
|
# return feed based on category
|
|
urlbase = domain + "/" + org + "/" + project + "/"
|
|
if category == "":
|
|
# note, this is an alternate link to /org/project/commits/default-branch.atom
|
|
return urlbase + "commits.atom"
|
|
elif category == "releases":
|
|
return urlbase + "releases.atom"
|
|
elif category == "tags":
|
|
return urlbase + "tags.atom"
|
|
elif category == "tree":
|
|
# get current branch
|
|
index4 = path.find("/",index3+1)
|
|
if index4 < 0:
|
|
branch = path[index3+1:]
|
|
else:
|
|
branch = path[index3+1:index4]
|
|
if branch != "":
|
|
# return commit feed for that branch
|
|
return urlbase + "commits/" + branch + ".atom"
|
|
|
|
def extract_from_page (page, verbosity):
|
|
result = search (page, '<link href="', 'type="application/atom+xml">', reverse=True)
|
|
if not result is None:
|
|
# strip '" rel="alternate" title="Recent Commits to <project name>:<branch>" '
|
|
index = result.find('"')
|
|
result = result[:index]
|
|
if not result == "":
|
|
return result
|
|
|
|
def extract (url, page=None, network=False, verbosity=3, args={}):
|
|
feed = extract_from_url (url, verbosity)
|
|
if not feed is None:
|
|
return feed
|
|
else:
|
|
notify ("Unable to get feed from URL alone", verbosity, platform)
|
|
if network == True:
|
|
page = download (platform, url, args, verbosity)
|
|
feed = extract_from_page (page, verbosity)
|
|
if not feed is None:
|
|
return feed
|