Skip to content

Commit

Permalink
Add Video Search sample
Browse files Browse the repository at this point in the history
  • Loading branch information
lmazuel committed Nov 30, 2017
1 parent 2ca5b11 commit 3460afe
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project framework provides examples for the following services:

* Using the **Bing Entity Search SDK** [azure-cognititiveservices-search-entitysearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-entitysearch) for the [Entity Search API](https://azure.microsoft.com/en-us/services/cognitive-services/bing-entity-search-api/)
* Using the **Bing Web Search SDK** [azure-cognititiveservices-search-websearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-websearch) for the [Web Search API](https://azure.microsoft.com/en-us/services/cognitive-services/bing-web-search-api/)
* Using the **Bing Video Search SDK** [azure-cognititiveservices-search-videosearch](http://pypi.python.org/pypi/azure-cognititiveservices-search-videosearch) for the [Video Search API](https://azure.microsoft.com/en-us/services/cognitive-services/bing-video-search-api/)

We provide several meta-packages to help you install several packages at a time. Please note that meta-packages are only recommended for development purpose. It's recommended in production to always pin specific version of individual packages.

Expand Down Expand Up @@ -52,6 +53,7 @@ We provide several meta-packages to help you install several packages at a time.
3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your CS key if you want to execute EntitySearch tests.
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your CS key if you want to execute WebSearch tests.
4. Set up the environment variable `VIDEOSEARCH_SUBSCRIPTION_KEY` with your CS key if you want to execute VideoSearch tests.
## Demo
Expand All @@ -63,6 +65,7 @@ To run each individual demo, point directly to the file:
1. `python samples/entity_search_samples.py`
2. `python samples/web_search_samples.py`
2. `python samples/video_search_samples.py`
To see the code of each example, simply look at the examples in the Samples folder. They are written to be isolated in scope so that you can see only what you're interested in.
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
git+https://github.com/Azure/azure-sdk-for-python#egg=azure-cognitiveservices-search-entitysearch&subdirectory=azure-cognitiveservices-search-entitysearch
git+https://github.com/Azure/azure-sdk-for-python#egg=azure-cognitiveservices-search-websearch&subdirectory=azure-cognitiveservices-search-websearch
git+https://github.com/Azure/azure-sdk-for-python#egg=azure-cognitiveservices-search-websearch&subdirectory=azure-cognitiveservices-search-websearch
git+https://github.com/Azure/azure-sdk-for-python#egg=azure-cognitiveservices-search-videosearch&subdirectory=azure-cognitiveservices-search-videosearch
148 changes: 148 additions & 0 deletions samples/video_search_samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
from azure.cognitiveservices.search.videosearch import VideoSearchAPI
from azure.cognitiveservices.search.videosearch.models import VideoPricing, VideoLength, VideoResolution, VideoInsightModule
from msrest.authentication import CognitiveServicesCredentials

SUBSCRIPTION_KEY_ENV_NAME = "VIDEOSEARCH_SUBSCRIPTION_KEY"

def video_search(subscription_key):
"""VideoSearch.
This will search videos for (Nasa CubeSat) then verify number of results and print out id, name and url of first video result.
"""
client = VideoSearchAPI(CognitiveServicesCredentials(subscription_key))

try:
video_result = client.videos.search(query="Nasa CubeSat")
print("Search videos for query \"Nasa CubeSat\"")

if video_result.value:
first_video_result = video_result.value[0]
print("Video result count: {}".format(len(video_result.value)))
print("First video id: {}".format(first_video_result.video_id))
print("First video name: {}".format(first_video_result.name))
print("First video url: {}".format(first_video_result.content_url))
else:
print("Didn't see any video result data..")

except Exception as err:
print("Encountered exception. {}".format(err))


def video_search_with_filtering(subscription_key):
"""VideoSearchWithFilters.
This will search videos for (Interstellar Trailer) that is free, short and 1080p resolution then verify number of results and print out id, name and url of first video result
"""
client = VideoSearchAPI(CognitiveServicesCredentials(subscription_key))

try:
video_result = client.videos.search(
query="Interstellar Trailer",
pricing=VideoPricing.free, # Can use the str "free" too
length=VideoLength.short, # Can use the str "short" too
resolution=VideoResolution.hd1080p # Can use the str "hd1080p" too
)
print("Search videos for query \"Interstellar Trailer\" that is free, short and 1080p resolution")

if video_result.value:
first_video_result = video_result.value[0]
print("Video result count: {}".format(len(video_result.value)))
print("First video id: {}".format(first_video_result.video_id))
print("First video name: {}".format(first_video_result.name))
print("First video url: {}".format(first_video_result.content_url))
else:
print("Didn't see any video result data..")

except Exception as err:
print("Encountered exception. {}".format(err))


def video_trending(subscription_key):
"""VideoTrending.
This will trending videos then verify banner tiles and categories.
"""
client = VideoSearchAPI(CognitiveServicesCredentials(subscription_key))

try:
trending_result = client.videos.trending()
print("Search trending video")

# Banner tiles
if trending_result.banner_tiles:
first_banner_tile = trending_result.banner_tiles[0]
print("Banner tile count: {}".format(len(trending_result.banner_tiles)))
print("First banner tile text: {}".format(first_banner_tile.query.text))
print("First banner tile url: {}".format(first_banner_tile.query.web_search_url))
else:
print("Couldn't find banner tiles!")

# Categorires
if trending_result.categories:
first_category = trending_result.categories[0]
print("Category count: {}".format(len(trending_result.categories)))
print("First category title: {}".format(first_category.title))
if first_category.subcategories:
first_subcategory = first_category.subcategories[0]
print("Subcategory count: {}".format(len(first_category.subcategories)))
print("First subcategory title: {}".format(first_subcategory.title))
if first_subcategory.tiles:
first_tile = first_subcategory.tiles[0]
print("Subcategory tile count: {}".format(len(first_subcategory.tiles)))
print("First tile text: {}".format(first_tile.query.text))
print("First tile url: {}".format(first_tile.query.web_search_url))
else:
print("Couldn't find subcategory tiles!")
else:
print("Couldn't find subcategories!")
else:
print("Couldn't find categories!")

except Exception as err:
print("Encountered exception. {}".format(err))


def video_detail(subscription_key):
"""VideoDetail.
This will search videos for (Interstellar Trailer) and then search for detail information of the first video
"""
client = VideoSearchAPI(CognitiveServicesCredentials(subscription_key))

try:
video_result = client.videos.search(query="Interstellar Trailer")
first_video_result = video_result.value[0]

video_details = client.videos.details(
query="Interstellar Trailer",
id=first_video_result.video_id,
modules=[VideoInsightModule.all] # Can use ["all"] too
)
print("Search detail for video id={}, name={}".format(
first_video_result.video_id,
first_video_result.name
))

if video_details.video_result:
print("Expected Video id: {}".format(video_details.video_result.video_id))
print("Expected Video name: {}".format(video_details.video_result.name))
print("Expected Video url: {}".format(video_details.video_result.content_url))
else:
print("Couldn't find expected video")

if video_details.related_videos.value:
first_related_video = video_details.related_videos.value[0]
print("Related video count: {}".format(len(video_details.related_videos.value)))
print("First related video id: {}".format(first_related_video.video_id))
print("First related video name: {}".format(first_related_video.name))
print("First related video content url: {}".format(first_related_video.content_url))
else:
print("Couldn't find any related video!")

except Exception as err:
print("Encountered exception. {}".format(err))


if __name__ == "__main__":
from tools import execute_samples
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 comments on commit 3460afe

Please sign in to comment.