-
Notifications
You must be signed in to change notification settings - Fork 8
/
transport_facebook.go
107 lines (77 loc) · 2.02 KB
/
transport_facebook.go
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
package transports
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/headzoo/surf"
"github.com/headzoo/surf/browser"
)
type FacebookTransport struct {
*Transport
Login string
Password string
Friend string
Browser *browser.Browser
Serializer DefaultSerializer
ChatURL string
}
func (t *FacebookTransport) DoLogin() bool {
fmt.Println("FacebookTransport, Login()")
err := t.Browser.Open("https://mobile.facebook.com/")
if err != nil {
panic(err)
}
LoginForm := t.Browser.Forms()[1]
LoginForm.Input("email", t.Login)
LoginForm.Input("pass", t.Password)
if LoginForm.Submit() != nil {
panic(err)
}
err = t.Browser.Open("https://mobile.facebook.com/profile.php")
if err != nil {
panic(err)
}
fmt.Println("Logged in as", t.Browser.Title(), "?")
FriendURL := fmt.Sprintf("https://mobile.facebook.com/%s", t.Friend)
err = t.Browser.Open(FriendURL)
if err != nil {
panic(err)
}
t.Browser.Click("a[href*=\"/messages/thread/\"]")
t.ChatURL = t.Browser.Url().String()
return true
}
func (t *FacebookTransport) Prepare() {
fmt.Println("FacebookTransport, Prepare()")
t.Serializer = DefaultSerializer{}
t.Browser = surf.NewBrowser()
if !t.DoLogin() {
err := errors.New("Authentication error")
panic(err)
}
return
}
func (t *FacebookTransport) Handler(w http.ResponseWriter, originalRequest *http.Request) {
t.Browser.Open(t.ChatURL)
client := &http.Client{}
request, _ := http.NewRequest(originalRequest.Method, originalRequest.URL.String(), nil)
serializedRequest := t.Serializer.Serialize(request, true).([]byte)
fmt.Println("Got", originalRequest)
fmt.Println("Serialized", string(serializedRequest))
MessageForm, _ := t.Browser.Form("#composer_form")
MessageForm.Input("body", string(serializedRequest))
MessageForm.Submit()
resp, _ := client.Do(request)
b, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
w.Write(b)
return
}
func (t *FacebookTransport) Listen() {
fmt.Println("FacebookTransport, Listen()")
t.Prepare()
fmt.Println("Polling...")
for {
}
}