Last active
July 8, 2024 21:03
-
-
Save inv2004/026231b5e25f3fe0d85b1ba03cd5ef3d to your computer and use it in GitHub Desktop.
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
import std/httpclient | |
import std/net | |
import std/strutils | |
import std/json | |
import std/re | |
var client = newHttpClient(sslContext=newContext(verifyMode=CVerifyNone)) | |
const siteUrl = "https://mysport.photo" | |
const extEvent = "capital2024" | |
const urlEvent = siteUrl & "/public/photo-service/api/v1/events?externalEventId=" & extEvent | |
const tokenUrl = siteUrl & "/public/competitor-service/api/v1/competitors/" | |
# https://mysport.photo/public/photo-service/api/v1/events?tenantId=fed7b705-acfd-4576-88a3-e3ebb74268b2&externalEventId=capital2024&organizerId=&searchString= | |
# const siteUrl = "https://marathon-photo.ru" | |
# const extEvent = "2024-gran-fondo-pereslavl-zalesskii" | |
# const urlEvent = siteUrl & "/competitions/" & extEvent & "/instant_search_results/bib_number/all/" | |
let pageRe = re"^.*""\?page=(\d+)"".*$" | |
let detailRe = re"^.*""(/photos/detail/\d+)/"">.*$" | |
let srcRe = re"^.* src='(.+)'.*$" | |
proc mysport() = | |
let eventId = client.getContent(urlEvent).parseJson()["values"][0]["eventId"].getStr() | |
# echo tokenUrl & eventId & "/token" | |
# let token = client.postContent(tokenUrl & eventId & "/token", "{}").parseJson() | |
let token = client.request(tokenUrl & eventId & "/token", HttpPost, "{}", newHttpHeaders({"Content-Type": "application/json"})) | |
.body().parseJson()["value"].getStr() | |
var html = "" | |
var offset = 0 | |
const size = 50 | |
while true: | |
let urlPhotos = "https://mysport.photo/public/photo-service/api/v1/photos?albumId=" & eventId & "&offset=" & $offset & "&count=" & $size | |
let j = client.request(urlPhotos, HttpGet, "", newHttpHeaders({"Content-Type": "application/json", "Authorization": "Bearer " & token})).body().parseJson() | |
html.add "<p/><b>Page " & $(1 + offset div size) & "</b><p/>\n" | |
for v in j["values"]: | |
let res = v["resources"] | |
html.add "<a href=\"" & res["watermark"].getStr() & "\"><img src=\"" & res["preview"].getStr() & "\"/></a>\n" | |
let total = j["total"].getInt() | |
offset += 100 | |
if offset > total: | |
break | |
echo offset, " / ", total | |
let file = "/mnt/c/Users/u/Downloads/" & extEvent & ".html" | |
echo file | |
writeFile(file, html) | |
proc marathon() = | |
var html = "" | |
var page = 1 | |
var pageMax = 1 | |
var subs = @[""] | |
var detail = "" | |
while page <= pageMax: | |
echo "Processing page ", page, " of ", pageMax | |
html.add "<p/><b>Page " & $page & "</b><p/>\n" | |
for line in client.getContent(urlEvent & "?page=" & $page).splitLines(): | |
if line.match(detailRe, subs): | |
detail = subs[0] | |
elif line.match(srcRe, subs): | |
html.add "<a href=\"" & siteUrl & detail & "\"><img src=\"" & subs[0] & "\" width=\"19%\"/></a>\n" | |
elif line.match(pageRe, subs): | |
pageMax = max(subs[0].parseInt, pageMax) | |
page.inc | |
let file = "/mnt/c/Users/u/Downloads/" & extEvent & ".html" | |
echo file | |
writeFile("/mnt/c/Users/u/Downloads/" & extEvent & ".html", html) | |
proc main() = | |
if "mysport" in urlEvent: | |
mysport() | |
elif "marathon-photo" in urlEvent: | |
marathon() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment