summaryrefslogtreecommitdiff
path: root/foaf/mkfoaf.sh
blob: e8ee107329f39b2b21c41816b1f350ef66124cf7 (plain)
  1. #!/bin/sh
  2. # Origin: http://code.google.com/p/lindenb/source/browse/trunk/src/xsl/linkedin2foaf.xsl
  3. # info: http://plindenbaum.blogspot.com/2010/02/linkedinxslt-foaf-people-from.html
  4. set -e
  5. exit1() {
  6. echo "ERROR: $1"
  7. exit 1
  8. }
  9. bindir=$(dirname "$0")
  10. xsltdir="$bindir"
  11. turtle2foaf() {
  12. inpath="$1"
  13. outpath="$2"
  14. [ -n "$outpath" ] || outpath=$(echo "$inpath" | perl -pe 's/\.ttl$/.rdf/ or exit 1') || exit1 "Failed resolving output RDF file from input Turtle file \"$inpath\"."
  15. [ -e "$inpath" ] || exit1 "Turtle file \"$inpath\" does not exist."
  16. # [ ! -e "$outpath" ] || exit1 "RDF file \"$outpath\" already exists."
  17. base="$(perl -ne '/^\@base\s+<(http.+)>/ and print $1 and exit;' "$inpath")" || true
  18. rapper ${base:+-I "$base"} -i turtle -o rdfxml-abbrev "$inpath" > "$outpath"
  19. foafsign "$outpath"
  20. }
  21. linkedin2foaf() {
  22. inpath="$1"
  23. outpath="$2"
  24. [ -n "$outpath" ] || outfile=index.rdf && outpath="$(dirname "$inpath")/linkedin/$outfile"
  25. outdir=$(dirname "$outpath")
  26. tmppath="$outdir/index.html"
  27. [ -e "$inpath" ] || exit1 "Turtle file \"$inpath\" does not exist."
  28. [ ! -e "$tmppath" ] || exit1 "Tempfile \"$tmppath\" already exists."
  29. # TODO: support homepage as fallback for accountName
  30. # id=$(perl -0 -ne '/foaf:accountServiceHomepage\s+<http:\/\/www.linkedin.com\/>\s+;\s+foaf:(?:homepage\s+<(?=http)|accountName\s+")([^<"\s]+)/ and print $1 and exit;' "$inpath") #'
  31. id=$(perl -0 -ne '/^:me.*?foaf:accountServiceHomepage\s+<http:\/\/www.linkedin.com\/>\s+;\s+foaf:accountName\s+"([^<"\s]+)/ms and print $1 and exit;' "$inpath") #'
  32. [ -n "$id" ] || exit1 "Failed to resolve LinkedIn account name."
  33. mkdir -p "$outdir"
  34. # work around unescaped &'s in linkedin pages
  35. # xsltproc --html "$bindir/linkedin2foaf.xsl" "http://www.linkedin.com/in/$id" > "$outpath"
  36. wget -q -O "$tmppath" "http://www.linkedin.com/in/$id"
  37. perl -i -pe 's/&([a-zA-Z0-9]+=)/&amp;$1/g' "$tmppath"
  38. xsltproc --html "$xsltdir/linkedin2foaf.xsl" "$tmppath" > "$outpath"
  39. rm -f "$tmppath"
  40. foafsign "$outpath"
  41. }
  42. tidyfacebookfoaf() {
  43. inpath="$1"
  44. outpath="$2"
  45. [ -e "$inpath" ] || exit1 "Facebook Exporter RDF file \"$inpath\" does not exist."
  46. perl "$bindir/fbfixup.pl" "$inpath" > "$outpath"
  47. foafsign "$outpath"
  48. }
  49. foafsign() {
  50. inpath="$1"
  51. outpath="$inpath.asc"
  52. gpg -a -o- --detach-sign "$inpath" > "$outpath"
  53. }
  54. paths="$@"
  55. [ -n "$paths" ] || paths=index.ttl
  56. for path in $paths; do
  57. basedir=$(dirname "$path")
  58. turtle2foaf "$path"
  59. linkedin2foaf "$path" || true
  60. tidyfacebookfoaf "$basedir/facebook/foaf.xml" "$basedir/facebook/index.rdf" || true
  61. done