<commit_before>import { createIO } from 'impure'

import { loadCustomSong } from '../CustomSongs'
import DndResources from '../../resources/dnd-resources'
import {
  downloadFileEntryFromURL,
  CustomSongResources,
} from '../../resources/custom-song-resources'
import { getIPFSResources } from '../../resources/ipfs-resources'

export function handleCustomSongFolderDrop(event) {
  return createIO(async ({ store, customSongLoader }) => {
    const resources = new DndResources(event)
    const initialLog = ['Examining dropped items...']
    return loadCustomSong(resources, initialLog, { store, customSongLoader })
  })
}

export function handleCustomSongURLLoad(url) {
  return createIO(async ({ store, customSongLoader }) => {
    const resources = new CustomSongResources({
      getFiles: async (log) => [await downloadFileEntryFromURL(url, log)],
    })
    const initialLog = ['Loading from ' + url]
    return loadCustomSong(resources, initialLog, { store, customSongLoader })
  })
}

export function handleClipboardPaste(e) {
  return createIO(async ({ store, customSongLoader }) => {
    let match
    const text = e.clipboardData.getData('text/plain')
    match = text.match(/(https?:\/\/[a-zA-Z0-9:.-]+)?(\/ipfs\/\S+)/)
    if (match) {
      const gateway = match[1] || undefined
      const path = gateway ? decodeURI(match[2]) : match[2]
      const resources = getIPFSResources(path, gateway)
      const initialLog = [
        'Loading from IPFS path ' + path + '...',
        `(Using ${resources.gatewayName})`,
      ]
      if (/^http:/.test(gateway) && window.location.protocol === 'https:') {
        initialLog.push(
          store,
          'WARNING: Loading http URL from https. This will likely fail!'
        )
      }
      return loadCustomSong(resources, initialLog, { store, customSongLoader })
    }
  })
}<commit_msg>Allow pasting URL to bemuse-song.json<commit_after>import { createIO } from 'impure'

import { loadCustomSong } from '../CustomSongs'
import DndResources from '../../resources/dnd-resources'
import {
  downloadFileEntryFromURL,
  CustomSongResources,
} from '../../resources/custom-song-resources'
import { getIPFSResources } from '../../resources/ipfs-resources'
import { URLResources } from 'bemuse/resources/url'

export function handleCustomSongFolderDrop(event) {
  return createIO(async ({ store, customSongLoader }) => {
    const resources = new DndResources(event)
    const initialLog = ['Examining dropped items...']
    return loadCustomSong(resources, initialLog, { store, customSongLoader })
  })
}

export function handleCustomSongURLLoad(url) {
  return createIO(async ({ store, customSongLoader }) => {
    const resources = new CustomSongResources({
      getFiles: async (log) => [await downloadFileEntryFromURL(url, log)],
    })
    const initialLog = ['Loading from ' + url]
    return loadCustomSong(resources, initialLog, { store, customSongLoader })
  })
}

export function handleClipboardPaste(e) {
  return createIO(async ({ store, customSongLoader }) => {
    const text = e.clipboardData.getData('text/plain')
    {
      const match = text.match(/(https?:\/\/[a-zA-Z0-9:.-]+)?(\/ipfs\/\S+)/)
      if (match) {
        const gateway = match[1] || undefined
        const path = gateway ? decodeURI(match[2]) : match[2]
        const resources = getIPFSResources(path, gateway)
        const initialLog = [
          'Loading from IPFS path ' + path + '...',
          `(Using ${resources.gatewayName})`,
        ]
        if (/^http:/.test(gateway) && window.location.protocol === 'https:') {
          initialLog.push(
            store,
            'WARNING: Loading http URL from https. This will likely fail!'
          )
        }
        return loadCustomSong(resources, initialLog, {
          store,
          customSongLoader,
        })
      }
      {
        const match = text.match(
          /https?:\/\/[a-zA-Z0-9:.-]+\/\S+\/bemuse-song\.json/
        )
        if (match) {
          const url = match[0]
          const initialLog = ['Loading prepared song...']
          const resources = new PreparedSongResources(new URL(url))
          return loadCustomSong(resources, initialLog, {
            store,
            customSongLoader,
          })
        }
      }
    }
  })
}

class PreparedSongResources extends URLResources {
  fileList = Promise.resolve(['bemuse-song.json'])
}
{'fraction_non_alphanumeric': 0.13930688554491563, 'fraction_numerical': 0.0029639762881896944, 'mean_word_length': 2.851624231782265, 'pattern_counts': {'":': 0, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5917252', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1262, 'n_words': 293}