Microsoft Power Automate "Switch" - Action not matching Case results - power-automate

I have a flow with a switch control triggered by a form, where the switch depends on the answer to one of the questions (Single Choice + "Other" with an empty space, hence the "Default" case).
There is also Compose action to replace spacing before the switch action:
replace(variables('aplicabilidade'), ' ', '')
("aplicabilidade" being the value extracted from the Form)
and then I manually applied the same replacement to the values before adding them to the switch control. However, while the Case is correctly identified, the Flow does not follow-up with the correct action:
In my original post in the Power Automate forum a user suggested using Trim instead of Replace, but it not change the result.
Since (for other than spacing) the values are literally copy-paste from the Forms to the Switch, Case Sensitivity should not be an issue.
I tested the form with an answer that I know matches Case 4, and when the Flow is run it indeed identifies it as matching; however, instead of following the action intended in that Case, it triggers the Default Case action.
Any ideas?
I attempted to use a Switch Control on a Power Automate Flow to send different emails based on an answer in a Microsoft Forms. However, the flow always triggers the default action even when correctly matching any of the existing Cases.
EDIT:
As requested, json code that I was able to extract from Microsoft Flow Platform:
definition.json
"name":"10fe749a-1fad-4da7-a482-630fc9e4d102","id":"/providers/Microsoft.Flow/flows/10fe749a-1fad-4da7-a482-630fc9e4d102","type":"Microsoft.Flow/flows","properties":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_logicflows","displayName":"Sugestões MC","definition":{"metadata":{"workflowEntityId":null,"processAdvisorMetadata":null,"flowclientsuspensionreason":"None","flowclientsuspensiontime":null,"creator":{"id":"f9028ef6-5f90-4c4b-a97e-3b027eeeea8b","type":"User","tenantId":"f9082694-5379-4912-bbe1-1c5e9b6737d7"},"provisioningMethod":"FromDefinition","failureAlertSubscription":true,"clientLastModifiedTime":"2022-03-24T15:00:54.6466915Z"},"$schema":"https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#","contentVersion":"1.0.0.0","parameters":{"$connections":{"defaultValue":{},"type":"Object"},"$authentication":{"defaultValue":{},"type":"SecureObject"}},"triggers":{"When_a_new_response_is_submitted":{"splitOn":"#triggerOutputs()?['body/value']","metadata":{"operationMetadataId":"6b49d155-7960-482f-8ae4-7d0b4b87f316"},"type":"OpenApiConnectionWebhook","inputs":{"host":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_microsoftforms","connectionName":"shared_microsoftforms","operationId":"CreateFormWebhook"},"parameters":{"form_id":"lCYI-XlTEkm74Rxem2c312Y6IcYaoPlGrOot18luu5lUOEUzN00wV1c0UFBPUkxONFhVWVJLR0RaNCQlQCN0PWcu"},"authentication":"#parameters('$authentication')"}}},"actions":{"Switch":{"runAfter":{"Compose":["Succeeded"]},"cases":{"Case":{"case":"InformaçãoTécnico-CientíficadeMedicamentos\n","actions":{"Send_an_email_notification_(V3)_6":{"runAfter":{},"type":"OpenApiConnection","inputs":{"host":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_sendmail","connectionName":"shared_sendmail","operationId":"SendEmailV3"},"parameters":{"request/to":"XXX#XXX.XX;","request/subject":"MC - Nova Sugestão ","request/text":"<p>Bom dia,<br>\n<br>\nFoi efetuada a seguinte sugestão no formulário de Melhoria Contínua:<br>\n<br>\nTema: #{outputs('Get_response_details_2')?['body/rf479dcadc3884bf09c7c7ad0bb15dfa9']}<br>\nDetalhe: #{outputs('Get_response_details_2')?['body/r31fe96bb0b4a431896902e60a110d3f2']}<br>\nSugerida por: #{outputs('Get_response_details_2')?['body/r0a0b849bba0b4c7f9da5734602f6aefb']}<br>\n<br>\nSolicitamos análise e follow-up do pedido com a maior brevidade possível.<br>\n<br>\nAtentamente,</p>","request/cc":"XX#XX.XX; XX#XX.XX"},"authentication":"#parameters('$authentication')"}}}},"Case_2":{"case":"IntervençõesemSaúde\n","actions":{"Send_an_email_notification_(V3)_5":{"runAfter":{},"type":"OpenApiConnection","inputs":{"host":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_sendmail","connectionName":"shared_sendmail","operationId":"SendEmailV3"},"parameters":{"request/to":"XX#XX.XX;","request/subject":"MC - Nova Sugestão ","request/text":"<p>Bom dia,<br>\n<br>\nFoi efetuada a seguinte sugestão no formulário de Melhoria Contínua:<br>\n<br>\nTema: #{outputs('Get_response_details_2')?['body/rf479dcadc3884bf09c7c7ad0bb15dfa9']}<br>\nDetalhe: #{outputs('Get_response_details_2')?['body/r31fe96bb0b4a431896902e60a110d3f2']}<br>\nSugerida por: #{outputs('Get_response_details_2')?['body/r0a0b849bba0b4c7f9da5734602f6aefb']}<br>\n<br>\nSolicitamos análise e follow-up do pedido com a maior brevidade possível.<br>\n<br>\nAtentamente,</p>","request/cc":"XX#XX.XX; XX#XX.XX"},"authentication":"#parameters('$authentication')"}}}},"Case_3":{"case":"LiteraciaemSaúde\n","actions":{"Send_an_email_notification_(V3)_4":{"runAfter":{},"type":"OpenApiConnection","inputs":{"host":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_sendmail","connectionName":"shared_sendmail","operationId":"SendEmailV3"},"parameters":{"request/to":"XX#XX.XX;","request/subject":"MC - Nova Sugestão ","request/text":"<p>Bom dia,<br>\n<br>\nFoi efetuada a seguinte sugestão no formulário de Melhoria Contínua:<br>\n<br>\nTema: #{outputs('Get_response_details_2')?['body/rf479dcadc3884bf09c7c7ad0bb15dfa9']}<br>\nDetalhe: #{outputs('Get_response_details_2')?['body/r31fe96bb0b4a431896902e60a110d3f2']}<br>\nSugerida por: #{outputs('Get_response_details_2')?['body/r0a0b849bba0b4c7f9da5734602f6aefb']}<br>\n<br>\nSolicitamos análise e follow-up do pedido com a maior brevidade possível.<br>\n<br>\nAtentamente,</p>","request/cc":"XX#XX.XX; XX#XX.XX"},"authentication":"#parameters('$authentication')"}}}},"Case_4":{"case":"Serviço de Consultas\n","actions":{"Send_an_email_notification_(V3)_3":{"runAfter":{},"type":"OpenApiConnection","inputs":{"host":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_sendmail","connectionName":"shared_sendmail","operationId":"SendEmailV3"},"parameters":{"request/to":"XX#XX.XX;","request/subject":"MC - Nova Sugestão ","request/text":"<p>Bom dia,<br>\n<br>\nFoi efetuada a seguinte sugestão no formulário de Melhoria Contínua:<br>\n<br>\nTema: #{outputs('Get_response_details_2')?['body/rf479dcadc3884bf09c7c7ad0bb15dfa9']}<br>\nDetalhe: #{outputs('Get_response_details_2')?['body/r31fe96bb0b4a431896902e60a110d3f2']}<br>\nSugerida por: #{outputs('Get_response_details_2')?['body/r0a0b849bba0b4c7f9da5734602f6aefb']}<br>\n<br>\nSolicitamos análise e follow-up do pedido com a maior brevidade possível.<br>\n<br>\nAtentamente,</p>","request/cc":"XX#XX.XX; XX#XX.XX"},"authentication":"#parameters('$authentication')"}}}},"Case_5":{"case":"Geral/GestãodoCEDIME\n","actions":{"Send_an_email_notification_(V3)_2":{"runAfter":{},"type":"OpenApiConnection","inputs":{"host":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_sendmail","connectionName":"shared_sendmail","operationId":"SendEmailV3"},"parameters":{"request/to":"XX#XX.XX;","request/subject":"MC - Nova Sugestão ","request/text":"<p>Bom dia,<br>\n<br>\nFoi efetuada a seguinte sugestão no formulário de Melhoria Contínua:<br>\n<br>\nTema: #{outputs('Get_response_details_2')?['body/rf479dcadc3884bf09c7c7ad0bb15dfa9']}<br>\nDetalhe: #{outputs('Get_response_details_2')?['body/r31fe96bb0b4a431896902e60a110d3f2']}<br>\nSugerida por: #{outputs('Get_response_details_2')?['body/r0a0b849bba0b4c7f9da5734602f6aefb']}<br>\n<br>\nSolicitamos análise e follow-up do pedido com a maior brevidade possível.<br>\n<br>\nAtentamente,</p>","request/cc":"XX#XX.XX; XX#XX.XX"},"authentication":"#parameters('$authentication')"}}}}},"default":{"actions":{"Send_an_email_notification_(V3)_7":{"runAfter":{},"type":"OpenApiConnection","inputs":{"host":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_sendmail","connectionName":"shared_sendmail","operationId":"SendEmailV3"},"parameters":{"request/to":"XX#XX.XX","request/subject":"MC - Nova Sugestão ","request/text":"<p>Bom dia,<br>\n<br>\nFoi efetuada a seguinte sugestão no formulário de Melhoria Contínua:<br>\n<br>\nTema: #{outputs('Get_response_details_2')?['body/rf479dcadc3884bf09c7c7ad0bb15dfa9']}<br>\nDetalhe: #{outputs('Get_response_details_2')?['body/r31fe96bb0b4a431896902e60a110d3f2']}<br>\nSugerida por: #{outputs('Get_response_details_2')?['body/r0a0b849bba0b4c7f9da5734602f6aefb']}<br>\n<br>\nSolicitamos análise e follow-up do pedido com a maior brevidade possível.<br>\n<br>\nAtentamente,</p>"},"authentication":"#parameters('$authentication')"}}}},"expression":"#outputs('Compose')","metadata":{"operationMetadataId":"91e61dbb-4330-4958-b9b0-234c88e46269"},"type":"Switch"},"Initialize_variable":{"runAfter":{},"metadata":{"operationMetadataId":"2b93713b-f1c3-4e32-a45d-e7f92d417601"},"type":"InitializeVariable","inputs":{"variables":[{"name":"aplicabilidade","type":"string"}]}},"Get_response_details_2":{"runAfter":{"Initialize_variable":["Succeeded"]},"metadata":{"operationMetadataId":"a09d568b-4d47-4c19-9973-6822901883b3"},"type":"OpenApiConnection","inputs":{"host":{"apiId":"/providers/Microsoft.PowerApps/apis/shared_microsoftforms","connectionName":"shared_microsoftforms","operationId":"GetFormResponseById"},"parameters":{"form_id":"lCYI-XlTEkm74Rxem2c312Y6IcYaoPlGrOot18luu5lUOEUzN00wV1c0UFBPUkxONFhVWVJLR0RaNCQlQCN0PWcu","response_id":"#triggerOutputs()?['body/resourceData/responseId']"},"authentication":"#parameters('$authentication')"}},"Set_variable":{"runAfter":{"Get_response_details_2":["Succeeded"]},"metadata":{"operationMetadataId":"70ac8adb-8da6-462d-afa0-1a5ef8db7ccc"},"type":"SetVariable","inputs":{"name":"aplicabilidade","value":"#outputs('Get_response_details_2')?['body/rf479dcadc3884bf09c7c7ad0bb15dfa9']"}},"Compose":{"runAfter":{"Set_variable":["Succeeded"]},"metadata":{"operationMetadataId":"e12e505a-c214-465a-90a5-f8e1f6028f20"},"type":"Compose","inputs":"#trim(variables('aplicabilidade'))"}}},"connectionReferences":{"shared_sendmail":{"connectionName":"shared-sendmail-fbcdc7a1-f228-45b6-8ec5-53f0f421e355","source":"Embedded","id":"/providers/Microsoft.PowerApps/apis/shared_sendmail","tier":"NotSpecified"},"shared_microsoftforms":{"connectionName":"shared-microsoftform-9b29b94d-94d8-434e-9909-861cecd152f4","source":"Embedded","id":"/providers/Microsoft.PowerApps/apis/shared_microsoftforms","tier":"NotSpecified"}},"flowFailureAlertSubscribed":false}}
apisMap.json
{"shared_sendmail":"75acecb3-ef37-4d57-99e2-37af8154d7db","shared_microsoftforms":"5ccd7725-e205-445d-9d1f-9b28a19e8263"}
connectionsMap.json
{"shared_sendmail":"d3995a2b-3566-48f9-b4d4-13a8c8d0d01c","shared_microsoftforms":"9fe07a7e-3104-4c22-a9ec-bc81d18469f0"}
EDIT 2:
"When a new response is submitted"
{
"inputs": {
"host": {
"connectionName": "shared_microsoftforms",
"operationId": "CreateFormWebhook",
"apiId": "/providers/Microsoft.PowerApps/apis/shared_microsoftforms"
},
"parameters": {
"form_id": "lCYI-XlTEkm74Rxem2c312Y6IcYaoPlGrOot18luu5lUOEUzN00wV1c0UFBPUkxONFhVWVJLR0RaNCQlQCN0PWcu"
},
"authentication": "#parameters('$authentication')"
},
"metadata": {
"operationMetadataId": "6b49d155-7960-482f-8ae4-7d0b4b87f316"
},
"splitOn": "#triggerOutputs()?['body/value']"
}
And then "Get response details":
{
"inputs": {
"host": {
"connectionName": "shared_microsoftforms",
"operationId": "GetFormResponseById",
"apiId": "/providers/Microsoft.PowerApps/apis/shared_microsoftforms"
},
"parameters": {
"form_id": "lCYI-XlTEkm74Rxem2c312Y6IcYaoPlGrOot18luu5lUOEUzN00wV1c0UFBPUkxONFhVWVJLR0RaNCQlQCN0PWcu",
"response_id": "#triggerOutputs()?['body/resourceData/responseId']"
},
"authentication": "#parameters('$authentication')"
},
"metadata": {
"operationMetadataId": "a09d568b-4d47-4c19-9973-6822901883b3"
}
}
EDIT 3: Data from run history
"When a new response is submitted":
{"headers":{"Expect":"100-continue","Host":"prod-214.westeurope.logic.azure.com","X-CorrelationId":"ee542f19-e358-4f9d-8b6f-9b8e823d2b04","Content-Length":"267","Content-Type":"application/json; charset=utf-8"},"body":{"webhookId":"cb5188c2-c861-4722-92cd-cbd942baeb10","eventType":"ResponseAdded","resourceData":{"formId":"lCYI-XlTEkm74Rxem2c312Y6IcYaoPlGrOot18luu5lUOEUzN00wV1c0UFBPUkxONFhVWVJLR0RaNCQlQCN0PWcu","responseId":3},"eventTime":"2022-03-24T10:24:04.1798293Z"}}
"Get Response Details"
{ "responder": "anonymous",
"submitDate": "3/24/2022 10:24:03 AM",
"r0a0b849bba0b4c7f9da5734602f6aefb": "João Peres",
"rf479dcadc3884bf09c7c7ad0bb15dfa9": "Serviço de Consultas",
"r31fe96bb0b4a431896902e60a110d3f2": "texttexttext"
}
I was not able to extract any more code, as the "Show raw outputs" window is blank:

All of your case statements have a line break at the end.
"Case_4": {
"case": "Serviço de Consultas\n",
"actions": {
"Send_an_email_notification_(V3)_3": {
...
}
}
}
}

Related

getting the label and description of wikidata item as well as specified properties labels and description

i am relatively new to sparql and i need to get the label/title of the wikidata item as well as other specified properties' label and description.
my code gives me only the properties' label and description but i cannot get the label/title and description of the wikidata item(qid). the label/title of the wikidata item(qid) - Building at Rua Esteves Junior, 26 and description of the wikidata item(qid) - historical site registered by State Institute of Cultural Heritage of Rio de Janeiro in the city of Rio de Janeiro, Rio de Janeiro, Brazil
my code:
SELECT ?locationDescription ?locationLabel ?streetLabel ?descriptionLabel ?countryLabel ?heritageLabel WHERE {
wd:Q69363514 wdt:P131 ?location.
wd:Q69363514 wdt:P6375 ?street.
wd:Q69363514 wdt:P973 ?description.
wd:Q69363514 wdt:P17 ?country.
wd:Q69363514 wdt:P1435 ?heritage.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
You could use VALUES:
SELECT
?item ?itemLabel ?itemDescription
?locationDescription ?locationLabel
?streetLabel
?descriptionLabel
?countryLabel
?heritageLabel
WHERE {
VALUES ?item { wd:Q69363514 }
?item wdt:P131 ?location ;
wdt:P6375 ?street ;
wdt:P973 ?description ;
wdt:P17 ?country ;
wdt:P1435 ?heritage .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
This would also allow you to query multiple items at once:
VALUES ?item { wd:Q69363514 wd:… wd:… }

Customize Ruby on rails validation errors

Hi I'm trying to customize some rails form errors. All of which work fine except for the middle one regarding lead_reason_id etc i;m guessing this is because there are no custom validators?
validates :email, presence: true, format: { with: Validators::Base::EMAIL, message: 'Este campo tiene que ser positivo' }
validates :lead_reason_id, :lead_source_id, presence: true, format: { message: 'Este campo tiene que ser positivo' }
validates :amount, presence: true, numericality: { only_integer: true, greater_than: 0, message: 'Este campo tiene que ser positivo' }
The error I'm getting is
Not sure if validating several fields with a single validates call is allowed. Also, for your format hash you need to specify :with option, which is missing. If you just want to validate presence, then format hash is not necessary:
validates :lead_reason_id, presence: { message: 'Este campo tiene que ser positivo' }
validates :lead_source_id, presence: { message: 'Este campo tiene que ser positivo' }
See more at: https://guides.rubyonrails.org/active_record_validations.html#format

Simple transaction in hyperledger-composer application yields expected a resource or concept error

I was building a simple business network where a user can create an assest and sell/buy the assest. I installed the playground locally and was working on it. I had success in being able to create a user and asset(goods) but sell transaction yields expected resource or concept error.
Model.cto file
/**
* Simple buy and sell goods business network definition.
*/
namespace org.example.biznet
enum GoodsState {
o FOR_SALE
o SOLD
}
asset Goods identified by goodsId {
o String goodsId
o GoodsState state
o String description
--> User owner
}
participant User identified by email {
o String email
o String firstName
o String lastName
o Double balance
}
transaction Buy {
--> User user
--> Goods goods
o Double productPrice
}
transaction Sell {
--> User user
--> Goods goods
o Double sellingPrice
}
logic.js file
/**
* Selling the product owned by the user
* #param {org.example.biznet.Sell} selling - the selling transaction
* #transaction
*/
function sell(sell) {
if(sell.user.email !== sell.goods.owner.email){
throw new Error('The user does not own the assest');
}
state = sell.goods.state;
state = 'SOLD';
console.log('#### he can sell the goods');
return getAssetRegistry('org.example.biznet.Goods')
.then(function(goodsRegistry) {
// save the updated goods status
return goodsRegistry.update(state);
});
}
permissions.acl file
/**
* Access control list for simple buy and sell network.
*/
rule Member {
description: "Allow all participants read access to all resources"
participant: "org.example.biznet.User"
operation: READ
resource: "org.example.biznet.*"
action: ALLOW
}
rule OwnerHasFullAccessToTheirAssets {
description: "Allow all participants full access to their assets"
participant(p): "org.example.biznet.User"
operation: ALL
resource(r): "org.example.biznet.Goods"
condition: (r.owner.getIdentifier() === p.getIdentifier())
action: ALLOW
}
rule SystemACL {
description: "System ACL to permit all access"
participant: "org.hyperledger.composer.system.Participant"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}
rule NetworkAdminUser {
description: "Grant business network administrators full access to user resources"
participant: "org.hyperledger.composer.system.NetworkAdmin"
operation: ALL
resource: "**"
action: ALLOW
}
rule NetworkAdminSystem {
description: "Grant business network administrators full access to system resources"
participant: "org.hyperledger.composer.system.NetworkAdmin"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}
So after that as I previously mentioned, I tried creating a couple of users and goods this way.
{
"$class": "org.example.biznet.User",
"email": "memberA#gmail.com",
"firstName": "Jenny",
"lastName": "Jones",
"balance": 3000
}
{
"$class": "org.example.biznet.User",
"email": "memberB#gmail.com",
"firstName": "Amy",
"lastName": "Williams",
"balance": 5000
}
{
"$class": "org.example.biznet.Goods",
"goodsId": "goodsId:1",
"state": "FOR_SALE",
"description": "car",
"owner": "resource:org.example.biznet.User#memberA#gmail.com"
}
It worked.
But when I try to sell an asset, the following way, I get this error that I can't figure out why
{
"$class": "org.example.biznet.Sell",
"sellingPrice": 5000,
"user": "resource:org.example.biznet.User#memberA#gmail.com",
"goods": "resource:org.example.biznet.Goods#goodsId:1"
}
Error: Error trying invoke business network. Error: No valid responses from any peers. Response from attempted peer comms was an error: Error: chaincode error (status: 500, message: Error: Expected a Resource or Concept.)
you have a problem in your transaction code:
/**
* Selling the product owned by the user
* #param {org.example.biznet.Sell} sell - the selling transaction
* #transaction
*/
function sell(sell) {
if(sell.user.email !== sell.goods.owner.email){
throw new Error('The user does not own the assest');
}
state = 'SOLD';
sell.goods.state = state;
console.log('#### he can sell the goods');
return getAssetRegistry('org.example.biznet.Goods')
.then(function(goodsRegistry) {
// save the updated goods status to the goods asset instance
return goodsRegistry.update(sell.goods);
});
}
your #param definition is incorrect (should be 'sell')
you need to save the state to sell.goods resource passed in to the txn as a relationship.

How to get Company info from Google?

if we search website of the company in google we get following result. i need to get company info and founded year. and no of employee how can i get. is there any API availabe?. can you help me on this. thanks
You are looking for the Google knowledge graph API. The info on the box to the right is pulled from the Google Knowledge Graph for the top result.
You can get the information you need for an Organization entity:
An organization such as a school, NGO, corporation, club, etc.
Example properties of an Organization include legalName, logo, foundingDate
For example, here is a simple query I used for Facebook:
https://kgsearch.googleapis.com/v1/entities:search?query=Facebook&key=<YOUR_API_KEY_HERE>&indent=True
And here is the result I got in return:
{
"#type": "EntitySearchResult",
"result": {
"#id": "kg:/m/0hmyfsv",
"name": "Facebook, Inc.",
"#type": [
"Corporation",
"Organization",
"Thing"
],
"description": "Social network company",
"image": {
"contentUrl": "http://t3.gstatic.com/images?q=tbn:ANd9GcTjO7_7_DBuI3EpMBdVTACYT2WDkwKGrBic0JYSGtIt1c_0oMK9",
"url": "https://commons.wikimedia.org/wiki/File:F_icon.svg"
},
"url": "https://www.facebook.com/"
},
"resultScore": 32.638672
}
BTW, for some reason, Facebook was the second on the list of results after Youtube
Update
Looks like, at the moment, the API does not provide a way to control which properties to be returned in the results, and not all properties are included in the response by default. There is a question here about how to get that done
From the API reference, the accepted request paramaters are:
query (e.g. query=Facebook)
ids (e.g. ids=/m/0hmyfsv)
languages (e.g. languages=en)
types (e.g. types=Corporation)
indent (e.g. indent=true)
limit (e.g. limit=2)
And the response parameters are: #id, name,#type, description, image detailedDescription (if available), and resultScore
The information you are looking for are actually available in the Wikipedia page included in the URL provided as part of the detailedDescription property, so you may want to consider using the Wikidata API instead
Run following query in wikidata sparql endpoint
SELECT DISTINCT
?wdindustryLabel
?wdcompanyName
?wdcountryLabel
(SAMPLE(?wdemployee) AS ?wdemployee)
(SAMPLE(?wdfounded) AS ?wdfounded)
(SAMPLE(?wdofficial_website) AS ?wdofficial_website)
WHERE {
?wdcompany wdt:P31 ?type;
rdfs:label ?wdcompanyName.
OPTIONAL {
?wdcompany wdt:P452 ?wdindustry;
wdt:P1128 ?wdemployee;
wdt:P571 ?wdfounded;
wdt:P17 ?wdcountry;
wdt:P856 ?wdofficial_website.
}
FILTER(LANGMATCHES(LANG(?wdcompanyName), "EN"))
VALUES ?type {
wd:Q6881511
wd:Q43229
}
VALUES ?wdcompanyName {
"Apple Inc."#en
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?wdcompanyName ?wdindustryLabel ?wdcountryLabel
ORDER BY (?wdcompanyName)
or use following code:
# pip install sparqlwrapper
# https://rdflib.github.io/sparqlwrapper/
import sys
from SPARQLWrapper import SPARQLWrapper, JSON
endpoint_url = "https://query.wikidata.org/sparql"
query = """SELECT DISTINCT ?wdindustryLabel ?wdcompanyName ?wdcountryLabel (SAMPLE(?wdemployee) AS ?wdemployee) (SAMPLE(?wdfounded) AS ?wdfounded) (SAMPLE(?wdofficial_website) AS ?wdofficial_website) WHERE {
?wdcompany wdt:P31 ?type;
rdfs:label ?wdcompanyName.
OPTIONAL {
?wdcompany wdt:P452 ?wdindustry;
wdt:P1128 ?wdemployee;
wdt:P571 ?wdfounded;
wdt:P17 ?wdcountry;
wdt:P856 ?wdofficial_website.
}
FILTER(LANGMATCHES(LANG(?wdcompanyName), "EN"))
VALUES ?type {
wd:Q6881511
wd:Q43229
}
VALUES ?wdcompanyName {
'Apple Inc.'#en
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?wdcompanyName ?wdindustryLabel ?wdcountryLabel
Order By ?wdcompanyName"""
def get_results(endpoint_url, query):
user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
# TODO adjust user agent; see https://w.wiki/CX6
sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query().convert()
results = get_results(endpoint_url, query)
for result in results["results"]["bindings"]:
print(result)

Google custom search API and Ruby

I wanted to write a Google search scraper/parser to pull employees from Google's index of linkedin.com. Linkedin closed their API, so I wrote a Mechanize/Nokogiri scraper first, which got me captcha'd, so I rewrote the script using the Google search API gem.
The problem is, I can't figure out where to begin to make it bring back more than the first page of results and the official docs couldn't even be described as 'sparse'.
This is the code that returns page 1 only:
require 'rubygems'
require 'google/api_client'
require 'json'
require 'pp'
puts "What organisation's employees shall we get today?"
organisation = gets.chomp
puts "Harvesting Google Search Results - This may take some time"
apikey = "1234"
cxid = "5678"
client = Google::APIClient.new(:key => apikey, :authorization => nil, :application_name => "linkedout", :application_version => "beta_0.5")
search = client.discovered_api('customsearch')
response = client.execute(
:api_method => search.cse.list,
:parameters => {
'q' => 'current ' + organisation + ' site:linkedin.com',
'maxResults' => 100,
'key' => apikey,
'cx' => cxid
}
)
status, headers, body = response
jsonresponse = response.body
employees = []
#tags = JSON.parse(jsonresponse)['items']
#tags.each do |tag|
x = tag['title']
x.gsub!(/ \| LinkedIn/, "")
x.downcase!
x.gsub!(/ profiles/, "")
employees << x
end
employees = employees.uniq
puts employees
Any help would be very gratefully received - I'm still learning this stuff.
Edit:
Here is a snippet of the JSON google's API returns:
"items": [
{
"kind": "customsearch#result",
"title": "Tina Minor - Recruiter, The Walt Disney Company | LinkedIn",
"htmlTitle": "Tina Minor - Recruiter, The \u003cb\u003eWalt Disney\u003c/b\u003e Company | LinkedIn",
"link": "https://www.linkedin.com/pub/tina-minor-recruiter-the-walt-disney- company/5/849/5a6",
"displayLink": "www.linkedin.com",
"snippet": "View Tina Minor - Recruiter, The Walt Disney Company's professional profile on \n... Current. The Walt Disney Company. Previous. True Religion Brand Jeans, ...",
"htmlSnippet": "View Tina Minor - Recruiter, The \u003cb\u003eWalt Disney\u003c/b\u003e Company's professional profile on \u003cbr\u003e\n... \u003cb\u003eCurrent\u003c/b\u003e. The \u003cb\u003eWalt Disney\u003c/b\u003e Company. Previous. True Religion Brand Jeans, ...",
"formattedUrl": "https://www.linkedin.com/pub/tina-minor-recruiter-the- walt- disney.../5a6",
"htmlFormattedUrl": "https://www.linkedin.com/pub/tina-minor-recruiter- the- \u003cb\u003ewalt\u003c/b\u003e- \u003cb\u003edisney\u003c/b\u003e.../5a6",
"pagemap": {
"cse_image": [
{
"src": "https://media.licdn.com/mpr/mpr/shrink_200_200/p/8/005/09b/3f2/1eb6f83.jpg"
}
],
"person": [
{
"location": "Greater Los Angeles Area",
"role": "Recruiter, Talent Acquisition at The Walt Disney Company"
}
],
"cse_thumbnail": [
{
"width": "160",
"height": "160",
"src": "https://encrypted-tbn1.gstatic.com/images? q=tbn:ANd9GcTbmlbDVBOMKTtOA_D88aFaPuZ9MjABABwumzBPk0F2x2P2-0puaIRlktce"
}
],
"metatags": [
{
"globaltrackingurl": "//www.linkedin.com/mob/tracking",
"globaltrackingappname": "profile",
"globaltrackingappid": "webTracking",
"lnkd-track-json-lib": "https://static.licdn.com/scds/concat/common/js? h=2jds9coeh4w78ed9wblscv68v-ebbt2vixcc5qz0otts5io08xv&fc=2",
"treeid": "SnQhTqcr1RNgnKS8RSsAAA==",
"appname": "profile",
"pageimpressionid": "29ca4803-0233-4934-955a-1959a37dfbbf",
"pagekey": "nprofile_v2_public_fs",
"analyticsurl": "/analytics/noauthtracker",
"msapplication-tileimage": "https://static.licdn.com/scds/common/u/images/logos/linkedin/logo-in-win8-tile- 144_v1.png",
"msapplication-tilecolor": "#0077B5",
"application-name": "LinkedIn",
"remote-nav-init-marker": "true"
}
],
"hcard": [
{
"fn": "Tina Minor - Recruiter, The Walt Disney Company",
"title": "Recruiter, Talent Acquisition at The Walt Disney Company"
}
]
}
}
...
According to Search request metadata, there should be a nextPage value returned next to items when there are additional results. However it always says Note: This API returns up to the first 100 results only. so it looks like you are already getting the maximum number of results.

Resources