Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PETA Vegan Approved List to OBF #131

Closed
Tracked by #53
philipbrembeck opened this issue Sep 13, 2022 · 4 comments
Closed
Tracked by #53

Add PETA Vegan Approved List to OBF #131

philipbrembeck opened this issue Sep 13, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request low priority Fix when there is time
Milestone

Comments

@philipbrembeck
Copy link
Collaborator

Proposed changes

Add the PETA Vegan Approved list to all products from OBF and match them.

@philipbrembeck philipbrembeck added enhancement New feature or request low priority Fix when there is time labels Sep 13, 2022
@philipbrembeck philipbrembeck self-assigned this Sep 13, 2022
@philipbrembeck
Copy link
Collaborator Author

PETA Vegan Approved does not support food/cosmetics, etc. but only clothing, so it's not relevant.
Anyways, this is a way to search the Approved Manufacturers (maybe for future projects):

<?php
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);

$VAR = $_GET['company'];
if (empty($VAR)){
  header("Content-Type: application/json;charset=utf-8");
  echo '{
    "STATUS": "BAD_REQUEST"
}';
  exit();
}
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://petaapprovedvegan.peta.org/search-peta-approved-vegan/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"action\":\"facetwp_refresh\",\"data\":{\"facets\":{\"search\":\"$VAR\",\"categories\":[],\"country\":[],\"pagination\":[]},\"frozen_facets\":{\"search\":\"soft\"},\"http_params\":{\"get\":{\"_search\":\"$VAR\"},\"uri\":\"search-peta-approved-vegan\",\"url_vars\":[]},\"template\":\"wp\",\"extras\":{\"sort\":\"default\"},\"soft_refresh\":0,\"is_bfcache\":1,\"first_load\":0,\"paged\":1}}",
  CURLOPT_HTTPHEADER => [
    "Accept: */*",
    "Content-Type: application/json"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $peta = json_decode($response)->template;
  $petali = strip_tags($peta, "<h2><div>");
  $first_step = explode( '<div class="company-filter__companies">' , $petali );
  $second_step = explode("</div>" , $first_step[1] );
  $h2 = $second_step[0];
    $dom = new \DOMDocument();
    $dom->loadHTML('<?xml encoding="utf-8" ?>' . $h2);
    $array = [];
    foreach ($dom->getElementsByTagName("h2") as $node) {
      if ($node->textContent !== "No Companies Match Your Search"){
        $array["STATUS"] = "COMPANIES_FOUND";
        $array["PETA_VEGAN_APPROVED"][] = $node->textContent;
      }
      else {
        $array["STATUS"] = "NO_COMPANIES_FOUND";
      }
    }
    header("Content-Type: application/json;charset=utf-8");
    echo json_encode($array, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)."\n";
}
?>

@philipbrembeck
Copy link
Collaborator Author

But I did not stop there, PETA has also a list of Cruelty-Free Cosmetics and personal care products:
https://crueltyfree.peta.org

I contacted them and while they support the idea, they don't give out a JSON list or anything like that (apparently it is, because it's updated too often? I didn't really get it.)
But I worked my way through their website and got an access point - more or less.
We could access this list like every 4-5 days via a cronjob and then save the results to a separate JSON file which we could also integrate into our API:

<?php
$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => "https://tierversuchsfrei.peta-approved.de/wp-admin/admin-ajax.php",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    /* Security is the AJAX-Security Code generated by PETA and might need to be changed, for faster testing replace "all" with "50"*/
    CURLOPT_POSTFIELDS => "security=74eceaf085&action=dynamic_filtering&sort=companies-dont-test&per_page=all",
    CURLOPT_HTTPHEADER => [
        "Accept: */*",
        "Content-Type: application/x-www-form-urlencoded",
    ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
    echo "cURL Error #:" . $err;
} else {
    $ul = json_decode($response)->html;
    $petali = strip_tags($ul, "<li>");
    $peta = preg_replace("/\n|\t/", "", $petali);

    $dom = new \DOMDocument();
    $dom->loadHTML('<?xml encoding="utf-8" ?>' . $peta);
    $array = [];
    foreach ($dom->getElementsByTagName("li") as $node) {
        $array[] = $node->textContent;
    }

    $output = [];
    array_walk($array, function ($entry) use (&$output) {
        /* PETA Business Friend & Features PETA Logo */
        if ("FL" == substr($entry, -2)) {
            if(!empty(substr($entry, 0, -2))){
              $output["PETA_DOES_NOT_TEST"][] = substr($entry, 0, -2);
            }
        } 
        /* Features PETA Logo */
        elseif ("L" == substr($entry, -1)) {
            if(!empty(substr($entry, 0, -1))){
              $output["PETA_DOES_NOT_TEST"][] = substr($entry, 0, -1);
            }
        }
        /* PETA Business Friend */
        elseif ("F" == substr($entry, -1)) {
            if(!empty(substr($entry, 0, -1))){
              $output["PETA_DOES_NOT_TEST"][] = substr($entry, 0, -1);
            }
        }
        /* Does not test on animals */
        else {
            $output["PETA_DOES_NOT_TEST"][] = $entry;
        }
    });

    header("Content-Type: application/json;charset=utf-8");
    echo json_encode($output, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)."\n";
}

?>

@philipbrembeck
Copy link
Collaborator Author

A first little test is online:
https://api.vegancheck.me/v0/peta

@philipbrembeck
Copy link
Collaborator Author

EAN Code to test with: 5016155120031

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request low priority Fix when there is time
Projects
None yet
Development

No branches or pull requests

1 participant