<commit_before># Load core dependencies.
https = require 'https'
fs = require 'fs'


# Load third party dependencies.
request = require 'request'
jsonfile = require 'jsonfile'


# Load config files.
user = jsonfile.readFileSync './.pickselacc'
config = jsonfile.readFileSync './picksel.json'


# Initialize logging
Log = require 'log-color-optionaldate'
logSettings = 
  level: 'debug'
  color: true
  date: false
log = new Log logSettings


# Map of resolution property names.
resolutions = [
  'previewURL'
  'webformatURL'
  'largeImageURL'
  'fullHDURL'
  'imageURL'
  'vectorURL'
]


humanResolutions = [
  'tiny'
  'small'
  'large'
  'hd'
  'full'
  'vector'
]


isValidId = (id) -> !isNaN(id) && parseInt(id) >= 0


isValidResolution = (res) -> humanResolutions.indexOf(res) > -1


getResolutionCode = (res) -> humanResolutions.indexOf res


# Persists the currently loaded configuration settings
#
persistConfig = () ->
  options = 
    spaces: 4
  jsonfile.writeFileSync './picksel.json', config, options

    
# Downloads an image.
#
# @param [Number] id the Pixabay image ID
# @param [Number] resolution the resolution to download the image in
# @param [String] destination the path of the destination file
#
download = (id, resolution, destination) ->
  # Build URL for API request.
  url = 'https://pixabay.com/api/?key=' \
    + user.apiKey \
    + (if resolution > 1 then '&response_group=high_resolution' else '') \
    + '&id=' \
    + id
  
  log.info 'Requesting information for image with ID ' \
    + id \
    + ' from ' \
    + url
    
  # Request JSON from Pixabay.
  options =
    url: url
    json: true
  request options, (error, response, body) ->
    if !body.hits
      log.error 'Couldn\'t get information about image ' \
        + id
    else
      file = fs.createWriteStream destination
      req = https.get body.hits[0][resolutions[resolution]], (res) ->
        res.pipe file

      
# Installs a single image.
#
# @param [Object] image the image to grab
grab = (image) ->
  path = './' + config.directory + '/' + image.destination
  log.info 'Installing image with ID ' \ 
    + image.id \
    + ' at resolution \'' \
    + humanResolutions[image.resolution] \
    + '\' to \'' \
    + path \
    + '\'.'
  download image.id, image.resolution, path

  
# Installs all images present in the currently loaded configuration.
#
install = () -> grab image for image in config.images


# Adds an image as an asset.
#
# @param [Object] args the arguments to the program
# 
add = (args) ->
  # Check ID.
  id = args[3]
  if !isValidId
    log.error 'That ID \'' + id + '\' isn\'t valid.'
    return false
   
  # Check resolution.
  res = args[4]
  if !isValidResolution(res)
    log.error 'That resolution \'' + res + '\' isn\'t valid.'
    return false
  
  # Add new image to configuration file.
  image =
    id: id
    resolution: getResolutionCode(res)
    destination: args[5]
  config.images.push image
  
  persistConfig() # Update configuration file.
  
  log.info 'Added image with ID ' \
    + image.id \
    + ' as asset.'
  
  install() # Freshly install all images.
     
     
# Interpret commands.
switch process.argv[2]
  when 'install' then install()
  when 'add' then add process.argv
<commit_msg>Lint source<commit_after># Load core dependencies.
https = require 'https'
fs = require 'fs'


# Load third party dependencies.
request = require 'request'
jsonfile = require 'jsonfile'


# Load config files.
user = jsonfile.readFileSync './.pickselacc'
config = jsonfile.readFileSync './picksel.json'


# Initialize logging
Log = require 'log-color-optionaldate'
logSettings =
  level: 'debug'
  color: true
  date: false
log = new Log logSettings


# Map of resolution property names.
resolutions = [
  'previewURL'
  'webformatURL'
  'largeImageURL'
  'fullHDURL'
  'imageURL'
  'vectorURL'
]


humanResolutions = [
  'tiny'
  'small'
  'large'
  'hd'
  'full'
  'vector'
]


isValidId = (id) -> !isNaN(id) && parseInt(id) >= 0


isValidResolution = (res) -> humanResolutions.indexOf(res) > -1


getResolutionCode = (res) -> humanResolutions.indexOf res


# Persists the currently loaded configuration settings
#
persistConfig = () ->
  options =
    spaces: 4
  jsonfile.writeFileSync './picksel.json', config, options

    
# Downloads an image.
#
# @param [Number] id the Pixabay image ID
# @param [Number] resolution the resolution to download the image in
# @param [String] destination the path of the destination file
#
download = (id, resolution, destination) ->
  # Build URL for API request.
  url = 'https://pixabay.com/api/?key=' \
    + user.apiKey \
    + (if resolution > 1 then '&response_group=high_resolution' else '') \
    + '&id=' \
    + id
  
  log.info 'Requesting information for image with ID ' \
    + id \
    + ' from ' \
    + url
    
  # Request JSON from Pixabay.
  options =
    url: url
    json: true
  request options, (error, response, body) ->
    if !body.hits
      log.error 'Couldn\'t get information about image ' \
        + id
    else
      file = fs.createWriteStream destination
      req = https.get body.hits[0][resolutions[resolution]], (res) ->
        res.pipe file

      
# Installs a single image.
#
# @param [Object] image the image to grab
grab = (image) ->
  path = './' + config.directory + '/' + image.destination
  log.info 'Installing image with ID ' \
    + image.id \
    + ' at resolution \'' \
    + humanResolutions[image.resolution] \
    + '\' to \'' \
    + path \
    + '\'.'
  download image.id, image.resolution, path

  
# Installs all images present in the currently loaded configuration.
#
install = () -> grab image for image in config.images


# Adds an image as an asset.
#
# @param [Object] args the arguments to the program
#
add = (args) ->
  # Check ID.
  id = args[3]
  if !isValidId
    log.error 'That ID \'' + id + '\' isn\'t valid.'
    return false
   
  # Check resolution.
  res = args[4]
  if !isValidResolution(res)
    log.error 'That resolution \'' + res + '\' isn\'t valid.'
    return false
  
  # Add new image to configuration file.
  image =
    id: id
    resolution: getResolutionCode(res)
    destination: args[5]
  config.images.push image
  
  persistConfig() # Update configuration file.
  
  log.info 'Added image with ID ' \
    + image.id \
    + ' as asset.'
  
  install() # Freshly install all images.
     
     
# Removes an image from installed assets.
#
# @param [Object] args the arguments to the program
#
remove = (args) ->
  id = args[3]
  images = config.images
  filteredImages = images.filter (obj) -> obj.id != args[3]
  if images.length == filteredImages.length
    log.warning 'Couldn\'t uninstall image with ID ' \
      + id \
      + ' becuse it\'s not installed in the first place.'
  else
    config.images = filteredImages
    persistConfig()
     
     
# Interpret commands.
switch process.argv[2]
  when 'install' then install()
  when 'add' then add process.argv
  when 'remove' then remove process.argv

{'fraction_non_alphanumeric': 0.1232837528604119, 'fraction_numerical': 0.002860411899313501, 'mean_word_length': 2.773880194279547, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1249657', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2233, 'n_words': 691}