hcard.go 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
package federation
//
// GangGo Diaspora Federation Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
  "strings"
  "github.com/PuerkitoBio/goquery"
)

type Hcard struct {
  Guid string
  Nickname string
  FullName string
  Searchable bool
  PublicKey string
  FirstName string
  LastName string
  Url string
  Photo string
  PhotoMedium string
  PhotoSmall string
}

func (h *Hcard) Fetch(endpoint string) error {
  resp, err := FetchHtml("GET", endpoint, nil)
  if err != nil {
    return err
  }

  doc, err := goquery.NewDocumentFromResponse(resp)
  if err != nil {
    return err
  }

  doc.Find(".entity_uid").Each(
  func(i int, s *goquery.Selection) {
    (*h).Guid = s.Find("span").Text()
  })
  doc.Find(".entity_nickname").Each(
  func(i int, s *goquery.Selection) {
    (*h).Nickname = s.Find("span").Text()
  })
  doc.Find(".entity_full_name").Each(
  func(i int, s *goquery.Selection) {
    (*h).FullName = s.Find("span").Text()
  })
  doc.Find(".entity_first_name").Each(
  func(i int, s *goquery.Selection) {
    (*h).FirstName = s.Find("span").Text()
  })
  doc.Find(".entity_family_name").Each(
  func(i int, s *goquery.Selection) {
    (*h).LastName = s.Find("span").Text()
  })
  doc.Find(".entity_key").Each(
  func(i int, s *goquery.Selection) {
    (*h).PublicKey = s.Find("pre").Text()
  })
  doc.Find(".entity_url").Each(
  func(i int, s *goquery.Selection) {
    (*h).Url = s.Find("a").Text()
  })

  doc.Find(".entity_searchable").Each(
  func(i int, s *goquery.Selection) {
    var searchable bool = false
    if s.Find("span").Text() == "true" {
      searchable = true
    }
    (*h).Searchable = searchable
  })

  doc.Find(".entity_photo").Each(
  func(i int, s *goquery.Selection) {
    nodes := s.Find("img")
    for _, node := range nodes.Nodes {
      for _, attr := range node.Attr {
        if attr.Key == "src" {
          value := attr.Val
          if !strings.HasPrefix(value, "http") {
            value = h.Url + value
          }
          (*h).Photo = value
        }
      }
    }
  })
  doc.Find(".entity_photo_medium").Each(
  func(i int, s *goquery.Selection) {
    nodes := s.Find("img")
    for _, node := range nodes.Nodes {
      for _, attr := range node.Attr {
        if attr.Key == "src" {
          value := attr.Val
          if !strings.HasPrefix(value, "http") {
            value = h.Url + value
          }
          (*h).PhotoMedium = value
        }
      }
    }
  })
  doc.Find(".entity_photo_small").Each(
  func(i int, s *goquery.Selection) {
    nodes := s.Find("img")
    for _, node := range nodes.Nodes {
      for _, attr := range node.Attr {
        if attr.Key == "src" {
          value := attr.Val
          if !strings.HasPrefix(value, "http") {
            value = h.Url + value
          }
          (*h).PhotoSmall = value
        }
      }
    }
  })
  return nil
}