Skip to content

Instantly share code, notes, and snippets.

@margusmartsepp
Created August 10, 2016 21:15
Show Gist options
  • Save margusmartsepp/4e5dbd205d3adf15437ef54f7619b67e to your computer and use it in GitHub Desktop.
Save margusmartsepp/4e5dbd205d3adf15437ef54f7619b67e to your computer and use it in GitHub Desktop.
Yle search
using System;
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Text;
using UnityEngine.UI;
using Object = UnityEngine.Object;
public class SearchBehaviour : MonoBehaviour
{
private Object thisLock = new Object();
public Slider slider;
public Scrollbar scrollbar;
public GameObject listParent;
public GameObject listItem;
public YleManager yle;
public InputField query;
private int amount = 10;
private int position;
void Start()
{
yle = new YleManager();
slider.onValueChanged.AddListener(o => PagingChanged());
scrollbar.onValueChanged.AddListener(o => ScrollChanged());
}
private void ScrollChanged()
{
if (Math.Abs(scrollbar.value) < 0.0001)
{
SearchYle(position + amount, amount, true);
}
}
private void PagingChanged()
{
SearchYle((int)slider.value * 10);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
{
SearchYle();
}
}
public void SearchYle(int skip = 0, int take = 10, bool append = false)
{
lock (thisLock)
{
position = skip;
if (!append) RemoveExistingItems();
var uri = yle.GetYleUri(query.text, skip, take);
Debug.Log(uri);
var www = new WWW(uri);
StartCoroutine(WaitForRequest(www, ParseRequest));
}
}
private IEnumerator WaitForRequest(WWW www, Action<string> action)
{
yield return www;
if (www.error == null)
action(www.text);
else
Debug.Log("WWW Error: " + www.error);
}
private void ParseRequest(string json)
{
var root = JsonUtility.FromJson<Serializables.Rootobject>(json);
var sb = new StringBuilder();
var skip = int.Parse(root.meta.offset);
slider.maxValue = root.meta.count == 0 ? 0 : (root.meta.count - 1) / 10;
foreach (var titleFi in root.data.Select(o => o.title.fi))
{
var go = Instantiate(listItem);
go.transform.SetParent(listParent.transform);
go.transform.Find("ElementTitle").GetComponent<Text>().text = titleFi;
sb.AppendLine(string.Format("{0})\t{1}", ++skip, titleFi));
}
if (root.meta.count > amount && root.data.Length == amount && Math.Abs(scrollbar.size) > 0.9999)
{
SearchYle(position + amount, amount, true);
}
Debug.Log(sb.ToString());
}
private void RemoveExistingItems()
{
while (listParent.transform.childCount > 0)
{
var c = listParent.transform.GetChild(0);
c.SetParent(null);
Destroy(c.gameObject);
}
}
}
using UnityEngine;
using System.Collections;
public static class Serializables
{
[System.Serializable]
public class Rootobject
{
public string apiVersion;
public Meta meta;
public Datum[] data;
}
[System.Serializable]
public class Meta
{
public string offset;
public string limit;
public int count;
public int program;
public int clip;
public string q;
}
[System.Serializable]
public class Datum
{
public Title title;
}
[System.Serializable]
public class Title
{
public string fi;
public string sv;
public string en;
}
}
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class YleManager
{
private readonly string _appServer;
private readonly string _appId;
private readonly string _appKey;
public YleManager(
string appId = "d055de8a",
string appKey = "6f4b3d0a16c6b47d61709b3a280ece34",
string appServer = "https://external.api.yle.fi/v1/programs/items.json?")
{
_appServer = appServer;
_appId = appId;
_appKey = appKey;
}
public string GetYleUri(string query, int skip = 0, int take = 10)
{
var parameters = new Dictionary<string, string>
{
{ "q", WWW.EscapeURL(query)},
{ "offset", skip.ToString()},
{ "limit", take.ToString()},
{ "app_id", _appId},
{ "app_key", _appKey}
}
.Select(o => string.Format("{0}={1}", o.Key, o.Value))
.ToArray();
return _appServer + string.Join("&", parameters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment