diff --git a/DEMO.ipynb b/DEMO.ipynb index 72a59a6..252dc32 100644 --- a/DEMO.ipynb +++ b/DEMO.ipynb @@ -38,7 +38,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Typically, you'd import only the classes you plan on using, but for this demo all classes are listed except for superclasses which you do not use explicitly)" + "Typically, you'd import only the classes you plan on using, but for this demo all classes are listed except for superclasses which you do not use explicitly." ] }, { @@ -61,7 +61,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -71,7 +71,7 @@ "#(Default) All logging messages enabled\n", "#logger.setLevel(logging.DEBUG)\n", "\n", - "#Does not report URL's of API requests, but all other messages enabled\n", + "#Does not report URLs of API requests, but all other messages enabled\n", "#logger.setLevel(logging.INFO)\n", "\n", "#Report only errors and warnings\n", @@ -95,7 +95,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "When you use the API for the first time you have to authenticate with Brandwatch. This will get you an access token. The access token is stored in a credentials file (`tokens.txt` in this example). Once you've authenticated your access token will be read from that file so you won't need to enter your password again.\n", + "When you use the API for the first time you have to authenticate with Brandwatch. This will get you an access token. The access token is stored in a credentials file (`tokens.txt` in this example). Once you've authenticated your access token will be read from that file so you won't need to enter your password again (until your token expires).\n", "\n", "You can authenticate from command line using the provided console script `bcr-authenticate`:\n", "\n", @@ -238,13 +238,19 @@ " \"booleanQuery\":\"(\\\"ice cream\\\" OR icecream) AND (cake)\"},\n", " \n", " {\"name\": \"Test1\",\n", - " \"booleanQuery\": \"akdnvaoifg;anf\"},\n", + " \"booleanQuery\": \"dog OR cat\"},\n", " \n", " {\"name\": \"Test2\",\n", - " \"booleanQuery\": \"anvoapihajkvn\"},\n", + " \"booleanQuery\": \"mouse OR rat\"},\n", " \n", " {\"name\": \"Test3\",\n", - " \"booleanQuery\": \"nviuphabaveh\"},\n", + " \"booleanQuery\": \"magpie OR parrot\"},\n", + " \n", + " {\"name\": \"Test4\",\n", + " \"booleanQuery\": \"llama OR rhea\"},\n", + " \n", + " {\"name\": \"Test5\",\n", + " \"booleanQuery\": \"capybara OR quokka\"},\n", "\n", " ])" ] @@ -262,8 +268,8 @@ "metadata": {}, "outputs": [], "source": [ - "queries.delete(name = \"Brandwatch Engagement\")\n", - "queries.delete_all([\"Pets\", \"Test3\", \"Brandwatch\", \"BWReact\", \"Brandwatch Careers\"])" + "queries.delete(name = \"Pets\")\n", + "queries.delete_all([\"ice cream cake\", \"Test2\", \"Test3\"])" ] }, { @@ -341,7 +347,7 @@ "metadata": {}, "outputs": [], "source": [ - "groups.upload(name = \"group 1\", queries = [\"Test1\", \"Test2\"])" + "groups.upload(name = \"group 1\", queries = [\"Test4\", \"Test5\"])" ] }, { @@ -358,10 +364,10 @@ "outputs": [], "source": [ "groups.upload_queries_as_group(group_name = \"group 2\", \n", - " query_data_list = [{\"name\": \"Test3\",\n", + " query_data_list = [{\"name\": \"Test6\",\n", " \"booleanQuery\": \"adcioahnanva\"},\n", " \n", - " {\"name\": \"Test4\",\n", + " {\"name\": \"Test7\",\n", " \"booleanQuery\": \"ioanvauhekanv;\"}])" ] }, @@ -410,7 +416,7 @@ "outputs": [], "source": [ "today = (datetime.date.today() + datetime.timedelta(days=1)).isoformat() + \"T05:00:00\"\n", - "start = (datetime.date.today() - datetime.timedelta(days=29)).isoformat() + \"T05:00:00\"" + "start = (datetime.date.today() - datetime.timedelta(days=2)).isoformat() + \"T05:00:00\"" ] }, { @@ -426,7 +432,7 @@ "metadata": {}, "outputs": [], "source": [ - "filtered = queries.get_mentions(name = \"ice cream cake\",\n", + "filtered = queries.get_mentions(name = \"Test5\",\n", " startDate = start, \n", " endDate = today)" ] @@ -448,7 +454,7 @@ "metadata": {}, "outputs": [], "source": [ - "filtered = queries.get_mentions(name = \"ice cream cake\", \n", + "filtered = queries.get_mentions(name = \"Test5\", \n", " startDate = start, \n", " endDate = today, \n", " sentiment = \"positive\", \n", @@ -474,7 +480,7 @@ "metadata": {}, "outputs": [], "source": [ - "filtered = queries.get_mentions(name = \"ice cream cake\", \n", + "filtered = queries.get_mentions(name = \"Test5\", \n", " startDate = start, \n", " endDate = today,\n", " parentCategory = [\"Colors\", \"Days\"],\n", @@ -492,6 +498,28 @@ "filtered[0]" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can make many calls to aggregate data, just as you would in a BCR dashboard. For example, here we break down volume by sentiment:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sentiment_volumes = queries.get_chart(name='Test1', \n", + " startDate=start, \n", + " endDate=today,\n", + " y_axis='volume', \n", + " x_axis='sentiment',\n", + " breakdown_by='queries')\n", + "sentiment_volumes['results']" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -527,7 +555,7 @@ "\n", "For upload(), pass in name and children. name is the string which represents the parent category, and children is a list of dictionaries where each dictionary is a child category- its key is \"name\" and its value is the name of the child category.\n", "\n", - "By default, a category will allow multiple subcategories to be applies, so the keyword argument \"multiple\" is set to True. You can manually set it to False by passing in multipe=False as another parameter when uploading a category.\n", + "By default, a category will allow multiple subcategories to be applied, so the keyword argument \"multiple\" is set to True. You can manually set it to False by passing in multipe=False as another parameter when uploading a category.\n", "\n", "For upload_all(), pass in a list of dictionaries, where each dictionary corrosponds to one category, and contains the parameters described above." ] @@ -588,6 +616,61 @@ "categories.upload(name = \"Droids\", children = [\"bb8\"])" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It is also possible to upload categories with the associated boolean and filters, as shown in the example below. To do this, you can use the separate `upload_rule_categories` method:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "child_categories_and_rules = [\n", + " {\"name\": \"Positive views on chocolate\",\n", + " \"rules\": [\n", + " {\"filter\": {\"search\": \"chocolate OR chocco*\",\n", + " \"sentiment\": [\"positive\"],}\n", + " }\n", + " ]\n", + " },\n", + " {\"name\": \"Vanilla\",\n", + " \"rules\": [\n", + " {\"filter\": {\"search\": \"vanilla\",}\n", + " }\n", + " ]\n", + " }\n", + " ]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "categories.upload_rule_categories(name='ice cream views', children=child_categories_and_rules)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Please note that all rules which add categories or tags should now use the `BWCategories.upload_rule_categories()` and `BWTags.upload()` methods**, rather than using `BWRules`. This reflects updates to the BCR API outlined [here](https://developers.brandwatch.com/docs/retrieving-categories-2) and [here](https://developers.brandwatch.com/docs/retrieving-tags-2)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "categories.delete(name='ice cream views')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -630,18 +713,7 @@ "outputs": [], "source": [ "categories.delete({\"name\": \"Months\", \"children\":[\"February\"]})\n", - "categories.delete_all([{\"name\": \"Droids\", \"children\": [\"bb8\", \"c3po\"]}])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "categories.delete(\"Droids\")\n", - "categories.delete_all([\"Months\", \"Time of Day\"])\n", - "\n", + "categories.delete_all([{\"name\": \"Droids\", \"children\": [\"bb8\", \"c3po\"]}])\n", "categories.ids" ] }, @@ -697,6 +769,37 @@ "tags.names" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It is also possible to upload tags with the associated boolean and filters, as shown in the example below. You can specify multiple rules (searches and filters), but in this case we only have one rule to populate this tag with mentions." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "rules_for_tag = [\n", + " {\n", + " \"filter\": {\n", + " \"search\": \"cat OR cats\",\n", + " }\n", + " },\n", + " ]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tags.upload(name='cats', rules=rules_for_tag)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -710,7 +813,7 @@ "metadata": {}, "outputs": [], "source": [ - "tags.upload(name = \"yellow\", new_name = \"yellow-orange blend\")\n", + "tags.upload(name = \"cats\", new_name = \"Felis catus\")\n", "\n", "tags.names" ] @@ -735,7 +838,7 @@ "outputs": [], "source": [ "tags.delete(\"purple\")\n", - "tags.delete_all([\"blue\", \"green\", \"yellow-orange blend\"])\n", + "tags.delete_all([\"blue\", \"green\"])\n", "\n", "tags.names" ] @@ -922,7 +1025,7 @@ "metadata": {}, "outputs": [], "source": [ - "filters = rules.filters(queryName = \"ice cream cake\", \n", + "filters = rules.filters(queryName = \"Test1\", \n", " sentiment = \"positive\", \n", " twitterVerified = False, \n", " impactMin = 50, \n", @@ -935,7 +1038,7 @@ "metadata": {}, "outputs": [], "source": [ - "filters = rules.filters(queryName = [\"Australian Animals\", \"ice cream cake\"], \n", + "filters = rules.filters(queryName = [\"Test1\", \"Test4\"], \n", " search = '\"cat food\" OR \"dog food\"')" ] }, @@ -945,9 +1048,7 @@ "source": [ "The second step is to prepare the rule action by calling rule_action().\n", "\n", - "For this function, you must pass in the action and setting. Below I've used examples of adding categories and tags, but you can also set sentiment or workflow (as in the front end).\n", - "\n", - "If you pass in a category or tag that does not yet exist, it will be automatically uploaded for you." + "For this function, you must pass in the action and setting. Lots of rule actions are available, but if you want to add categories or tags, these should be handled by uploading tags and associated rules at the same time by using `tags.upload()` or `categories.upload_rule_categories()`." ] }, { @@ -956,8 +1057,8 @@ "metadata": {}, "outputs": [], "source": [ - "action = rules.rule_action(action = \"addTag\", \n", - " setting = [\"animal food\"])" + "action = rules.rule_action(action = \"priority\", \n", + " setting = \"high\")" ] }, { @@ -998,11 +1099,11 @@ "outputs": [], "source": [ "filters1 = rules.filters(search = \"caknvfoga;vnaei\")\n", - "filters2 = rules.filters(queryName = [\"Australian Animals\"], search = \"(bloop NEAR/10 blorp)\")\n", - "filters3 = rules.filters(queryName = [\"Australian Animals\", \"ice cream cake\"], search = '\"hello world\"')\n", + "filters2 = rules.filters(queryName = [\"Test1\"], search = \"(bloop NEAR/10 blorp)\")\n", + "filters3 = rules.filters(queryName = [\"Test1\", \"Test4\"], search = '\"hello world\"')\n", "\n", - "action1 = rules.rule_action(action = \"addCategories\", setting = {\"Example\": [\"One\"]})\n", - "action2 = rules.rule_action(action = \"addTag\", setting = [\"My Example\"])" + "action1 = rules.rule_action(action = \"sentiment\", setting = \"positive\")\n", + "action2 = rules.rule_action(action = \"starred\", setting = True)" ] }, { @@ -1240,11 +1341,18 @@ "source": [ "mentions.patch_mentions(filtered[6:8], action = \"addCategories\", setting = {\"color\":[\"green\", \"blue\"]})" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -1258,7 +1366,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.10.2" } }, "nbformat": 4,