Skip to content

Commit

Permalink
feat(opml): define opml struct
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Jul 8, 2019
1 parent 2d7bb4d commit 0d12aa4
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
13 changes: 13 additions & 0 deletions bot/bot.go
Expand Up @@ -593,4 +593,17 @@ func makeHandle() {
}
}
})

B.Handle(tb.OnDocument, func(m *tb.Message) {

if m.Document.MIME == "text/x-opml+xml" {

url, _ := B.FileURLByID(m.Document.FileID)
_, _ = GetOPMLByURL(url)

} else {
_, _ = B.Send(m.Chat, "如果需要导入订阅,请发送正确的OPML文件。")
}

})
}
99 changes: 99 additions & 0 deletions bot/opml.go
@@ -0,0 +1,99 @@
package bot

import (
"crypto/tls"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)

type OPML struct {
XMLName xml.Name `xml:"opml"`
Version string `xml:"version,attr"`
Head Head `xml:"head"`
Body Body `xml:"body"`
}

type Head struct {
Title string `xml:"title"`
DateCreated string `xml:"dateCreated,omitempty"`
DateModified string `xml:"dateModified,omitempty"`
OwnerName string `xml:"ownerName,omitempty"`
OwnerEmail string `xml:"ownerEmail,omitempty"`
OwnerID string `xml:"ownerId,omitempty"`
Docs string `xml:"docs,omitempty"`
ExpansionState string `xml:"expansionState,omitempty"`
VertScrollState string `xml:"vertScrollState,omitempty"`
WindowTop string `xml:"windowTop,omitempty"`
WindowBottom string `xml:"windowBottom,omitempty"`
WindowLeft string `xml:"windowLeft,omitempty"`
WindowRight string `xml:"windowRight,omitempty"`
}

type Body struct {
Outlines []Outline `xml:"outline"`
}

type Outline struct {
Outlines []Outline `xml:"outline"`
Text string `xml:"text,attr"`
Type string `xml:"type,attr,omitempty"`
IsComment string `xml:"isComment,attr,omitempty"`
IsBreakpoint string `xml:"isBreakpoint,attr,omitempty"`
Created string `xml:"created,attr,omitempty"`
Category string `xml:"category,attr,omitempty"`
XMLURL string `xml:"xmlUrl,attr,omitempty"`
HTMLURL string `xml:"htmlUrl,attr,omitempty"`
URL string `xml:"url,attr,omitempty"`
Language string `xml:"language,attr,omitempty"`
Title string `xml:"title,attr,omitempty"`
Version string `xml:"version,attr,omitempty"`
Description string `xml:"description,attr,omitempty"`
}

func NewOPML(b []byte) (*OPML, error) {
var root OPML
err := xml.Unmarshal(b, &root)
if err != nil {
return nil, err
}

return &root, nil
}

func GetOPMLByURL(file_url string) (*OPML, error) {

proxy, _ := url.Parse("socks5://" + socks5Proxy)
tr := &http.Transport{
Proxy: http.ProxyURL(proxy),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Timeout: time.Second * 5,
}
resp, err := client.Get(file_url)
if err != nil {
return nil, err
}
body, _ := ioutil.ReadAll(resp.Body)
o, err := NewOPML(body)
fmt.Println(string(body))

if err != nil {
return nil, err
}
return o, err
}

func (doc OPML) Outlines() []Outline {
return doc.Body.Outlines
}

func (doc OPML) XML() (string, error) {
b, err := xml.MarshalIndent(doc, "", "\t")
return xml.Header + string(b), err
}

0 comments on commit 0d12aa4

Please sign in to comment.