Giter Club home page Giter Club logo

biuc's Introduction

biuc

Bing Image Update Cache

biuc is a cache and service to by http get ask for images from Bing Cognitiv Image Search service.

For more information on Bing Image Search and how to run it in Azure, please visit:

[https://azure.microsoft.com/en-us/services/cognitive-services/bing-image-search-api/]

And I should also mark that this is a project that have not put security at first. It's quite relaxed pice of code.

settings.php

In settings.php is used to store db settings and password/salt for hash to use when asking for data. Also your bing azure key to use to for bing image search.

stats.php

Used this ask for db stats. You will recive a json string as response.

# curl http://host.to.your.service/biuc/stats.php?h=YOUR_ADMIN_PASSOWORD_FROM_SETTINGS
{"total_count":[{"count(*)":"41377"}],"album_count":[{"count(*)":"37753"}],"artist_count":[{"count(*)":"3624"}],"local_count_false":[{"count(*)":"22"}],"local_count_true":[{"count(*)":"41355"}]}

storelocal.php

This script will check the db for new post and download any new found image url's to the local cache.

# curl http://host.to.your.service/biuc/storelocal.php?h=YOUR_ADMIN_PASSOWORD_FROM_SETTINGS

biuc.php

The main script to be used by clients to ask for images that can be found either by a bing search or in the local cache. You will have to create a hash from the search string and the user password as arguments on enquiring the biuc service.

The hash to send as argument "h" is a Md5, simply generated by adding your query_string + YOUR_USER_PASSWORD_IN_SETTINGS and convert this to binary and calc a md5 hash on this and make it a hex string.

Pseudo code to generate a md5 hash.

binary_string = to_binary_string(query + user_password_string)
md5_hash = generate_md5_hash(binary_string)
hash = to_hex_string(md5_hash)

send the hash as "h" in HTTP GET argument.

Below is a usage example in C#.

private static async Task<string> GetBiucArtworkURLAsync(string query)
{
    var client = new HttpClient();

    var key = "YOUR_USER_PASSWORD_IN_SETTINGS";

    var queryHash = CryptographicBuffer.EncodeToHexString(
        HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5).HashData(
            CryptographicBuffer.ConvertStringToBinary(query + key, BinaryStringEncoding.Utf8)));

    // Request parameters
    string encodedQuery = string.Empty;

    using (var content = new FormUrlEncodedContent(new KeyValuePair<string, string>[]{
        new KeyValuePair<string, string>("q", query),
        new KeyValuePair<string, string>("h", queryHash),
    }))
    {
        encodedQuery = content.ReadAsStringAsync().Result;
    }

    var uri = "http://host.to.your.service/biuc/?" + encodedQuery;

    Debug.WriteLine(uri);

    HttpResponseMessage response = null;
    var numberOfGetAttemps = 3;

    while (response == null && numberOfGetAttemps > 0)
    {
        try
        {
            response = await client.GetAsync(uri);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            numberOfGetAttemps--;
        }
    }

    if (response != null)
    {
        var resp = await response.Content.ReadAsStringAsync();

        Debug.WriteLine(resp);

        var imageJson = new JsonObject();
        if (JsonObject.TryParse(resp, out imageJson)
            && imageJson.ContainsKey("url") && imageJson["url"].ValueType == JsonValueType.String)
        {
            return imageJson["url"].GetString();
        }
    }

    return null;
}

biuc's People

Contributors

opengd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.