Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created December 27, 2008 02:35
Show Gist options
  • Save ngerakines/40179 to your computer and use it in GitHub Desktop.
Save ngerakines/40179 to your computer and use it in GitHub Desktop.
A module used to create foaf from git profile pages.
-module(git2foaf).
-compile(export_all).
%% git2foaf:projects("ngerakines").
projects(Username) ->
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}} = http:request(user_url(Username)),
Profile = profile_xml(Body),
CurrentProjects = projects_xml(Body),
Following = following_xml(Body),
[CurrentProjects, Following, Profile].
projects_xml(Body) ->
ProjectRegex =
"<div class=\"title\"><b><a href=\"/([^/]*)/([^/]*)/tree\">([^<]*)</a></b></div>",
{match, Projects} = re:run(Body, ProjectRegex, [global, {capture, [1, 2], list}]),
[begin
erlang:iolist_to_binary([
"<foaf:currentProject rdf:resource=\"",
"http://www.github.com/",
User, "/", Project,
"\" />"
])
end || [User, Project] <- Projects].
following_xml(Body) ->
UserRegex =
"<a href=\"/([^\"]*)\" rel=\"contact\" title=\"([^\"]*)\">",
{match, Users} = re:run(Body, UserRegex, [global, {capture, [1], list}]),
[begin
erlang:iolist_to_binary([
"<foaf:knows rdf:resource=\"http://www.github.com/",
User,
"\" />"
])
end || [User] <- Users].
profile_xml(Body) ->
ProfileUrlRegex =
"<a id=\"profile_([^\"]*)\" rel=\"([^\"]*)\" href=\"([^\"]*)\" class=\"([^\"]*)\">([^<]*)</a>",
ProfileBitsRegex =
"<span id=\"profile_([^\"]*)\" rel=\"/users/([^\"]*)\" class=\"([^\"]*)\">([^<]*)</span>",
{match, ProfileBits} = re:run(Body, ProfileBitsRegex, [global, {capture, [1, 2, 4], list}]),
{match, ProfileUrlBits} = re:run(Body, ProfileUrlRegex, [global, {capture, [1, 3, 5], list}]),
ProfileBits ++ ProfileUrlBits.
user_url(Username) ->
lists:concat(["http://www.github.com/", Username]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment