Last active
March 5, 2019 19:26
-
-
Save gokaybiz/f6a5353f4a48f61be65953ed62ff6fb4 to your computer and use it in GitHub Desktop.
Itu kutuphane otomasyon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/http/cookiejar" | |
"os" | |
"strings" | |
"time" | |
) | |
var ( | |
user = "name=Necmi%2C+Duduk&code=123123123&pin=12313&submit.x=0&submit.y=0" | |
renew = "requestRenewAll=requestRenewAll¤tsortorder=current_duedate¤tsortorder=current_duedate" | |
renewStep2 = "currentsortorder=current_duedate&renewall=YES¤tsortorder=current_duedate" | |
client *http.Client | |
) | |
func callITU(cl *http.Client, postData, url string) string { | |
body := strings.NewReader(postData) | |
req, err := http.NewRequest("POST", url, body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req.Header.Set("Connection", "keep-alive") | |
req.Header.Set("Pragma", "no-cache") | |
req.Header.Set("Cache-Control", "no-cache") | |
req.Header.Set("Origin", "https://divit.library.itu.edu.tr") | |
req.Header.Set("Upgrade-Insecure-Requests", "1") | |
req.Header.Set("Dnt", "1") | |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36") | |
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") | |
req.Header.Set("Referer", url) | |
req.Header.Set("Accept-Language", "en-US,en;q=0.9,tr;q=0.8") | |
resp, err := cl.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
bodyBytes, _ := ioutil.ReadAll(resp.Body) | |
bodyString := string(bodyBytes) | |
return bodyString | |
} | |
func doLogin(cl *http.Client) bool { | |
call := callITU(cl, user, "https://divit.library.itu.edu.tr/patroninfo~S0") | |
if strings.Contains(call, "You are logged into ITU Library Services") { | |
return true | |
} | |
return false | |
} | |
func doRenew(cl *http.Client, postData, postData2 string) bool { | |
var reqTo string = "https://divit.library.itu.edu.tr/patroninfo/123123123/items" | |
call := callITU(cl, postData, reqTo) | |
if !strings.Contains(call, "You are logged into ITU Library Services") { | |
_ = doLogin(cl) | |
return doRenew(cl, postData, postData2) | |
} | |
if strings.Contains(call, "will be renewed") { | |
call = callITU(cl, postData2, reqTo) | |
if strings.Contains(call, "RENEWED") { | |
return true | |
} else { | |
return false | |
} | |
} else { | |
return false | |
} | |
return false | |
} | |
func renewHandler(w http.ResponseWriter, r *http.Request) { | |
check := doRenew(client, renew, renewStep2) | |
if check { | |
w.Write(([]byte("<title>SUCCESS</title>\nYENILENDI"))) | |
} else { | |
w.Write(([]byte("<title>FAILED</title>\nYENILENEMEDI"))) | |
} | |
} | |
func determineListenAddress() (string, error) { | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "3137" | |
} | |
return ":" + port, nil | |
} | |
func main() { | |
cookieJar, _ := cookiejar.New(nil) | |
tr := &http.Transport{ | |
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | |
} | |
client = &http.Client{ | |
Jar: cookieJar, | |
Transport: tr, | |
} | |
http.HandleFunc("/renew", renewHandler) | |
go func() { | |
addr, err := determineListenAddress() | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Fatal(http.ListenAndServe(addr, nil)) | |
}() | |
fmt.Println("Server baslatildi!") | |
//program ilk calistiginda kavanoza cerez salla | |
_ = doLogin(client) | |
func() { | |
for { | |
time.Sleep(time.Second) | |
} | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment