arte stream url/seperate mp4

get arte stream url with just one click (or none at all) or download seperate audio/video mp4

// ==UserScript==
// @name         arte stream url/seperate mp4
// @namespace    http://tampermonkey.net/
// @version      1.5.5
// @description  get arte stream url with just one click (or none at all) or download seperate audio/video mp4
// @author       mihau
// @match        https://www.arte.tv/de/videos/*
// @match        https://www.arte.tv/fr/videos/*
// @license MIT
// @supportURL   https://greasyfork.org/en/scripts/533451-arte-stream-url-seperate-mp4
// ==/UserScript==

// if you want to get the url in the very moment the page loads, change this from 0 to 1:
var loadonload = 0;
// please do not edit below this line





$   = function(_) {return document.getElementById(_)}
$tn = function(_) {return document.getElementsByTagName(_)}
$cn = function(_) {return document.getElementsByClassName(_)}
$qa = function(_) {return document.querySelectorAll(_)}
$qs = function(_) {return document.querySelector(_)}

var url, thisisthefilmid = "";
var loc = window.location.pathname.split('/');
var lang = loc[1];
var id = loc[3];
var name = loc[4];

var thestring = self.__next_f[self.__next_f.length - 1].toString().slice(0, -1).slice(22, -1);
if (/Generate\//.test(thestring)) {
  var schemaObj = JSON.parse(thestring);
  url = schemaObj.apiPlayerConfig.attributes.streams[0].url;
  thisisthefilmid = "worky!";
} else {

  var jsoncontainer = 0;

  for (var i = 0, l = $tn("script").length; i < l; ++i) {

    if ($tn("script")[i].innerText.indexOf("Generate/") != -1) {
      jsoncontainer = i;
      break;
    }

  }

  var nix = $tn("script")[jsoncontainer].innerText;
  var myregex = /Generate\/.*?\/\//;
  var match = nix.match(myregex);
  var mynewregex = new RegExp("\/.*", "gi")
  // very, very dirty regex, but hey - it works, and there's no XHR necessary:
  thisisthefilmid = match[0].replace("Generate/", "").replace(mynewregex, "");
  url = "https://manifest-arte.akamaized.net/api/manifest/v1/Generate/" + thisisthefilmid + "/" + lang + "/XQ+KS+CHEV1/" + id + ".m3u8";
}

// just in case, for XHR later on
var api_base = " https://api.arte.tv/api/player/v2/config/" + lang + "/";
var download_url = api_base + id;

if ((loadonload != 0) && (thisisthefilmid != null) && (thisisthefilmid != "null") && (thisisthefilmid != "") && (thisisthefilmid != "undefined") && (thisisthefilmid != undefined) && (thisisthefilmid != NaN)) {
  showURL();
}

var filmtitle = $qs('meta[property="og:title"]').content;
[" | ARTE Concert", " | ARTE", " - Komplette Sendung", " - Programm in voller Länge", " - Film in voller Länge", " - Regarder le film complet", " - Regarder l’émission complète"].forEach((item) => {
  filmtitle = filmtitle.replace(item, "")
});
filmtitle = filmtitle.replace(/ /g, "_").replace(/[^a-z0-9 \.,_-]/gim, "").replace("_-_", "-");

function showURL() {
  var test = prompt("stream URL (OK for ffmpeg command or cancel)", url);
  if (test !== null) {
    prompt("ffmpeg command (OK for ffmpeg AUDIO-ONLY command or cancel)", 'ffmpeg -referer "' + location.href + '" -user_agent "' + window.navigator.userAgent + '" -i "' + url + '" -c copy -bsf:a aac_adtstoasc "' + filmtitle + '.mp4"');
    if (test !== null) {
      prompt("ffmpeg AUDIO-ONLY command (OK for yt-dlp command or cancel)", 'ffmpeg -referer "' + location.href + '" -user_agent "' + window.navigator.userAgent + '" -i "' + url + '" -vn -c:a copy "' + filmtitle + '-audio.m4a"');
          if (test !== null) {
      prompt("yt-dlp command", "yt-dlp " + url);
    }
    }
  }
}

$tn("body")[0].onclick = function() {
  setTimeout('document.getElementsByTagName("body")[0].click()', 1000);

  var para = document.createElement("a");
  para.setAttribute('id', 'dwnl');
  para.setAttribute('style', 'color: red; font-weight: 800; padding-left: 5px');
  para.setAttribute('class', 'ds-64fl0u');

  var anothernode = document.createElement("span");
  anothernode.setAttribute('id', 'placeholder');
  anothernode.setAttribute('class', 'ds-11ckmbs');
  anothernode.setAttribute('style', 'padding-left: 10px');
  anothernode.innerText = "";

  var node = document.createElement("span");
  node.setAttribute('id', 'streamurl');
  node.setAttribute('class', 'ds-11ckmbs');
  node.setAttribute('style', 'padding-left: 10px; color: #FA481C');
  node.innerText = "Stream URL";
  para.appendChild(node);
  para.appendChild(anothernode);

  if (!($("dwnl"))) {
    if ($cn('ds-1r0jukn')[0]) {

      $cn('ds-1r0jukn')[0].insertBefore(para, null);

    } else if ($cn('ds-1rm5mah')[0]) {
      $cn('ds-1rm5mah')[0].insertBefore(para, null);
    }
  }
  var getJSON = function(url, callback) {

    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';

    xhr.onload = function() {

      var status = xhr.status;

      if (status == 200) {
        callback(null, xhr.response);
      } else {
        callback(status);
      }
    };

    xhr.send();
  };

  var getM3U = function(url, callback) {

    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'text';

    xhr.onload = function() {

      var status = xhr.status;

      if (status == 200) {
        callback(null, xhr.response);
      } else {
        callback(status);
      }
    };

    xhr.send();
  };

  if ((thisisthefilmid != null) && (thisisthefilmid != "null") && (thisisthefilmid != "") && (thisisthefilmid != "undefined") && (thisisthefilmid != undefined) && (thisisthefilmid != NaN)) {
    $("streamurl").onclick = function() {
      showURL();
    }
  } else {
    getJSON(download_url, function(err, data) {
      if (err != null) {
        console.error(err);
      } else {
        url = data.data.attributes.streams[0].url;
      }
    });

  }

  getM3U(url, function(err, data) {
    if (err != null) {
      console.error(err);
    } else {

      var mp4avail = 1;
      var videosarr = new Array();
      var audiosarr = new Array();
      var vid = 0;
      var aud = 0;
      var result, videolinks, audiolinks = "";
      var vidurlclean = new RegExp(".*_v", "gi");
      var audurlclean = new RegExp(".*_aud_", "gi");

      var lines = data.split(/[\r\n]/);
      for (var i in lines) {
        var line = lines[i];
        if (/A_v/.test(line) && (!(/h265/.test(line))) && (!(/iframe/.test(line)))) {

          if (/aka_me_session/.test(line)) {
            mp4avail = 0;
          }

          videosarr[vid] = line;
          vid++;

        }

        if (/A_aud/.test(line) && (!(/h265/.test(line))) && (!(/iframe/.test(line)))) {

          audiosarr[aud] = line.match(/URI=(["'])(.*?)\1/)[2];
          aud++;

        }
      }

      videosarr = videosarr.sort();
      videosarr.push(videosarr.shift());
      videosarr = videosarr.reverse();

      for (var i = 0, l = videosarr.length; i < l; ++i) {

        videolinks += '<option value="' + videosarr[i] + '">' + videosarr[i].replace(".m3u8", "").replace(vidurlclean, "") + 'p</option>';

      }
      for (var i = 0, l = audiosarr.length; i < l; ++i) {

        var auddisplay = audiosarr[i].replace(".m3u8", "").replace(audurlclean, "");

        if (/-ALL/.test(audiosarr[i])) {
          auddisplay += " (Deutsch)";
        } else if (/-FRA/.test(audiosarr[i])) {
          auddisplay += " (Francais)";
        } else if (/-ITA/.test(audiosarr[i])) {
          auddisplay += " (Italiano)";
        } else if (/-ANG/.test(audiosarr[i])) {
          auddisplay += " (English)";
        } else if (/-ESP/.test(audiosarr[i])) {
          auddisplay += " (Espanol)";
        } else if (/-POL/.test(audiosarr[i])) {
          auddisplay += " (Polski)";
        } else if (/-MUS/.test(audiosarr[i])) {
          auddisplay += " (music only)";
        }

        audiolinks += '<option value="' + audiosarr[i] + '">' + auddisplay + '</option>';
      }

      if (mp4avail == 1) {
        result = '<form name="jump" action=""><select style="background-color: black;color:white" class="ds-11ckmbs" name="dlmenu" onchange="location=document.jump.dlmenu.options[document.jump.dlmenu.selectedIndex].value;">';
        result += '<option value="#" selected="selected">mp4 download</option>';
        result += '<option value="#">(seperate files)</option>';
        result += '<option value="#"> </option>';
        result += '<option value="#">video resolution:</option>';
        result += videolinks;
        result += '<option value="#"> </option>';
        result += '<option value="#">audio languages:</option>';
        result += audiolinks;
        result += '</select></form>';
        result = result.replaceAll(".m3u8", ".mp4").replaceAll("undefined", "");
      } else {
        result = "mp4 not available";
      }

      $("placeholder").innerHTML = result;
      $("streamurl").onclick = function() {
        showURL();
      }

      $tn("body")[0].onclick = function() {}

    }
  });

};

setTimeout('document.getElementsByTagName("body")[0].click()', 3500);