C:\$Recycle.Bin\S-1-5-21-3967099092-2644009230-141905953-500\$RHG4MPJ.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!doctype html>
<html lang="de">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title>Countdown bis nächster F1 GP (OpenF1)</title>


  <style>
    body { font-family: system-ui, Arial, sans-serif; display:flex; align-items:center; justify-content:center; height:100vh; margin:0; background:#0b1220; color:#e6eef8; }
    .card { background:#0f1724; padding:2px; border-radius:12px; box-shadow:0 6px 2px rgba(2,6,23,0.6); text-align:center; width:200px; }
    h1 { margin:0 0 10px; font-size:16px; font-weight:200; color:#fff; }
    .race { font-size:16px; font-weight:600; margin-bottom:2px; }
    .time { font-size:20px; letter-spacing:0.6px; margin-bottom:2px; }
    .note { font-size:12px; color:#9fb0d6; }
  </style>


</head>
<body>


  <div class="card">
    <h1>Next Live Session</h1>
    <div class="race" id="raceInfo">loading session info …</div>
    <div class="time" id="countdown">--:--:--:--</div>
    <div class="note" id="local">Local Time</div>
  </div>

  <script>
  const API = 'https://api.openf1.org/v1';

  function parseIso(s) {
    if (!s) return null;
    const d = new Date(s);
    return isNaN(d) ? null : d;
  }

  async function fetchAllUpcomingItems() {
    const tryUrls = [
      `${API}/sessions`,
      `${API}/meetings`,
      `${API}/meetings?future=true`
    ];
    for (const url of tryUrls) {
      try {
        const res = await fetch(url);
        if (!res.ok) continue;
        const data = await res.json();
        if (Array.isArray(data) && data.length) return data;
        if (Array.isArray(data?.sessions) && data.sessions.length) return data.sessions;
        if (Array.isArray(data?.meetings) && data.meetings.length) return data.meetings;
      } catch (e) {
        // next
      }
    }
    throw new Error('no session data available');
  }

  function getDateCandidates(item) {
    const list = [];
    if (item.date_start) list.push(parseIso(item.date_start));
    if (item.date_end) list.push(parseIso(item.date_end));
    if (item.date) list.push(parseIso(item.date));
    if (item.sessions && Array.isArray(item.sessions)) {
      for (const s of item.sessions) {
        if (s.date_start) list.push(parseIso(s.date_start));
        if (s.date) list.push(parseIso(s.date));
      }
    }
    return list.filter(d => d instanceof Date && !isNaN(d));
  }

  function buildCandidates(items) {
    const now = new Date();
    const candidates = [];
    for (const it of items) {
      if (it.session_name || it.type === 'session') {
        const d = parseIso(it.date_start || it.date || it.date_time);
        if (d && d > now) candidates.push({ item: it, date: d, parent: null });
      } else if (it.meeting_key || it.meeting_name || it.sessions) {
        const dates = getDateCandidates(it);
        for (const d of dates) if (d && d > now) candidates.push({ item: it, date: d, parent: null, isMeeting: true });
        if (Array.isArray(it.sessions)) {
          for (const s of it.sessions) {
            const sd = parseIso(s.date_start || s.date);
            if (sd && sd > now) candidates.push({ item: s, date: sd, parent: it });
          }
        }
      } else {
        const d = parseIso(it.date_start || it.date || it.date_time);
        if (d && d > now) candidates.push({ item: it, date: d, parent: null });
      }
    }
    return candidates;
  }

  function pickNextSimple(items) {
    const candidates = buildCandidates(items);
    if (candidates.length === 0) return null;
    candidates.sort((a,b) => a.date - b.date);
    return candidates[0];
  }

  function startCountdown(targetDate, onTick) {
    let interval = null;
    function update() {
      const now = new Date();
      let diff = targetDate - now;
      if (diff < 0) diff = 0;
      const days = Math.floor(diff / (1000*60*60*24));
      const hours = Math.floor((diff%(1000*60*60*24)) / (1000*60*60));
      const minutes = Math.floor((diff%(1000*60*60)) / (1000*60));
      const seconds = Math.floor((diff%(1000*60)) / 1000);
      onTick({ days, hours, minutes, seconds, remainingMs: diff });
      if (diff === 0 && interval !== null) {
        clearInterval(interval);
        interval = null;
      }
    }
    update();
    interval = setInterval(update, 1000);
    return () => { if (interval !== null) { clearInterval(interval); interval = null; } };
  }

  (async function init() {
    const raceInfoEl = document.getElementById('raceInfo');
    const countdownEl = document.getElementById('countdown');
    const localEl = document.getElementById('local');

    try {
      const items = await fetchAllUpcomingItems();
      const picked = pickNextSimple(items);
      if (!picked) throw new Error('Kein zukünftiges Rennen gefunden');

      const obj = picked.item;
      const parent = picked.parent || null;
      const date = picked.date;
      if (!date) throw new Error('no date in picked item');

      console.log('picked:', { picked, obj, parent });

      function resolveSessionName(item, parentObj) {
        const possibleFields = ['session_name','sessionName','session','sessionTitle','session_title','name','title','race_name','raceName'];
        const candidates = [];
        if (item) {
          for (const f of possibleFields) if (item[f]) candidates.push(item[f]);
          if (item.sessions && Array.isArray(item.sessions)) {
            const s = item.sessions.find(s => possibleFields.some(f => s[f]));
            if (s) for (const f of possibleFields) if (s[f]) candidates.push(s[f]);
          }
        }
        if (parentObj) {
          for (const f of possibleFields) if (parentObj[f]) candidates.push(parentObj[f]);
        }
        return candidates.find(Boolean) || null;
      }

      const sessionName = resolveSessionName(obj, parent);
      // Meeting_Name bevorzugen; falls nicht vorhanden, fallback auf meeting_key
      const meetingName = (parent && (parent.meeting_name || parent.title)) || obj.meeting_name || obj.title || obj.country_name || null;
      const country = (parent && (parent.country || parent.location_country)) || obj.country || obj.location_country || '';

      let displayName = '';
      if (meetingName) {
        displayName = meetingName;
        if (sessionName) displayName += ' - ' + sessionName;
      } else {
        displayName = sessionName || 'next session';
      }

      // Anzeige ohne Location/Circuit
      raceInfoEl.textContent = `${displayName}${country ? ' ('+country+')' : ''}`;
      localEl.textContent = `Start (local time): ${date.toLocaleString(undefined, { dateStyle:'medium', timeStyle:'short' })}`;

      startCountdown(date, ({days, hours, minutes, seconds, remainingMs}) => {
        const dstr = days > 0 ? days + 'd ' : '';
        countdownEl.textContent = `${dstr}${String(hours).padStart(2,'0')}:${String(minutes).padStart(2,'0')}:${String(seconds).padStart(2,'0')}`;
        if (remainingMs === 0) countdownEl.textContent = 'session has started!';
      });

    } catch (err) {
      raceInfoEl.textContent = 'error loading';
      countdownEl.textContent = '--:--:--:--';
      console.error(err);
    }
  })();
  </script>


</body>
</html>