<commit_before># Python wrapper for P2P Content Services

## Connection configuration

Configuration settings. Set these values in your environment or your Django settings.

    P2P_API_KEY = your_p2p_api_key
    P2P_API_URL = url_of_p2p_endpoint
    P2P_API_DEBUG = plz  # display an http log
    P2P_PRESERVE_EMBEDDED_TAGS = False # set to false to fix encoding issues with special characters

    # Optional
    P2P_IMAGE_SERVICES_URL = url_of_image_services_endpoint

To get a connection object based on these settings:

    from p2p import get_connection
    p2p = get_connection()

Or you can create a connection object manually. You'll want to do this in order to enable caching.

    from p2p import P2P, cache
    p2p = P2P(
        url='url_of_p2p_endpoint',
        auth_token='<PASSWORD>',
        debug=False or True,
        image_services_url='url_of_image_services_endpoint',
        cache=cache.DictionaryCache(),
        preserve_embedded_tags=False or True
    )

## Testing

To run tests:

    $ python setup.py test


## Methods on the connection object

### Content items

`create_content_item(content_item)`

Create a new content item.

Takes a single dictionary representing the new content item. Refer to the P2P API docs for the content item field names.

`create_or_update_content_item(content_item)`

Attempts to update a content item, if it doesn't exist, attempts to create it.

`update_content_item(content_item, slug=None)`

Update a single content item.

Takes a single dictionary representing the content_item to be updated. Refer to the P2P API docs for the content item field names.

By default this function uses the value of the 'slug' key from the dictionary to perform the API call. It takes an optional `slug` parameter in case the dictionary does not contain a 'slug' key or if the dictionary contains a changed slug. Alternatively, an ID can be passed to the slug parameter.

`get_content_item(slug, query=None, force_update=False)`

Gets a single content item by slug or ID.

Takes an optional `query` parameter which is a dictionary containing parameters to pass along in the API call. See [query parameters](#query-parameters).

Use the parameter `force_update=True` to update the cache for this item and query.

`get_fancy_content_item(slug, query=None, related_items_query=None, force_update=False)`

Get a content item and its related items.

`get_multi_content_items(ids, query=None, force_update=False)`

Get a bunch of content items at once. We need to use the content items ids to use this API call.

The API only allows 25 items to be requested at once, so this function breaks the list of ids into groups of 25 and makes multiple API calls.

Takes an optional `query` parameter which is a dictionary containing parameters to pass along in the API call. See [query parameters](#query-parameters).

`delete_content_item(slug)`

Delete the content item out of p2p. Provide the slug of the content item you'd like to delete.

`junk_content_item(slug)`

Sets a content item to junk status. Provide the slug of the content item you'd like to junk.

`hide_right_rail(slug)`

Hide the right rail from an HTML story. Provide the slug of the content item you'd like to update.

`show_to_robots(slug)`

Add metadata to the item so it is seen by robots and remove any noindex and nofollow tags. Provide the slug of the content item you'd like to update.

`hide_to_robots(slug)`

Add metadata to the item so it is hidden from robots using the noindex and nofollow tags. Provide the slug of the content item you'd like to update.


### Topics

#### Single Topics

`add_topic(topic_id, slug=None)`

Adds a topic to a story. Provide the slug of the content item you'd like to update.

`remove_topic(topic_id, slug=None)`

Remove a topic from a story. Provide the slug of the content item you'd like to update.

#### Bulk Topics

To handle topics in bulk we have to add another key to the final payload to CS.
a standard `content_item` param for create_content_item() or update_content_item()
looks like so:
`{
  "content_item": {
    "slug": "la-test-slug-00000000",
    "title": "Test Story"
  }
}`

To process topics in bulk, 2 keys can be added to the `content_item` param:
`{
  "add_topic_ids": [
    "topic_id_1",
    "topic_id_2"
  ],
  "remove_topic_ids": [
    "topic_id_3",
    "topic_id_4"
  ],
  "content_item": {
    "slug": "la-test-slug-00000000",
    "title": "Test Story"
  }
}`

### Search

`search(params)`

Searches P2P for content items. Refer to the P2P API docs for parameter reference.


### Collections

`get_collection(code, query=None, force_update=False)`

Get the data for this collection. To get the items in a collection, use get_collection_layout.<commit_msg>updated docs again<commit_after># Python wrapper for P2P Content Services

## Connection configuration

Configuration settings. Set these values in your environment or your Django settings.

    P2P_API_KEY = your_p2p_api_key
    P2P_API_URL = url_of_p2p_endpoint
    P2P_API_DEBUG = plz  # display an http log
    P2P_PRESERVE_EMBEDDED_TAGS = False # set to false to fix encoding issues with special characters

    # Optional
    P2P_IMAGE_SERVICES_URL = url_of_image_services_endpoint

To get a connection object based on these settings:

    from p2p import get_connection
    p2p = get_connection()

Or you can create a connection object manually. You'll want to do this in order to enable caching.

    from p2p import P2P, cache
    p2p = P2P(
        url='url_of_p2p_endpoint',
        auth_token='<PASSWORD>',
        debug=False or True,
        image_services_url='url_of_image_services_endpoint',
        cache=cache.DictionaryCache(),
        preserve_embedded_tags=False or True
    )

## Testing

To run tests:

    $ python setup.py test


## Methods on the connection object

### Content items

`create_content_item(content_item)`

Create a new content item.

Takes a single dictionary representing the new content item. Refer to the P2P API docs for the content item field names.

`create_or_update_content_item(content_item)`

Attempts to update a content item, if it doesn't exist, attempts to create it.

`update_content_item(content_item, slug=None)`

Update a single content item.

Takes a single dictionary representing the content_item to be updated. Refer to the P2P API docs for the content item field names.

By default this function uses the value of the 'slug' key from the dictionary to perform the API call. It takes an optional `slug` parameter in case the dictionary does not contain a 'slug' key or if the dictionary contains a changed slug. Alternatively, an ID can be passed to the slug parameter.

`get_content_item(slug, query=None, force_update=False)`

Gets a single content item by slug or ID.

Takes an optional `query` parameter which is a dictionary containing parameters to pass along in the API call. See [query parameters](#query-parameters).

Use the parameter `force_update=True` to update the cache for this item and query.

`get_fancy_content_item(slug, query=None, related_items_query=None, force_update=False)`

Get a content item and its related items.

`get_multi_content_items(ids, query=None, force_update=False)`

Get a bunch of content items at once. We need to use the content items ids to use this API call.

The API only allows 25 items to be requested at once, so this function breaks the list of ids into groups of 25 and makes multiple API calls.

Takes an optional `query` parameter which is a dictionary containing parameters to pass along in the API call. See [query parameters](#query-parameters).

`delete_content_item(slug)`

Delete the content item out of p2p. Provide the slug of the content item you'd like to delete.

`junk_content_item(slug)`

Sets a content item to junk status. Provide the slug of the content item you'd like to junk.

`hide_right_rail(slug)`

Hide the right rail from an HTML story. Provide the slug of the content item you'd like to update.

`show_to_robots(slug)`

Add metadata to the item so it is seen by robots and remove any noindex and nofollow tags. Provide the slug of the content item you'd like to update.

`hide_to_robots(slug)`

Add metadata to the item so it is hidden from robots using the noindex and nofollow tags. Provide the slug of the content item you'd like to update.


### Topics

#### Single Topics

`add_topic(topic_id, slug=None)`

Adds a topic to a story. Provide the slug of the content item you'd like to update.

`remove_topic(topic_id, slug=None)`

Remove a topic from a story. Provide the slug of the content item you'd like to update.

#### Bulk Topics

To handle topics in bulk we have to add another key to the final payload to Content Services. The functions `create_content_item()` and `update_content_item()` accept a param called `content_item` that contains
the payload to be processed and sent to Content Services. Normally `content_item` looks like so:
`{
  "slug": "la-test-slug-00000000",
  "title": "Test Story",
  ...
}`

To process topics in bulk, 2 keys can be added to `content_item` param:
`{
  "add_topic_ids": [
    "topic_id_1",
    "topic_id_2"
  ],
  "remove_topic_ids": [
    "topic_id_3",
    "topic_id_4"
  ],
  "content_item": {
    "slug": "la-test-slug-00000000",
    "title": "Test Story",
    ...
  }
}`

### Search

`search(params)`

Searches P2P for content items. Refer to the P2P API docs for parameter reference.


### Collections

`get_collection(code, query=None, force_update=False)`

Get the data for this collection. To get the items in a collection, use get_collection_layout.
{'fraction_non_alphanumeric': 0.06703560613970973, 'fraction_numerical': 0.009397514879398558, 'mean_word_length': 4.196961475854585, 'pattern_counts': {'":': 15, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2417358', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2845, 'n_words': 1256}