[WEB-API] Bentschi.net (GetIP, GetGeo, GetWeather, ...)

Veröffentliche deine funktionierenden Skripte und Funktionen

Moderator: jNizM

Bentschi
Posts: 22
Joined: 02 Oct 2013, 18:45

[WEB-API] Bentschi.net (GetIP, GetGeo, GetWeather, ...)

28 Aug 2014, 12:50

Hallo,
Hier mal ein kleines neues Projekt das ich aufgrund eines größeren Projektes gestarted habe.
Dieses Projekt gebe ich kostenlos Öffentlich frei und werde es von Zeit zu Zeit mal erweitern.
Bzw. würde ich auch gerne eure Ideen für Erweiterungen/Verbesserungen hören.

Limitierungen:
- Ein Captcha (CreateCaptcha) ist nur max. 24 Stunden lang gültig

Hier das aktuellste Skript dazu:

Code: Select all

GetIP()
;http://api.bentschi.net/ip
{
  return (RegExMatch(BentschiNetAPI("api.bentschi.net/ip"), "([0-9]{1,3}(\.[0-9]{1,3}){3})", m)) ? m1 : ""
}

GetGeo(q="")
;http://api.bentschi.net/geo?[q=<IP-Address>]
{
  return BentschiNetAPI("api.bentschi.net/geo", {q:q})
}

GetWeather(q="") ;or {lat:y, lon:x}
;http://api.bentschi.net/weather?[q=<IP-Address|City/Country/...>]
;http://api.bentschi.net/weather?lat=<Latitude-Coordinate>&lon=<Longitude-Coordinate>
{
  return BentschiNetAPI("api.bentschi.net/weather", (q.lat && q.lon) ? q : {q:q})
}

CreateCaptcha(filename) ;Filetype: PNG
;http://api.bentschi.net/captcha
;http://api.bentschi.net/captchaImage?s=<Seed returned by captcha>
{
  FileDelete, % filename
  o := BentschiNetAPI("api.bentschi.net/captcha/")
  UrlDownloadToFile, % o.img, % filename
  return o.s
}

VerifyCaptcha(s, text)
;http://api.bentschi.net/captchaVerify?s=<Seed returned by captcha>&text=<Captchatext to verify>
{
  return BentschiNetAPI("api.bentschi.net/captchaVerify", {s:s, text:text})
}

BentschiNetAPI(v, d="")
{
  static hex := {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:"A", 11:"B", 12:"C", 13:"D", 14:"E", 15:"F"}
  if (v="postenc")
  {
    VarSetCapacity(s, StrPut(d, "utf-8"), 0), StrPut(d, &s, "cp0")
    while (c := NumGet(s, A_Index-1, "uchar"))
      o .= (RegExMatch(chr(c), "([a-zA-Z0-9])", m)) ? m1 : ("%" hex[(c&0xf0)>>4] hex[c&0x0f])
    return o
  }
  if (v="json")
  {
    if (regexmatch(d, "s)^__chr(A|W):(.*)", m))
    {
      VarSetCapacity(b, 4, 0), NumPut(m2, b, 0, "int")
      return StrGet(&b, 1, (m1="A") ? "cp28591" : "utf-16")
    }
    if (regexmatch(d, "s)^__str:((\\""|[^""])*)", m))
    {
      str := m1
      for p,r in {b:"`b", f:"`f", n:"`n", 0:"", r:"`r", t:"`t", v:"`v", "'":"'", """":"""", "/":"/"}
        str := regexreplace(str, "\\" p, r)
      while (regexmatch(str, "s)^(.*?)\\x([0-9a-fA-F]{2})(.*)", m))
        str := m1 BentschiNetAPI("json", "__chrA:0x" m2) m3
      while (regexmatch(str, "s)^(.*?)\\u([0-9a-fA-F]{4})(.*)", m))
        str := m1 BentschiNetAPI("json", "__chrW:0x" m2) m3
      while (regexmatch(str, "s)^(.*?)\\([0-9]{1,3})(.*)", m))
        str := m1 BentschiNetAPI("json", "__chrA:" m2) m3
      return regexreplace(str, "\\\\", "\")
    }
    str := [], obj := []
    while (RegExMatch(d, "s)^(.*?[^\\])""((\\""|[^""])*?[^\\]|)""(.*)$", m))
      str.insert(BentschiNetAPI("json", "__str:" m2)), d := m1 "__str<" str.maxIndex() ">" m4
    while (RegExMatch(RegExReplace(d, "\s+", ""), "s)^(.*?)(\{|\[)([^\{\[\]\}]*?)(\}|\])(.*)$", m))
    {
      a := (m2="{") ? 0 : 1, c := m3, d := m1 "__obj<" ((obj.maxIndex()+1) ? obj.maxIndex()+1 : 1) ">" m5, tmp := []
      while (RegExMatch(c, "^(.*?),(.*)$", m))
        tmp.insert(m1), c := m2
      tmp.insert(c), tmp2 := {}, obj.insert(cobj := {})
      for k,v in tmp
      {
        if (RegExMatch(v, "^(.*?):(.*)$", m))
          tmp2[m1] := m2
        else
          tmp2.insert(v)
      }
      for k,v in tmp2
      {
        for x,y in str
          k := RegExReplace(k, "__str<" x ">", y), v := RegExReplace(v, "__str<" x ">", y)
        for x,y in obj
          v := RegExMatch(v, "^__obj<" x ">$") ? y : v
        cobj[k] := v
      }
    }
    return obj[obj.maxIndex()]
  }
  o := q := ""
  for k,l in d
    q .= ((q) ? "&" : "") BentschiNetAPI("postenc", k) "=" BentschiNetAPI("postenc", l)
  if (!DllCall("LoadLibrary", "str", "wininet"))
    return
  if (!(hI := DllCall("wininet\InternetOpen", "str", "AutoHotkey/" A_AhkVersion, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
    return
  if (hC := DllCall("wininet\InternetConnect", "ptr", hI, "str", regexreplace(v, "(^http:\/\/|\/.+$)"), "ushort", 80, "ptr", 0, "ptr", 0, "uint", 3, "uint", 0, "uint", 0, "ptr"))
  {
    if (hR := DllCall("wininet\HttpOpenRequest", "ptr", hC, "str", "POST", "str", regexreplace(v, "^(http:\/\/)?.+?\/"), "ptr", 0, "ptr", 0, "ptr", 0, "uint", 0x84280100, "ptr", 0, "ptr"))
    {
      hdr := "Content-Type: application/x-www-form-urlencoded"
      if (DllCall("wininet\HttpSendRequest", "ptr", hR, "str", hdr, "uint", StrLen(hdr), (q) ? "astr" : "ptr", (q) ? q : 0, "uint", StrLen(q)))
      {
        if (DllCall("wininet\HttpQueryInfo", "ptr", hR, "uint", 0x20000000|19, "uint*", sc, "uint*", scs := 4, "uint*", sci := 0) && sc=200)
        {
          while (DllCall("wininet\InternetQueryDataAvailable", "ptr", hR, "uint*", s, "uint", 0, "ptr", 0) && s>0)
          {
            DllCall("wininet\InternetReadFile", "ptr", hR, "ptr", (VarSetCapacity(b, s, 0)) ? &b : 0, "uint", s, "uint*", r)
            o .= StrGet(&b, r, "utf-8")
          }
        }
        else if (sc>=300 && sc<400)
        {
          VarSetCapacity(ql, qls := 4096, 0)
          if (DllCall("wininet\HttpQueryInfo", "ptr", hR, "uint", 33, "ptr", &ql, "uint*", qls, "uint*", qli := 0))
            o := BentschiNetAPI(StrGet(&ql), d)
        }
      }
      DllCall("wininet\InternetCloseHandle", "ptr", hR)
    }
    DllCall("wininet\InternetCloseHandle", "ptr", hC)
  }
  DllCall("wininet\InternetCloseHandle", "ptr", hI)
  return (RegExMatch(o, "^[\{\[]")) ? BentschiNetAPI("json", o) : o
}
GetIP
Spoiler
GetGeo
Spoiler
GetWeather
Spoiler
CreateCaptcha, VerifyCaptcha
Spoiler
Changelog wrote:29.08.2014 - URI-Encoding für Postdaten im Skript hinzugefügt
29.08.2014 - Das Backend erlaubt jetzt neben den POST-Anfragen auch GET, daher können die Anfragen jetzt auch direkt im Browser getestet werden
29.08.2014 - Funktionen angepasst damit die Parameter-Namen ähnlich der der API sind
01.09.2014 - Die URL http://api.bentschi.net linkt jetzt zu diesem Artikel
01.09.2014 - CreateCaptcha und VerifyCaptcha hinzugefügt
* Die Ausgabe wurde mit print_r von SAPlayer formatiert
ruespe
Posts: 26
Joined: 09 Nov 2013, 04:47

Re: [WEB-API] Bentschi.net (GetIP, GetGeo, GetWeather, ...)

01 Sep 2014, 13:21

Schick. Aber fehlt da nicht die BentschiNetAPI()?
Bentschi
Posts: 22
Joined: 02 Oct 2013, 18:45

Re: [WEB-API] Bentschi.net (GetIP, GetGeo, GetWeather, ...)

01 Sep 2014, 14:48

Die ist doch im skript oben dabei

Return to “Skripte und Funktionen”

Who is online

Users browsing this forum: No registered users and 20 guests