Brydom
10/10/2019 - 10:45 PM

pmex

case "barnimages":
    return await check_previous_search(query, provider, page).then(
      async data => {
        if (data) {
          return {
            results: data.length,
            photos: data
          };
        }

        return await fetch(
          `https://barnimages.com/xxxxx/pixelmob`
        )
          .then(data => data.text())
          .then(parsed_xml =>
            JSON.parse(convert.xml2json(parsed_xml, { alwaysArray: true }))
          )
          .then(async json => {
            let photos = [];

            json.elements[0].elements[0].elements.map(item => {
              if (item.elements && item.elements.length === 8) {
                const elements = item.elements;
                const get_value = item => item.elements[0].text.trim();

                photos.push(
                  photo_object(
                    get_value(elements[0]),
                    get_value(elements[2]),
                    get_value(elements[1]),
                    get_value(elements[4]),
                    get_value(elements[3]),
                    get_value(elements[1])
                  )
                );
              }
            });

            return await set_previous_search(
              query,
              provider,
              page,
              photos
            ).then(() => {
              return {
                results,
                photos
              };
            });
          })
          .catch(error => {
            console.log({
              query: request.query,
              error
            });

            return {
              results: 0,
              photos: []
            };
          });
      }
    );