Skip to content

Commit

Permalink
feat: upm package
Browse files Browse the repository at this point in the history
  • Loading branch information
bilal-arikan committed Jan 29, 2022
1 parent af3da2c commit d148fd2
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 91 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,47 @@ Unity3d wrapper for duckduckgo-images-api

### Screenshot

![Alt text](/SS.gif?raw=true "Example Gif")
![Alt text](SS~/SS.gif?raw=true "Example Gif")

How to Import:
- Add to manifest.json
```
"com.arikan.duckduckgo.images" : "https://github.com/bilal-arikan/duckduckgo-images-api-unity.git",
```

How to Use:

```C#
DuckDuckGo.Search(
"banana", // search input
SafeSearch.Off, // safesearch
0, // page
"us-en", // locations
OnSearchCallback // callback
);

void OnSearchCallback(ImageSearchResult result)
{
if (result == null)
{
Debug.LogError("Result Null", this);
return;
}

Debug.Log($"Result Count: " + result.results.Count, this);
Debug.Log("First Result:\n" + JsonUtility.ToJson(result.results[0], true), this);
foreach (var item in result.results.Take(ShowMaxResultAmount))
{
var request = UnityWebRequestTexture.GetTexture(item.thumbnail).SendWebRequest();
request.completed +=
(ao) =>
{
if (ao.isDone)
{
RawImage image = Instantiate(ResultImagePrefab, ResultsLayout.transform);
image.texture = DownloadHandlerTexture.GetContent(request.webRequest);
}
};
}
}
```
156 changes: 81 additions & 75 deletions Runtime/Example/ApiExample.cs
Original file line number Diff line number Diff line change
@@ -1,98 +1,104 @@
using System.Linq;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using Arikan.Duckduckgo.Api;
using UnityEngine.UI;

public class ApiExample : MonoBehaviour
namespace Arikan.Example
{
public int ShowMaxResultAmount = 15;
public string location = "us-en";
[Space]
public InputField SearchInput;
public Button SearchButton;
public LayoutGroup ResultsLayout;
public RawImage ResultImagePrefab;
[Header("Paging")]
public Button prevButton;
public Button nextButton;
public Text pageNo;
[Space]
public int lastPageNo;
using Arikan.Models;

private List<RawImage> resultImages = new List<RawImage>();

private void Start()
public class ApiExample : MonoBehaviour
{
SearchButton.onClick.AddListener(SendExample);
nextButton.onClick.AddListener(NextPage);
prevButton.onClick.AddListener(PreviousPage);
}
public int ShowMaxResultAmount = 15;
public string location = "us-en";
[Space]
public InputField SearchInput;
public Button SearchButton;
public LayoutGroup ResultsLayout;
public RawImage ResultImagePrefab;
[Header("Paging")]
public Button prevButton;
public Button nextButton;
public Text pageNo;
[Space]
public int lastPageNo;

void SendExample()
{
foreach (Transform child in ResultsLayout.transform)
Destroy(child.gameObject);
Resources.UnloadUnusedAssets();
private List<RawImage> resultImages = new List<RawImage>();

Debug.Log($"Searching : {SearchInput.text}", this);
private void Start()
{
SearchButton.onClick.AddListener(SendExample);
nextButton.onClick.AddListener(NextPage);
prevButton.onClick.AddListener(PreviousPage);
}

SearchButton.interactable = false;
lastPageNo = 1;
ClearResults();
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}
void SendExample()
{
foreach (Transform child in ResultsLayout.transform)
Destroy(child.gameObject);
Resources.UnloadUnusedAssets();

void NextPage()
{
lastPageNo++;
ClearResults();
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}
void PreviousPage()
{
lastPageNo--;
ClearResults();
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}
Debug.Log($"Searching : {SearchInput.text}", this);

void ClearResults(){
foreach (var img in resultImages)
{
Destroy(img.gameObject);
SearchButton.interactable = false;
lastPageNo = 1;
ClearResults();
DuckDuckGo.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}
resultImages.Clear();
}

void OnSearchCallback(ImageSearchResult result)
{
SearchButton.interactable = true;
prevButton.interactable = lastPageNo > 1;
pageNo.text = lastPageNo.ToString("00");
void NextPage()
{
lastPageNo++;
ClearResults();
DuckDuckGo.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}
void PreviousPage()
{
lastPageNo--;
ClearResults();
DuckDuckGo.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}

if (result == null)
void ClearResults()
{
Debug.LogError("Result Null", this);
return;
foreach (var img in resultImages)
{
Destroy(img.gameObject);
}
resultImages.Clear();
}

Debug.Log($"Result Count: " + result.results.Count, this);
Debug.Log("First Result:\n" + JsonUtility.ToJson(result.results[0], true), this);
foreach (var item in result.results.Take(ShowMaxResultAmount))
void OnSearchCallback(ImageSearchResult result)
{
var request = UnityWebRequestTexture.GetTexture(item.thumbnail).SendWebRequest();
request.completed +=
(ao) =>
SearchButton.interactable = true;
prevButton.interactable = lastPageNo > 1;
pageNo.text = lastPageNo.ToString("00");

if (result == null)
{
if (ao.isDone)
Debug.LogError("Result Null", this);
return;
}

Debug.Log($"Result Count: " + result.results.Count, this);
Debug.Log("First Result:\n" + JsonUtility.ToJson(result.results[0], true), this);
foreach (var item in result.results.Take(ShowMaxResultAmount))
{
var request = UnityWebRequestTexture.GetTexture(item.thumbnail).SendWebRequest();
request.completed +=
(ao) =>
{
var image = Instantiate(ResultImagePrefab, ResultsLayout.transform);
image.texture = DownloadHandlerTexture.GetContent(request.webRequest);
resultImages.Add(image);
}
};
if (ao.isDone)
{
var image = Instantiate(ResultImagePrefab, ResultsLayout.transform);
image.texture = DownloadHandlerTexture.GetContent(request.webRequest);
resultImages.Add(image);
}
};
}
}
}
}

}
2 changes: 1 addition & 1 deletion Runtime/Example/SampleScene.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions Runtime/Scripts/ImagesApi.cs → Runtime/Scripts/DuckDuckGo.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.Net;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Networking;

namespace Arikan.Duckduckgo.Api
namespace Arikan
{
using Arikan.Models;

public enum SafeSearch
{
// Strict = 1, // Not Available yet
Moderate = -1,
Off = -2
}

public class ImagesApi : MonoBehaviour
public class DuckDuckGo : MonoBehaviour
{
private static ImagesApi instance;
private static DuckDuckGo instance;

private const string url = "https://duckduckgo.com/";
private KeyValuePair<string, string> lastSearch; // keyword : token
Expand All @@ -40,9 +41,9 @@ public static void Search(string text, SafeSearch safeSearch, int pageNo, string
{
if (!instance)
{
instance = new GameObject("DuckDuckGoAPI").AddComponent<Arikan.Duckduckgo.Api.ImagesApi>();
instance = new GameObject("DuckDuckGoAPI").AddComponent<Arikan.DuckDuckGo>();
}
if(string.IsNullOrWhiteSpace(location))
if (string.IsNullOrWhiteSpace(location))
{
location = "us-en";
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Runtime/Scripts/Models/ImageSearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

namespace Arikan.Duckduckgo.Api
namespace Arikan.Models
{
[Serializable]
public class ImageSearchResult
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Models/ImageSummaryModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using UnityEngine;

namespace Arikan.Duckduckgo.Api
namespace Arikan.Models
{
[Serializable]
public class ImageSummary
Expand Down
File renamed without changes
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "com.arikan.duckduckgo.api.images",
"name": "com.arikan.duckduckgo.images",
"displayName": "DuckDuckGo Images API",
"version": "1.0.0",
"unity": "2019.1",
"unity": "2018.1",
"author": "Bilal Arikan",
"description": "DuckDuckGo Images API Codes",
"description": "DuckDuckGo Images API",
"repository": {
"type": "git",
"url": "https://github.com/bilal-arikan/duckduckgo-images-api-unity"
Expand Down

0 comments on commit d148fd2

Please sign in to comment.