Skip to content

Instantly share code, notes, and snippets.

@lynndylanhurley
Created March 27, 2013 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lynndylanhurley/5257713 to your computer and use it in GitHub Desktop.
Save lynndylanhurley/5257713 to your computer and use it in GitHub Desktop.
package controllers
import (
"github.com/kr/pretty"
"github.com/robfig/revel"
"io"
"io/ioutil"
"net/http"
"net/url"
)
type Application struct {
*revel.Controller
}
type Bug struct {
Id, Link, Title, Description string
}
func (c Application) Index() revel.Result {
return c.Render()
}
func (c Application) Scraper() revel.Result {
// account login data
// TODO: pull username / password from session vars
jar := &myjar{}
jar.jar = make(map[string][]*http.Cookie)
client := http.Client{nil, nil, jar}
// populate cookie jar with session vars
establishClientSession(&client)
body := logIn(&client)
return c.RenderText(body)
}
func establishClientSession(c *http.Client) {
// capture first cookie
req, _ := http.NewRequest(
"GET",
"https://www.elementool.com/services/loginpage/login.aspx",
nil,
)
prepareHeader(req)
// get, parse response
c.Do(req)
}
func logIn(c *http.Client) string {
formData := url.Values{
"accountname": {"xxx"},
"username": {"xxx"},
"password": {"xxx"},
"actiontype": {"Submit"},
"isDoUpgrade": {""},
"isBacklog": {""},
"isDoSubscribe": {""},
"fileid": {""},
}
// build request
req, _ := http.NewRequest(
"POST",
"https://www.elementool.com/Services/loginPage/enter.aspx?source=f",
nil,
)
prepareHeader(req)
req.Header.Set("Referer", "https://www.elementool.com/services/loginpage/login.aspx")
req.Header.Set("Pragma", "no-cache")
req.Form = formData
resp, err := c.Do(req)
if err != nil {
pretty.Printf("@-->error %#v", err)
}
pretty.Printf("@-->second response")
pretty.Printf("%# v", resp)
return ioToString(resp.Body)
}
func ioToString(r io.Reader) string {
bs, _ := ioutil.ReadAll(r)
return string(bs)
}
// cookie jar
type myjar struct {
jar map[string][]*http.Cookie
}
func (p *myjar) SetCookies(u *url.URL, cookies []*http.Cookie) {
pretty.Printf("The URL is : %s\n", u.String())
pretty.Printf("The cookie being set is : %s\n", cookies)
p.jar[u.Host] = cookies
}
func (p *myjar) Cookies(u *url.URL) []*http.Cookie {
pretty.Printf("The URL is : %s\n", u.String())
pretty.Printf("Cookie being returned is : %s\n", p.jar[u.Host])
return p.jar[u.Host]
}
func (c Application) BugList() revel.Result {
b := []Bug{Bug{
Id: "123456",
Link: "http://google.com",
Title: "Like / Send Buttons Causing Design Issues",
Description: "- Bottom of the send box for the S Voice gets cut off (GS III Feature) (screenshot available in ticket #15973) - Send buttons for the accessories of the GSIII have their bottoms cut off (IE9) - The send dialog box for the NFC/Android Beam gets cut off at the bottom (Nexus Feature) (IE9, screenshot in ticket #15974) - In the send dialog for the flip coer (Note Accessory), the send button from the accessory on the right shows through the box (screenshot in ticket #15976)",
}}
return c.RenderJson(b)
}
// pretend to be from a firefox browser
func prepareHeader(req *http.Request) *http.Request {
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0) Gecko/20100101 Firefox/9.0")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Set("Accept-Encoding", "gzip,deflate,sdch")
req.Header.Set("Accept-Language", "en-US,en;q=0.8")
req.Header.Set("Cache-Control", "max-age=0")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Origin", "https://www.elementool.com")
req.Header.Set("Host", "www.elementool.com")
req.Header.Set("DNT", "1")
return req
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment