Пример #1
0
Файл: botan.go Проект: cmr/sdk
func createRequest(getp sender, payload interface{}) *gorequest.SuperAgent {
	return gorequest.New().
		Post(URL).
		DisableKeepAlives(true).
		CloseRequest(true).
		Query(getp).  // Get parameters
		Send(payload) // Post payload
}
Пример #2
0
func getPetition(url string, queries []string) (string, error) {
	req := gorequest.New().
		DisableKeepAlives(true).
		CloseRequest(true).
		Get(url)

	for _, q := range queries {
		req.Query(q)
	}
	_, body, errq := req.End()
	if errq != nil {
		return "", errors.New("There were some error trying to do the petition")
	}
	return body, nil
}
Пример #3
0
func (bot TgBot) DownloadFilePathReader(path string) (io.ReadCloser, error) {
	url := bot.buildFilePath(path)
	resp, _, errq := gorequest.New().
		DisableKeepAlives(true).
		CloseRequest(true).
		Get(url).
		End()
	if errq != nil {
		if len(errq) > 0 {
			return nil, errq[0]
		} else {
			return nil, errors.New("Error in GET petition")
		}
	}

	return resp.Body, nil
}
Пример #4
0
func postPetition(url string, payload interface{}, ctype *string) (string, error) {
	request := gorequest.New().
		DisableKeepAlives(true).
		CloseRequest(true).
		Post(url).
		Send(payload)
	request.TargetType = "form"

	if ctype != nil {
		request.Set("Content-Type", *ctype)
	}

	_, body, err := request.End()

	if err != nil {
		return "", errors.New("Some error happened")
	}
	return body, nil
}
Пример #5
0
func other_soundcloud(uri UrlInfo, kind TypeMedia) UrlInfo {
	request := gorequest.New().
		Post("http://soundflush.com/").
		Send(struct {
			TrackUrl string `json:"track_url"`
			BtnDl    string `json:"btn_download"`
		}{uri.Url, "Download"})
	request.TargetType = "form"
	_, body, err := request.End()

	if len(err) > 0 {
		return new_soundcloud(uri, kind)
	}

	reg := regexp.MustCompile(`<a .*(?:download=".+"|href="([^"]+)").*(?:download=".+"|href="([^"]+)").*>`)
	allu := reg.FindStringSubmatch(body)
	if len(allu) <= 2 {
		return new_soundcloud(uri, kind)
	}
	uri.Url = allu[2]
	return uri
}
Пример #6
0
func (w Worker) Start() {
	go func() {
		log.Printf("Started worker %d", w.ID)
		for {
			w.WorkerQueue <- w.Work
			select {
			case work := <-w.Work:
				urlcachekind := work.Kind.WithUrl(work.OriginalUrl)
				id, ok := cacheids.Get(urlcachekind)
				if ok {
					log.Println("Founded in cache (in processor)!")
					work.Bot.Send(work.Id).Document(id).End()
					continue
				}

				// log.Printf("Received work %v\n", work)
				res, _, err := gorequest.New().
					DisableKeepAlives(true).
					CloseRequest(true).
					Get(work.Url).
					AddCookies(work.Cookies).
					End()
				// res, err := http.Get(work.Url)
				if err != nil {
					log.Println(err)
					work.AnswerChannel <- WorkAnswer{ErrorDownloading}
					continue
				}
				// Know if document/video/image?
				ans := work.Bot.Send(work.Id).Document(tgbot.ReaderSender{res.Body, work.Name}).End()
				res.Body.Close()

				if ans.Ok && ans.Result != nil {
					result := *ans.Result
					fileid := ""
					if result.Document != nil {
						fileid = result.Document.FileID
					}
					if result.Audio != nil {
						fileid = result.Audio.FileID
					}
					if result.Video != nil {
						fileid = result.Video.FileID
					}
					if fileid != "" {
						urlfmt := work.Kind.WithUrl(work.OriginalUrl)
						cacheids.Set(urlfmt, fileid, cache.DefaultExpiration)
					}
				}
				// Just to avoid non reading sender channel
				select {
				case work.AnswerChannel <- WorkAnswer{OkDownloading}:
				case <-time.After(time.Second * 1):
				}
			case cans := <-w.QuitChan:
				log.Printf("Stopping worker %d\n", w.ID)
				// Notify me are finished!
				cans <- true
				return
			}
		}
	}()
}
Пример #7
0
func uploadboy(uri UrlInfo) UrlInfo {
	up, err := url.Parse(uri.Url)
	if err != nil {
		fmt.Println("Error parsing URL")
		return uri
	}

	fid := strings.TrimSuffix(strings.TrimRight(strings.TrimLeft(up.RequestURI(), "/"), "/"), ".html")

	_, body, errs := gorequest.New().
		DisableKeepAlives(true).
		CloseRequest(true).
		Get(uri.Url).
		End()
	if errs != nil {
		fmt.Println("Error in GET request")
		return uri
	}

	rfname := regexp.MustCompile(`<input (?:type="hidden"|name="fname"|value="([^"]+)") (?:value="([^"]+)"|name="fname"|type="hidden") (?:name="fname"|type="hidden"|value="([^"]+)")>`)
	if !rfname.MatchString(body) {
		fmt.Println("Error, first match don't do it")
		return uri
	}

	all := rfname.FindStringSubmatch(body)
	if len(all) < 2 {
		fmt.Println("Error, all < 2,", all)
		return uri
	}

	fname := ""
	for _, fni := range all[1:] {
		if fname == "" {
			fname = fni
		}
	}
	if fname == "" {
		fmt.Println("I didn't detect any fname :(")
		return uri
	}

	uri.Name = fname

	data1 := struct {
		Op         string `json:"op"`
		UsrLogin   string `json:"usr_login"`
		ID         string `json:"id"`
		Fname      string `json:"fname"`
		Referer    string `json:"referer"`
		MethodFree string `json:"method_free"`
	}{
		Op:         "download1",
		UsrLogin:   "",
		ID:         fid,
		Fname:      fname,
		Referer:    "",
		MethodFree: "Free+Download",
	}

	r := gorequest.New().
		DisableKeepAlives(true).
		CloseRequest(true).
		Post(uri.Url).
		Send(data1)
	r.TargetType = "form"
	_, body, errs = r.End()
	if errs != nil {
		fmt.Println("Error in POST download1")
		return uri
	}

	if !strings.Contains(body, "Create") {
		fmt.Println("Error, body don't have Create")
		return uri
	}

	data := struct {
		Op            string `json:"op"`
		ID            string `json:"id"`
		Rand          string `json:"rand"`
		Referer       string `json:"referer"`
		UsrRes        string `json:"usr_resolution"`
		UsrOS         string `json:"usr_os"`
		UsrBrowser    string `json:"usr_browser"`
		MethodFree    string `json:"method_free"`
		MethodPremium string `json:"method_premium"`
		DownScript    string `json:"down_script"`
	}{
		Op:            "download2",
		ID:            fid,
		Rand:          "",
		Referer:       uri.Url,
		UsrRes:        "1366x768",
		UsrOS:         "Linux+x86_64",
		UsrBrowser:    "Firefox+41",
		MethodFree:    "Free+Download",
		MethodPremium: "",
		DownScript:    "1",
	}

	r = gorequest.New().
		DisableKeepAlives(true).
		CloseRequest(true).
		Post(uri.Url).
		Send(data)
	r.TargetType = "form"
	_, body, errs = r.End()
	if errs != nil {
		fmt.Println("Error in POST download2")
		return uri
	}
	rurl := regexp.MustCompile(`<span\s*class="hidden-xs" style="[^"]+">\s*<a href="([^"]+)">\s*[^<]+\s*<\/a>\s*<\/span>`)
	if !rurl.MatchString(body) {
		fmt.Println("Error in match2")
		return uri
	}
	allu := rurl.FindStringSubmatch(body)
	if len(allu) > 1 {
		uri.Url = allu[1]
	}
	return uri
}