Is there a way to query Wikidata for company details from official web page? - wikidata

I would like to query Wikidata for a company based on its domain url.
For instance if I query facebook.com, it should return Facebook Inc.
I have the below query which works fine but doesn't return the Ticker symbol.
SELECT distinct ?item ?itemLabel ?stock_exchange ?stock_exchangeLabel ?ticker_symbol
WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?item (wdt:P31/wdt:P279*) wd:Q783794.
{
{ ?item wdt:P856 <https://www.google.com.co> }
UNION { ?item wdt:P856 <http://www.google.com.co> }
}
OPTIONAL { ?item wdt:P414 ?stock_exchange. }
OPTIONAL { ?item wdt:P249 ?ticker_symbol. }
}
GROUP BY ?item ?stock_exchange ?itemLabel stock_exchangeLabel ?ticker_symbol

Related

Validate the API key and the city name by the API request

I created a custom module where in the block I display the weather using data from https://openweathermap.org/
Code of this block:
https://phpsandbox.io/n/sweet-forest-1lew-1wmof
Also I have WeatherForm.php file with the form which adds in a configuration city and an API key for which it is necessary to display weather.
I needed to Add form validation:
fields should not be empty
City name should not contain numbers
I did it this way:
public function validateForm(array &$form, FormStateInterface $form_state) {
$pattern = '/[0-9]/';
if (empty($form_state->getValue('weather_city'))) {
$form_state->setErrorByName('weather_city', $this->t('Fields should not be empty'));
}
if (preg_match($pattern, $form_state->getValue('weather_city'))) {
$form_state->setErrorByName('weather_city', $this->t('City name should not contain numbers'));
}
}
But I got these remark after the code review:
Also, will be good to validate the API key and the city name by the API request.
I found an example of how to implement this:
public function validateWeatherData(string $city_name, $api_key):bool {
try {
$url = "https://api.openweather.org/data/2.5/weather?q=$city_name&appid=$api_key";
$response = $this->client->request('GET', $url);
if ($response->getStatusCode() != 200) {
throw new \Exception('Failed to retrieve data.');
}
$reg_ex = "#^[A-Za-z-]=$#";
return preg_match($reg_ex, $city_name);
}
catch (GuzzleException $e) {
return FALSE;
}
}
But I don't know how to integrate the example code into my function validateForm. What my code should look like so that it also implements validate the API key and the city name by the API request?
All code of my Form:
https://phpsandbox.io/n/spring-mountain-gdnn-emozx
Why not use both, brainstorm with me something along the lines of..
function validateWeatherData($city, $apikey) {
try {
$url = "https://api.openweather.org/data/2.5/weather?q=$city_name&appid=$api_key"; // Build the URL
$response = file_get_contents($url); // You can use cURL here as well incase CORS blocks file_get_contents
return $response; // Return the data from the call made above
}
catch (Exception $e) {
return $e;
}
}
function validateForm(array &$form, FormStateInterface $form_state) {
$pattern = '/[0-9]/';
if (empty($form_state->getValue('weather_city'))) {
$form_state->setErrorByName('weather_city', $this->t('Fields should not be empty'));
return false; // Failed to validate city, return false go back start again
}
if (preg_match($pattern, $form_state->getValue('weather_city'))) {
$form_state->setErrorByName('weather_city', $this->t('City name should not contain numbers'));
return false; // Failed to validate city, return false go back start again
}
$apikey = "ABCDEFG"; // API key (can be conditional based on city via CASE/IF when needed)
$weatherdata = validateWeatherData($form_state->getValue('weather_city'), $apikey); // Validate weather data
return $weatherdata; // Return validateWeatherData's response or do something else with it
}

Obtaining next set of results

I've searched around but cannot seem to find the answer to this, I need to access more pages worth of results from a places search. I noticed in the JSON response there is a next field with a URL that, when clicked in the browser, calls a further 20 results. However I haven't been able to access this field in my C# application. Below is the JSON response (text format).
{
results: {next:https://places.cit.api.here.com/places/v1/discover/search;context=Zmxv...
items: [
{ The Botanist }
{ Alexanders Jazz Theatre Bar }
{ The Architect }
{ 1539 Restaurant & Bar }
{ Barlounge Chester }
{ Meze }
{ Hanky Panky Pancakes }
{ The Slowboat }
{ The Moorings }
{ Missoula }
{ Istanbul BBQ }
{ Chip-O-Dee }
{ The Flower Cup }
{ Mama K's Burritos }
{ The Stage Door Cafe Chester }
{ Cinderbox Coffee }
{ Wok&Go }
{ Covino }
{ Urbano 32 }
{ Beatons Tearooms }
]
}
search: {
context: { urn:nlp-types:place }
supportsPanning:true
ranking:category-recommendations
}
}
And my C# classes used to access the fields, the next string just returns null:
[System.Serializable]
public class Response
{
public results results;
}
[System.Serializable]
public class results
{
public string next;
public string previous;
public items[] items;
}
Thank you
Places API is built from a consumer perspective and restricted only to return the first 100(relevant) POIs around a location. There is no provision to get all the POIs as of today.

Find out field change after update in Symfony 2.x

I want to perform an action when a field changes from null to any value.
cat /tmp/tag.log will only show preupdate. It continues to run without stopping.
This is what I have done.
public function preUpdate(PreUpdateEventArgs $args)
{
if ($args->getEntity() instanceof Contact) {
file_put_contents('/tmp/tag.log', 'preUpdate' .PHP_EOL, FILE_APPEND);
if ($args->hasChangedField('assignedTo')){
file_put_contents('/tmp/tag.log', 'preUpdate changed' .PHP_EOL, FILE_APPEND);
}
if ( $args->getOldValue('assignedTo') == null ){
file_put_contents('/tmp/tag.log', 'preUpdate assignedTo is null ' .PHP_EOL, FILE_APPEND);
}
if ( $args->getNewValue('assignedTo') != null) {
file_put_contents('/tmp/tag.log', 'preUpdate changed to :'.$args->getNewValue('assignedTo') . PHP_EOL, FILE_APPEND);
}
}
}
I have registered this service with
tags:
- { name: doctrine.event_listener, event: preUpdate }
The problem is with the field name. The correct field name is user. I use this print_r(array_keys($args->getEntityChangeSet())) to find out the correct field name.

symfony 2 doctrine $query->getArrayResult() how to remove selected key->values from result

As I don't want id values from a select with createQuery, but the select command doesn't allow omitting id (primary key) from the actual query (using "partial") I need to remove the id's from the result from getArrayResult()
I made this small recursive key remover static class:
class arrayTool
{
public static function cleanup($array, $deleteKeys)
{
foreach($array as $key => $value )
{
if(is_array( $value))
{
$array[$key] = self::cleanup($array[$key], $deleteKeys);
} else {
if (in_array($key, $deleteKeys)) unset($array[$key]);
}
}
return $array;
}
}
Which is called by an array containing one or more keys to be removed from the result, of any array depth:
$array = arrayTool::cleanup($array, array('id', 'id2'));

How to retrieve vista's network status (e.g. "Local Only", "Local and Internet") in powershell

I have a flaky NIC that drops out from time to time, especially after resuming from hibernation. A drop-out corresponds to Vista's network status showing in the notification area as "Local Only". Is there a way of retrieving these status values (e.g. "Limited Connectivity", "Local Only", "Local and Internet") programmatically?
I am writing a powershell script that polls to see if the connection is down, and if so, resets the adapter. Currently I am trying to detect the connection state by pinging my ISP's DNS server. However, since the OS is already correctly identifying this condition, it would be much simpler if I could just retrieve this value.
Thanks!
Try this function:
PS> function Get-NetworkStatus {
$t = [Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")
$networkListManager = [Activator]::CreateInstance($t)
$connections = $networkListManager.GetNetworkConnections()
function getconnectivity {
param($network)
switch ($network.GetConnectivity()) {
0x0000 { 'disconnected' }
{ $_ -band 0x0001 } { 'IPV4_NOTRAFFIC' }
{ $_ -band 0x0002 } { 'IPV6_NOTRAFFIC' }
{ $_ -band 0x0010 } { 'IPV4_SUBNET' }
{ $_ -band 0x0020 } { 'IPV4_LOCALNETWORK' }
{ $_ -band 0x0040 } { 'IPV4_INTERNET' }
{ $_ -band 0x0100 } { 'IPV6_SUBNET' }
{ $_ -band 0x0200 } { 'IPV6_LOCALNETWORK' }
{ $_ -band 0x0400 } { 'IPV6_INTERNET' }
}
}
$connections |
% {
$n = $_.GetNetwork();
$name = $n.GetName();
$category = switch($n.GetCategory()) { 0 { 'public' } 1 { 'private' } 2 { 'domain' } }
$connectivity = getConnectivity $n
new-object PsObject -property #{Name=$name; Category=$category; Connectivity=$connectivity }
}
}
PS> Get-NetworkStatus
Name Connectivity Category
---- ------------ --------
Neznámá síť {IPV4_NOTRAFFIC, IPV6_NOTRAFFIC} public
stefan {IPV6_NOTRAFFIC, IPV4_INTERNET} private
If you pipe $connections and output from GetNetwork() to Get-Member you will find some more useful methods.

Resources