<commit_before>'use strict';

var dls = {};
var write_log = false;

browser.runtime.onMessage.addListener(handleMessages);
browser.downloads.onChanged.addListener(handleChanged);

loadOptions();

function parseDate(input) {
  // 2018-06-12T17:59:54.000Z
  return new Date(Date.UTC(
    parseInt(input.slice(0, 4), 10),
    parseInt(input.slice(5, 7), 10) - 1,
    parseInt(input.slice(8, 10), 10),
    parseInt(input.slice(11, 13), 10),
    parseInt(input.slice(14, 16), 10),
    parseInt(input.slice(17, 19), 10)
  ));
}

function notify(dl_id) {
  var mes = dls[dl_id];
  browser.notifications.create({
    "type": "basic",
    "iconUrl": getPicUrl(mes),
    "title": "Your IG download has completed!",
    "message": `Successfully downloaded ${mes.user}'s picture posted on ${
      parseDate(mes.timestamp).toLocaleString('en-US', { weekday: 'short',
                                                         year: 'numeric',
                                                         month: 'long',
                                                         day: 'numeric' })}.`
  }).catch(onError);
}

function onStartedDownload(message, id) {
  if (write_log) console.log(`Started downloading for ${message.user}: ${id} with ${message.artefact_icon}`);

  // store message based on id
  dls[id] = message;

  // notify content script
  browser.tabs.sendMessage(message.sender.tab.id, {
    msg: "download_started",
    digest: message.digest,
    artefact_type: message.artefact_type,
    artefact_icon: message.artefact_icon
  }).catch(onError);
}

function onFailed(error) {
  console.log(`Download failed: ${error}`);
}

function handleChanged(delta) {
  try {
    if (write_log) console.log(`background.js: dl_changes ${delta.state.current}, ${delta.bytesReceived}`);
  } catch(ex) {}

  if (delta.state && delta.state.current === "complete") {
    if (write_log) console.log(`background.js: Download ${delta.id} has completed. ${dls[delta.id].artefact_icon}`);

    // notify content script
    browser.tabs.sendMessage(dls[delta.id].sender.tab.id, {
      msg: "download_completed",
      digest: dls[delta.id].digest,
      artefact_type: dls[delta.id].artefact_type,
      artefact_icon: dls[delta.id].artefact_icon
    }).catch(onError);

    // show browser notification
    try {
      if (dls[delta.id].artefact_icon == 'image' || dls[delta.id].artefact_icon == 'file-video') { notify(delta.id) }
    } catch(ex) {}

    browser.downloads.erase({
      id: delta.id
    }).then(ids => {
      if (write_log) console.log(`background.js: Erased downloads: ${ids}`);
    }, error => {
      console.log(`background.js: Error erasing download: ${error}`);
    });

    if (write_log) console.log(`background.js: content of DLs: ${Object.keys(dls).length}`);
    delete dls[delta.id];

    // delete download from browser
    //browser.browsingData.removeDownloads({ originTypes: "extension" }).catch(onError);
  }
}

function getPicUrl(message) {
  if (message.url.constructor === Array) {
    return message.url[1];
  } else {
    return message.url;
  }
}

function getVidUrl(message) {
  if (message.url.constructor === Array) {
    return message.url[0];
  } else {
    return undefined;
  }
}
<commit_msg>Remove stuck downloads that were started more than 5 minutes ago<commit_after>'use strict';

var dls = {};
var write_log = false;

browser.runtime.onMessage.addListener(handleMessages);
browser.downloads.onChanged.addListener(handleChanged);

loadOptions();

function parseDate(input) {
  // 2018-06-12T17:59:54.000Z
  return new Date(Date.UTC(
    parseInt(input.slice(0, 4), 10),
    parseInt(input.slice(5, 7), 10) - 1,
    parseInt(input.slice(8, 10), 10),
    parseInt(input.slice(11, 13), 10),
    parseInt(input.slice(14, 16), 10),
    parseInt(input.slice(17, 19), 10)
  ));
}

function notify(dl_id) {
  var mes = dls[dl_id];
  browser.notifications.create({
    "type": "basic",
    "iconUrl": getPicUrl(mes),
    "title": "Your IG download has completed!",
    "message": `Successfully downloaded ${mes.user}'s picture posted on ${
      parseDate(mes.timestamp).toLocaleString('en-US', { weekday: 'short',
                                                         year: 'numeric',
                                                         month: 'long',
                                                         day: 'numeric' })}.`
  }).catch(onError);
}

function onStartedDownload(message, id) {
  if (write_log) console.log(`Started downloading for ${message.user}: ${id} with ${message.artefact_icon}`);

  // store message based on id
  dls[id] = message;

  // notify content script
  browser.tabs.sendMessage(message.sender.tab.id, {
    msg: "download_started",
    digest: message.digest,
    artefact_type: message.artefact_type,
    artefact_icon: message.artefact_icon
  }).catch(onError);
}

function onFailed(error) {
  console.log(`Download failed: ${error}`);
}

function handleChanged(delta) {
  try {
    if (write_log) console.log(`background.js: dl_changes ${delta.state.current}, ${delta.bytesReceived}`);
  } catch(ex) {}

  if (delta.state && delta.state.current === "complete") {
    if (write_log) console.log(`background.js: Download ${delta.id} has completed. ${dls[delta.id].artefact_icon}`);

    // notify content script
    browser.tabs.sendMessage(dls[delta.id].sender.tab.id, {
      msg: "download_completed",
      digest: dls[delta.id].digest,
      artefact_type: dls[delta.id].artefact_type,
      artefact_icon: dls[delta.id].artefact_icon
    }).catch(onError);

    // show browser notification
    try {
      if (dls[delta.id].artefact_icon == 'image' || dls[delta.id].artefact_icon == 'file-video') { notify(delta.id) }
    } catch(ex) {}

    browser.downloads.erase({
      id: delta.id
    }).then(ids => {
      if (write_log) console.log(`background.js: Erased downloads: ${ids}`);
    }, error => {
      console.log(`background.js: Error erasing download: ${error}`);
    });

    if (write_log) console.log(`background.js: Content of DLs: ${Object.keys(dls).length}`);

    // remove message reference from download store
    dls[delta.id] = undefined;

    // remove stuck downloads
    if (write_log) console.log(`background.js: Checking for stuck downloads...`);
    browser.downloads.search({
      urlRegex: 'cdninstagram\.com',                            // downloads from instagram
      startedBefore: new Date(new Date().getTime() - 1000*60*5) // started more than 5 minutes ago
    })
    .then(downloads => {
      for (let download of downloads) {
        console.log(download.id);
        console.log(download.state);
        console.log(download.url);
        browser.downloads.cancel(download.id).then(
          () => {
            if (write_log) console.log(`background.js: Cancelled stuck download`);
          },
          error => {
            if (write_log) console.log(`background.js: Error cancelling download: ${error}`);
          }
        );
      }
    }, error => {
      console.log(`background.js: Error removing stuck download: ${error}`);
    });

    // delete download from browser history
    //browser.browsingData.removeDownloads({ originTypes: "extension" }).catch(onError);
  }
}

function getPicUrl(message) {
  if (message.url.constructor === Array) {
    return message.url[1];
  } else {
    return message.url;
  }
}

function getVidUrl(message) {
  if (message.url.constructor === Array) {
    return message.url[0];
  } else {
    return undefined;
  }
}

{'fraction_non_alphanumeric': 0.15404141616566466, 'fraction_numerical': 0.014829659318637275, 'mean_word_length': 2.8787564766839377, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 57, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2792577', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2364, 'n_words': 483}