Browse Source

move into separte files

tags/3
Roshless 3 months ago
parent
commit
836882788a
5 changed files with 162 additions and 135 deletions
  1. 32
    0
      cookies.go
  2. 0
    135
      main.go
  3. 66
    0
      requests.go
  4. 37
    0
      slices.go
  5. 27
    0
      variables.go

+ 32
- 0
cookies.go View File

@@ -0,0 +1,32 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)

func loadCookieFile() string {
dat, err := ioutil.ReadFile(cookieFileName)
if err != nil {
log.Fatalln(err)
}
return strings.TrimSuffix(string(dat), "\n")
}

func createCookieFile() {
defaultHelpMessage := `go to random giveway, press f12 on firefox, something something inspector on chrome probably and
enter/leave giveway, click on the thing that pops up (POST, 200, REQUEST or something) and look for PHPSESSID=characters,
copy those into this file, without any formatting just characters`

if _, err := os.Stat(cookieFileName); os.IsNotExist(err) {
err := ioutil.WriteFile(cookieFileName, []byte(defaultHelpMessage), 0644)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Edit " + cookieFileName + " file")
os.Exit(0)
}
}

+ 0
- 135
main.go View File

@@ -2,150 +2,15 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
)

var (
mainURL = "https://www.steamgifts.com"
pagingURL = "https://www.steamgifts.com/giveaways/search?page="
wishlistURL = "&type=wishlist"
regionRestrictedURL = "&region_restricted=true"
bannedString = "Account has been temporarily blocked"

userAgent = "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"

mainRegex = regexp.MustCompile(`href="\/giveaway\/(?P<Code>[a-zA-Z0-9]*?)\/[^\/]*?"`)
xsrfRegex = regexp.MustCompile(`name="xsrf_token" value="(?P<XSRF>[0-9a-zA-Z]*?)"`)
pointsRegex = regexp.MustCompile(`<span class=\"nav__points\">(?P<Points>[0-9]{1,3})</span>`)
pointsResponseRegex = regexp.MustCompile(`\"points\":\"(?P<Points>[0-9]+)\"`)

cookieFileName = ".steamgifts_cookie"
cookieName = "PHPSESSID"

timeToSleepFor = time.Hour * 5
timeToRunFor = time.Hour * 168
)

func loadCookieFile() string {
dat, err := ioutil.ReadFile(cookieFileName)
if err != nil {
log.Fatalln(err)
}
return strings.TrimSuffix(string(dat), "\n")
}

func createCookieFile() {
defaultHelpMessage := `go to random giveway, press f12 on firefox, something something inspector on chrome probably and
enter/leave giveway, click on the thing that pops up (POST, 200, REQUEST or something) and look for PHPSESSID=characters,
copy those into this file, without any formatting just characters`

if _, err := os.Stat(cookieFileName); os.IsNotExist(err) {
err := ioutil.WriteFile(cookieFileName, []byte(defaultHelpMessage), 0644)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Edit " + cookieFileName + " file")
os.Exit(0)
}
}

func getResultsPage(url string, cookie *http.Cookie) string {
client := &http.Client{}

request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatalln(err)
}

request.Header.Add("User-Agent", userAgent)
request.AddCookie(cookie)

resp, err := client.Do(request)
if err != nil {
log.Fatalln(err)
}

if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}

return string(bodyBytes)
}
return ""
}

func sendRequest(url string, cookie *http.Cookie, postData *url.Values) string {
if postData == nil {
log.Fatalln("Empty form")
}
client := &http.Client{}

request, err := http.NewRequest(http.MethodPost, url, strings.NewReader(postData.Encode()))
if err != nil {
log.Fatalln(err)
}

request.Header.Add("User-Agent", userAgent)
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := client.Do(request)
if err != nil {
log.Fatalln(err)
}

if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
return string(bodyBytes)
}
return ""
}

func removeDuplicatesUnordered(elements []string) []string {
encountered := map[string]bool{}

// Create a map of all unique elements.
for v := range elements {
encountered[elements[v]] = true
}

// Place all keys from the map into a slice.
result := []string{}
for key := range encountered {
result = append(result, key)
}
return result
}

func convert2DsliceTo1D(sliceTwoDim [][]string) []string {
var returnSlice []string
for _, element := range sliceTwoDim {
returnSlice = append(returnSlice, element[1]) // ignore
}
return returnSlice
}

func doStuff(cookie *http.Cookie) {
log.Println("Entering giveaways...")
enterGiveaways(cookie, true)
enterGiveaways(cookie, false)
log.Println("Sleeping for " + timeToSleepFor.String())
}

func enterGiveaways(cookie *http.Cookie, enterWishlist bool) {
if enterWishlist {
fmt.Println("wishlist")

+ 66
- 0
requests.go View File

@@ -0,0 +1,66 @@
package main

import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)

func getResultsPage(url string, cookie *http.Cookie) string {
client := &http.Client{}

request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatalln(err)
}

request.Header.Add("User-Agent", userAgent)
request.AddCookie(cookie)

resp, err := client.Do(request)
if err != nil {
log.Fatalln(err)
}

if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}

return string(bodyBytes)
}
return ""
}

func sendRequest(url string, cookie *http.Cookie, postData *url.Values) string {
if postData == nil {
log.Fatalln("Empty form")
}
client := &http.Client{}

request, err := http.NewRequest(http.MethodPost, url, strings.NewReader(postData.Encode()))
if err != nil {
log.Fatalln(err)
}

request.Header.Add("User-Agent", userAgent)
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := client.Do(request)
if err != nil {
log.Fatalln(err)
}

if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
return string(bodyBytes)
}
return ""
}

+ 37
- 0
slices.go View File

@@ -0,0 +1,37 @@
package main

import (
"log"
"net/http"
)

func convert2DsliceTo1D(sliceTwoDim [][]string) []string {
var returnSlice []string
for _, element := range sliceTwoDim {
returnSlice = append(returnSlice, element[1]) // ignore
}
return returnSlice
}

func doStuff(cookie *http.Cookie) {
log.Println("Entering giveaways...")
enterGiveaways(cookie, true)
enterGiveaways(cookie, false)
log.Println("Sleeping for " + timeToSleepFor.String())
}

func removeDuplicatesUnordered(elements []string) []string {
encountered := map[string]bool{}

// Create a map of all unique elements.
for v := range elements {
encountered[elements[v]] = true
}

// Place all keys from the map into a slice.
result := []string{}
for key := range encountered {
result = append(result, key)
}
return result
}

+ 27
- 0
variables.go View File

@@ -0,0 +1,27 @@
package main

import (
"regexp"
"time"
)

var (
mainURL = "https://www.steamgifts.com"
pagingURL = "https://www.steamgifts.com/giveaways/search?page="
wishlistURL = "&type=wishlist"
regionRestrictedURL = "&region_restricted=true"
bannedString = "Account has been temporarily blocked"

userAgent = "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"

mainRegex = regexp.MustCompile(`href="\/giveaway\/(?P<Code>[a-zA-Z0-9]*?)\/[^\/]*?"`)
xsrfRegex = regexp.MustCompile(`name="xsrf_token" value="(?P<XSRF>[0-9a-zA-Z]*?)"`)
pointsRegex = regexp.MustCompile(`<span class=\"nav__points\">(?P<Points>[0-9]{1,3})</span>`)
pointsResponseRegex = regexp.MustCompile(`\"points\":\"(?P<Points>[0-9]+)\"`)

cookieFileName = ".steamgifts_cookie"
cookieName = "PHPSESSID"

timeToSleepFor = time.Hour * 5
timeToRunFor = time.Hour * 168
)

Loading…
Cancel
Save