Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
from fiftyone_devicedetection_cloud.devicedetection_cloud_pipelinebuilder import DeviceDetectionCloudPipelineBuilder
from fiftyone_devicedetection_examples.example_utils import ExampleUtils

# Where to ask about a paid resource key, for the properties a free key
# does not return.
CONTACT_US = "https://51degrees.com/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-configurator_console.py&utm_term=paid-resource-key"

# The properties that only a paid resource key returns, as the name the
# note gives and the property name.
PAID_PROPERTIES = [
("HardwareVendor", "hardwarevendor"),
("HardwareName", "hardwarename")]

# Where to go next, printed when the example ends.
FIND_OUT_MORE = [
("Device detection",
Expand Down Expand Up @@ -91,6 +101,17 @@ def run(self, resource_key, logger, output):
output("device.ismobile: " +
f"{ExampleUtils.get_human_readable(device, 'ismobile')}")

# The same for the properties only a paid resource key returns,
# saying once what to do about any the key cannot return.
missing = []
for label, property_name in PAID_PROPERTIES:
output(f"device.{property_name}: " +
f"{ExampleUtils.get_human_readable(device, property_name)}")
if ExampleUtils.needs_paid_key(device, property_name):
missing.append(label)
if missing:
output(ExampleUtils.get_paid_key_note(missing, CONTACT_US))

ExampleUtils.print_find_out_more(FIND_OUT_MORE, output)

def main(argv):
Expand All @@ -111,8 +132,8 @@ def main(argv):
"cloud service is accessed using a 'ResourceKey'. " +
"For more detail see " +
"https://51degrees.com/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-configurator_console.py&utm_term=resource-key-required. " +
"A resource key with the properties required by this " +
"example can be created for free at " +
"A resource key with the free properties this " +
"example uses can be created for free at " +
"https://51degrees.com/configure/1QWJwHxl?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-configurator_console.py&utm_term=resource-key-required. " +
"Once complete, populate the environment variable " +
"mentioned at the start of this message with the key.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
# it has no key.
from fiftyone_devicedetection_examples.example_utils import ExampleUtils

# Where to ask about a paid resource key, for the properties a free key
# does not return.
CONTACT_US = "https://51degrees.com/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-failuretomatch.py&utm_term=paid-resource-key"

# Where to go next, printed when the example ends.
FIND_OUT_MORE = [
("Device detection",
Expand All @@ -51,7 +55,7 @@
print(ExampleUtils.get_missing_resource_key_message())
# The address stays on one line, because the campaign lint reads a
# line at a time and a split address looks to it like a missing tag.
print(" To include the properties used in this example, go to "
print(" To include the free properties this example uses, go to "
"https://51degrees.com/configure/bxXqZhLT?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-failuretomatch.py&utm_term=resource-key-required")
else:

Expand Down Expand Up @@ -86,6 +90,14 @@
# Output why the value isn't meaningful
print(flowdata1.device.ismobile.no_value_message())

# The hardware vendor is only returned by a paid resource key, so it is
# read through the helper, which says why a value is missing rather
# than stopping the example.
print("Who made the device?: ")
print(ExampleUtils.get_human_readable(flowdata1.device, "hardwarevendor"))
if ExampleUtils.needs_paid_key(flowdata1.device, "hardwarevendor"):
print(ExampleUtils.get_paid_key_note(["HardwareVendor"], CONTACT_US))

# Now we do the same with a new User-Agent, this time a corrupted one

badUA = "--"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"elementPath": "fiftyone_pipeline_cloudrequestengine.cloudrequestengine",
"elementParameters": {
"settings": {
// Obtain a resource key with the properties required to run this
// example for free: https://51degrees.com/configure/1QWJwHxl?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-gettingstarted_console.json&utm_term=settings
// Obtain a resource key with the free properties this
// example uses: https://51degrees.com/configure/1QWJwHxl?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-gettingstarted_console.json&utm_term=settings
"resource_key": "!!ENTER_YOUR_RESOURCE_KEY_HERE!!",
"cloud_request_origin": "51Degrees.example.com"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
from fiftyone_pipeline_core.pipelinebuilder import PipelineBuilder
from fiftyone_devicedetection_examples.example_utils import ExampleUtils

# Where to ask about a paid resource key, for the properties a free key
# does not return.
CONTACT_US = "https://51degrees.com/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-gettingstarted_console.py&utm_term=paid-resource-key"

# Where to go next, printed when the example ends.
FIND_OUT_MORE = [
("Device detection",
Expand All @@ -67,9 +71,15 @@ def run(self, config, logger, output):
# Create the pipeline using the service provider and the configured options.
pipeline = PipelineBuilder().add_logger(logger).build_from_configuration(config)

# carry out some sample detections
# carry out some sample detections, noting the properties that the
# resource key cannot return
missing = []
for values in self.EvidenceValues:
self.analyseEvidence(values, pipeline, output)
missing += self.analyseEvidence(values, pipeline, output)

# Say once what to do about them.
if missing:
output(ExampleUtils.get_paid_key_note(missing, CONTACT_US))

ExampleUtils.print_find_out_more(FIND_OUT_MORE, output)

Expand Down Expand Up @@ -117,7 +127,18 @@ def analyseEvidence(self, evidence, pipeline, output):
self.outputValue("Platform Version", device.platformversion, message)
self.outputValue("Browser Name", device.browsername, message)
self.outputValue("Browser Version", device.browserversion, message)

# These properties are only returned by a paid resource key, so they
# are read through the helper, which says why a value is missing
# rather than stopping the example.
missing = []
for label, property_name in self.PaidProperties:
message.append(f"\t{label}: "
f"{ExampleUtils.get_human_readable(device, property_name)}\n")
if ExampleUtils.needs_paid_key(device, property_name):
missing.append(label)
output("".join(message))
return missing

def outputValue(self, name, value, message):
# Individual result values have a wrapper called
Expand All @@ -132,6 +153,12 @@ def outputValue(self, name, value, message):
else f"\t{name}: {value.no_value_message()}\n")


# The properties that only a paid resource key returns, as label and
# property name.
PaidProperties = [
("Hardware Vendor", "hardwarevendor"),
("Hardware Name", "hardwarename")]

# This collection contains the various input values that will
# be passed to the device detection algorithm.
EvidenceValues = [
Expand Down Expand Up @@ -189,7 +216,7 @@ def main(argv):
"service is accessed using a 'ResourceKey'. For more information " +
"see " +
"https://51degrees.com/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-gettingstarted_console.py&utm_term=resource-key-required. " +
"A resource key with the properties required by this example can be " +
"A resource key with the free properties this example uses can be " +
"created for free at https://51degrees.com/configure/1QWJwHxl?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-gettingstarted_console.py&utm_term=resource-key-required. " +
"Once complete, populate the config file or environment variable " +
"mentioned at the start of this message with the key.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@
import os
import sys

# Where to ask about a paid resource key, for the properties a free key
# does not return.
CONTACT_US = "https://51degrees.com/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-gettingstarted_web-app.py&utm_term=paid-resource-key"

# The properties on the page that only a paid resource key returns, as
# label and property name.
PAID_PROPERTIES = [
("Hardware Vendor", "hardwarevendor"),
("Hardware Name", "hardwarename"),
("Device Type", "devicetype"),
]


def get_paid_key_note_html(device):
"""!
The paid key note for the page, naming the paid properties the key
cannot return, or an empty string when it returns them all.
"""
missing = [label for label, name in PAID_PROPERTIES
if ExampleUtils.needs_paid_key(device, name)]
return ExampleUtils.get_paid_key_note_html(missing, CONTACT_US)


# Where to go next, printed when the example ends. The page ends with the
# same links, in templates/index.html.
FIND_OUT_MORE = [
Expand Down Expand Up @@ -203,6 +226,7 @@ def server():
'index.html',
data=flowdata,
utils=ExampleUtils,
paid_key_note=get_paid_key_note_html(flowdata.device),
response=response))

return response
Expand All @@ -225,8 +249,8 @@ def main(argv):
"cloud service is accessed using a 'ResourceKey'. " +
"For more detail see " +
"https://51degrees.com/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-gettingstarted_web-app.py&utm_term=resource-key-required. " +
"A resource key with the properties required by this " +
"example can be created for free at " +
"A resource key with the free properties this example uses " +
"can be created for free at " +
"https://51degrees.com/configure/g3gMZdPY?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-gettingstarted_web-app.py&utm_term=resource-key-required. " +
"Once complete, populated the environment variable " +
"mentioned at the start of this message with the key.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ <h3 class="c-eg-page__heading">Device data</h3>
<tr class="c-eg-table__row"><td class="c-eg-table__cell c-eg-table__cell--key">Device Id:</td><td class="c-eg-table__cell">{{ utils.get_human_readable(data.device, "deviceid") }}</td></tr>
</tbody>
</table>
{% if paid_key_note %}
<div class="c-eg-message"><p class="c-eg-message__text">{{ paid_key_note|safe }}</p></div>
{% endif %}

<h3 class="c-eg-page__heading">Client-side evidence and Apple models</h3>
<p class="c-eg-page__lead">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@
from fiftyone_pipeline_core.logger import Logger
from fiftyone_pipeline_core.web import webevidence, set_response_header

# Where to ask about a paid resource key, for the properties a free key
# does not return.
CONTACT_US = "https://51degrees.com/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-useragentclienthints_web-app.py&utm_term=paid-resource-key"

# The properties on the page that only a paid resource key returns, as
# label and property name.
PAID_PROPERTIES = [
("Hardware Vendor", "hardwarevendor"),
("Hardware Name", "hardwarename"),
("Device Type", "devicetype"),
]


def get_paid_key_note_html(device):
"""!
The paid key note for the page, naming the paid properties the key
cannot return, or an empty string when it returns them all.
"""
missing = [label for label, name in PAID_PROPERTIES
if ExampleUtils.needs_paid_key(device, name)]
return ExampleUtils.get_paid_key_note_html(missing, CONTACT_US)


# Where to go next, printed when the example ends. The page ends with the
# same links, in templates/index.html.
FIND_OUT_MORE = [
Expand Down Expand Up @@ -138,6 +161,7 @@ def server():
'index.html',
data=flowdata,
utils=ExampleUtils,
paid_key_note=get_paid_key_note_html(flowdata.device),
response=response))

return response
Expand All @@ -161,8 +185,8 @@ def main(argv):
"cloud service is accessed using a 'ResourceKey'. " +
"For more detail see " +
"https://51degrees.com/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-useragentclienthints_web-app.py&utm_term=resource-key-required. " +
"A resource key with the properties required by this " +
"example can be created for free at " +
"A resource key with the free properties this example uses " +
"can be created for free at " +
"https://51degrees.com/configure/?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-useragentclienthints_web-app.py&utm_term=resource-key-required. " +
"Once complete, populated the environment variable " +
"mentioned at the start of this message with the key.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ <h3 class="c-eg-page__heading">Detection results</h3>
<tr class="c-eg-table__row c-eg-table__row--alt"><td class="c-eg-table__cell c-eg-table__cell--key">Browser Version:</td><td class="c-eg-table__cell">{{ utils.get_human_readable(data.device, "browserversion") }}</td></tr>
</tbody>
</table>
{% if paid_key_note %}
<div class="c-eg-message"><p class="c-eg-message__text">{{ paid_key_note|safe }}</p></div>
{% endif %}
</div>

<div class="c-eg-message"><p class="c-eg-message__text">Want to try on-premise? <a href="https://51degrees.com/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-useragentclienthints_web-templates-index.html&utm_term=try-on-premise">Contact us</a> to discuss requirements.</p><a class="b-btn c-eg-message__cta" href="https://51degrees.com/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-useragentclienthints_web-templates-index.html&utm_term=try-on-premise">Contact us</a></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from datetime import datetime, timedelta
from threading import Event, Thread
import html
import os
import shlex
import shutil
Expand Down Expand Up @@ -138,6 +139,79 @@ def get_human_readable(device, property):

return f"Unknown ({reason})"

# The cloud service gives a property the resource key is not entitled
# to with no value and a reason that contains this.
PAID_FEATURE = "paid feature"

@staticmethod
def needs_paid_key(data, property_name):
"""!
True when the resource key in use cannot return the property. The
results leave out a property the key does not include, or give it
no value with a reason that says it is a paid feature.
"""

try:
value = data.get(property_name)
except Exception:
return True
if value is None:
return True
if value.has_value():
return False
reason = value.no_value_message() or ""
return ExampleUtils.PAID_FEATURE in reason.lower()

@staticmethod
def get_paid_key_note(missing, contact_us):
"""!
What to do about the properties the resource key in use cannot
return, written once after an example has shown them. It reads
right for a free key and for a paid key set up without them.
missing names the properties as the example shows them, and the
link is tagged with the example's file, so that an enquiry can be
traced to the example.
"""

names = list(dict.fromkeys(missing))
if len(names) == 1:
start = (f"This property needs a paid Resource Key: {names[0]}. "
"If yours is paid, add it")
else:
start = ("These properties need a paid Resource Key: "
f"{', '.join(names)}. If yours is paid, add them")
return (f"{start} on the configuration page. If it is free, contact "
f"us about a paid one at {contact_us}")

@staticmethod
def get_paid_key_note_html(missing, contact_us):
"""!
The paid key note as HTML for a page, with the contact link as a
link, or an empty string when nothing is missing.
"""

if not missing:
return ""
link = html.escape(contact_us)
return html.escape(
ExampleUtils.get_paid_key_note(missing, contact_us)).replace(
link, f'<a href="{link}">{link}</a>')

@staticmethod
def get_missing_evidence_file_message(file_name, argument):
"""!
The message shown when the file of evidence records an example reads
is missing, saying where the file goes. argument says how the
example takes the file's path instead.
"""

return (f"The evidence file '{file_name}' is missing, so the example "
"cannot run. It goes in the "
f"'{ExampleUtils.DATA_FOLDER}' folder of a clone of this "
f"repository, beside '{file_name}.link', where the example "
"downloads it when it can. Or give its path "
f"{argument}.")

@staticmethod
def get_resource_key():
return ExampleUtils.__get_env_variable(
Expand Down
Loading
Loading