<commit_before>/* Interface */

#include "enna.h"
#include "xml_utils.h"
#include "url_utils.h"

#define DEBUG 1

#define MAX_URL_SIZE        1024
#define MAX_KEYWORD_SIZE    1024
#define MAX_BUF_LEN         65535

#define AMAZON_HOSTNAME     "webservices.amazon.com"
#define AMAZON_LICENSE_KEY  "<KEY>"

#define AMAZON_SEARCH "http://%s/onca/xml?Service=AWSECommerceService&SubscriptionId=%s&Operation=ItemSearch&Keywords=%s&SearchIndex=%s"

#define AMAZON_SEARCH_MUSIC "Music"
#define AMAZON_SEARCH_MOVIE "DVD"

#define AMAZON_SEARCH_COVER "http://%s/onca/xml?Service=AWSECommerceService&SubscriptionId=%s&Operation=ItemLookup&ItemId=%s&ResponseGroup=Images"

/*****************************************************************************/
/*                         Private Module API                                */
/*****************************************************************************/

static char * amazon_music_cover_get (const char *artist, const char *album);
static char * amazon_movie_cover_get (const char *movie);

static Enna_Class_CoverPlugin class =
  {
    "amazon",
    amazon_music_cover_get,
    amazon_movie_cover_get,
  };

typedef struct _Enna_Module_Amazon {
  Evas *evas;
  Enna_Module *em;
  CURL *curl;
} Enna_Module_Amazon;

static Enna_Module_Amazon *mod;

EAPI Enna_Module_Api module_api =
  {
    ENNA_MODULE_VERSION,
    "amazon"
  };

static int
em_init (Enna_Module *em)
{
  mod = calloc (1, sizeof (Enna_Module_Amazon));

  mod->em = em;
  mod->evas = em->evas;
  
  curl_global_init (CURL_GLOBAL_DEFAULT);
  mod->curl = curl_easy_init ();
  enna_cover_plugin_register (&class);
  
  return 1;
}

static int
em_shutdown (Enna_Module *em)
{
  if (mod->curl)
    curl_easy_cleanup (mod->curl);
  curl_global_cleanup ();
  free (mod);
  
  return 1;
}

/*****************************************************************************/
/*                             Amazon Helpers                                */
/*****************************************************************************/

static char *
amazon_cover_get (char *search_type, char *keywords)
{
  char *cover;
  char url[MAX_URL_SIZE];
  url_data_t data;
  
  xmlDocPtr doc;
  xmlNode *img;
  xmlChar *asin, *cover_url;

  if (!search_type || !keywords)
    return NULL;
  
  /* 2. Prepare Amazon WebService URL for Search */
  memset (url, '\0', MAX_URL_SIZE);
  snprintf (url, MAX_URL_SIZE, AMAZON_SEARCH,
            AMAZON_HOSTNAME, AMAZON_LICENSE_KEY, keywords, search_type);

#ifdef DEBUG
  printf ("Amazon Search Request: %s\n", url);
#endif

  /* 3. Perform request */
  data = url_get_data (mod->curl, url);
  if (data.status != CURLE_OK)
    return NULL;

#ifdef DEBUG
  printf ("Amazon Search Reply: %s\n", data.buffer);
#endif

  /* 4. Parse the answer to get ASIN value */
  doc = xmlReadMemory (data.buffer, data.size, "noname.xml", NULL, 0);
  free (data.buffer);
  
  if (!doc)
    return NULL;

  asin = get_prop_value_from_xml_tree (xmlDocGetRootElement (doc), "ASIN");
  xmlFreeDoc (doc);

  if (!asin)
  {
    printf ("Amazon: Unable to find the item \"%s\"\n", keywords);
    return NULL;
  }

#ifdef DEBUG
  printf ("Found Amazon ASIN: %s\n", asin);
#endif

  /* 5. Prepare Amazon WebService URL for Cover Search */
  memset (url, '\0', MAX_URL_SIZE);
  snprintf (url, MAX_URL_SIZE, AMAZON_SEARCH_COVER,
            AMAZON_HOSTNAME, AMAZON_LICENSE_KEY, asin);
  xmlFree (asin);

#ifdef DEBUG
  printf ("Cover Search Request: %s\n", url);
#endif

  /* 6. Perform request */
  data = url_get_data (mod->curl, url);
  if (data.status != CURLE_OK)
    return NULL;

#ifdef DEBUG
  printf ("Cover Search Reply: %s\n", data.buffer);
#endif
  
  /* 7. Parse the answer to get cover URL */
  doc = xmlReadMemory (data.buffer, data.size, "noname.xml", NULL, 0);
  free (data.buffer);
  
  if (!doc)
    return NULL;

  img = get_node_xml_tree (xmlDocGetRootElement (doc), "LargeImage");
  if (!img)
    img = get_node_xml_tree (xmlDocGetRootElement (doc), "MediumImage");
  if (!img)
    img = get_node_xml_tree (xmlDocGetRootElement (doc), "SmallImage");

  if (!img)
  {
    xmlFreeDoc (doc);
    return NULL;<commit_msg>[svn] remove useless file name<commit_after>/* Interface */

#include "enna.h"
#include "xml_utils.h"
#include "url_utils.h"

#define DEBUG 1

#define MAX_URL_SIZE        1024
#define MAX_KEYWORD_SIZE    1024
#define MAX_BUF_LEN         65535

#define AMAZON_HOSTNAME     "webservices.amazon.com"
#define AMAZON_LICENSE_KEY  "<KEY>"

#define AMAZON_SEARCH "http://%s/onca/xml?Service=AWSECommerceService&SubscriptionId=%s&Operation=ItemSearch&Keywords=%s&SearchIndex=%s"

#define AMAZON_SEARCH_MUSIC "Music"
#define AMAZON_SEARCH_MOVIE "DVD"

#define AMAZON_SEARCH_COVER "http://%s/onca/xml?Service=AWSECommerceService&SubscriptionId=%s&Operation=ItemLookup&ItemId=%s&ResponseGroup=Images"

/*****************************************************************************/
/*                         Private Module API                                */
/*****************************************************************************/

static char * amazon_music_cover_get (const char *artist, const char *album);
static char * amazon_movie_cover_get (const char *movie);

static Enna_Class_CoverPlugin class =
  {
    "amazon",
    amazon_music_cover_get,
    amazon_movie_cover_get,
  };

typedef struct _Enna_Module_Amazon {
  Evas *evas;
  Enna_Module *em;
  CURL *curl;
} Enna_Module_Amazon;

static Enna_Module_Amazon *mod;

EAPI Enna_Module_Api module_api =
  {
    ENNA_MODULE_VERSION,
    "amazon"
  };

static int
em_init (Enna_Module *em)
{
  mod = calloc (1, sizeof (Enna_Module_Amazon));

  mod->em = em;
  mod->evas = em->evas;
  
  curl_global_init (CURL_GLOBAL_DEFAULT);
  mod->curl = curl_easy_init ();
  enna_cover_plugin_register (&class);
  
  return 1;
}

static int
em_shutdown (Enna_Module *em)
{
  if (mod->curl)
    curl_easy_cleanup (mod->curl);
  curl_global_cleanup ();
  free (mod);
  
  return 1;
}

/*****************************************************************************/
/*                             Amazon Helpers                                */
/*****************************************************************************/

static char *
amazon_cover_get (char *search_type, char *keywords)
{
  char *cover;
  char url[MAX_URL_SIZE];
  url_data_t data;
  
  xmlDocPtr doc;
  xmlNode *img;
  xmlChar *asin, *cover_url;

  if (!search_type || !keywords)
    return NULL;
  
  /* 2. Prepare Amazon WebService URL for Search */
  memset (url, '\0', MAX_URL_SIZE);
  snprintf (url, MAX_URL_SIZE, AMAZON_SEARCH,
            AMAZON_HOSTNAME, AMAZON_LICENSE_KEY, keywords, search_type);

#ifdef DEBUG
  printf ("Amazon Search Request: %s\n", url);
#endif

  /* 3. Perform request */
  data = url_get_data (mod->curl, url);
  if (data.status != CURLE_OK)
    return NULL;

#ifdef DEBUG
  printf ("Amazon Search Reply: %s\n", data.buffer);
#endif

  /* 4. Parse the answer to get ASIN value */
  doc = xmlReadMemory (data.buffer, data.size, NULL, NULL, 0);
  free (data.buffer);
  
  if (!doc)
    return NULL;

  asin = get_prop_value_from_xml_tree (xmlDocGetRootElement (doc), "ASIN");
  xmlFreeDoc (doc);

  if (!asin)
  {
    printf ("Amazon: Unable to find the item \"%s\"\n", keywords);
    return NULL;
  }

#ifdef DEBUG
  printf ("Found Amazon ASIN: %s\n", asin);
#endif

  /* 5. Prepare Amazon WebService URL for Cover Search */
  memset (url, '\0', MAX_URL_SIZE);
  snprintf (url, MAX_URL_SIZE, AMAZON_SEARCH_COVER,
            AMAZON_HOSTNAME, AMAZON_LICENSE_KEY, asin);
  xmlFree (asin);

#ifdef DEBUG
  printf ("Cover Search Request: %s\n", url);
#endif

  /* 6. Perform request */
  data = url_get_data (mod->curl, url);
  if (data.status != CURLE_OK)
    return NULL;

#ifdef DEBUG
  printf ("Cover Search Reply: %s\n", data.buffer);
#endif
  
  /* 7. Parse the answer to get cover URL */
  doc = xmlReadMemory (data.buffer, data.size, NULL, NULL, 0);
  free (data.buffer);
  
  if (!doc)
    return NULL;

  img = get_node_xml_tree (xmlDocGetRootElement (doc), "LargeImage");
  if (!img)
    img = get_node_xml_tree (xmlDocGetRootElement (doc), "MediumImage");
  if (!img)
    img = get_node_xml_tree (xmlDocGetRootElement (doc), "SmallImage");

  if (!img)
  {
    xmlFreeDoc (doc);
    return NULL;
{'fraction_non_alphanumeric': 0.19043027600337473, 'fraction_numerical': 0.006508376521634326, 'mean_word_length': 3.7228229937393285, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 40}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4997987', 'n_tokens_mistral': 2982, 'n_tokens_neox': 2807, 'n_words': 681}