Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Twitter is famous for its <a href="http://en.wikipedia.org/wiki/File:Failwhale.png" rel="nofollow noreferrer">Fail Whale</a>, and the <a href="http://apiwiki.twitter.com/" rel="nofollow noreferrer">Twitter API</a> gets a failing grade too; it's horrible.</p> <p>Twitter <a href="http://apiwiki.twitter.com/Twitter-Search-API-Method:-trends-current" rel="nofollow noreferrer">trends current Search API method</a> response can be expressed (using just two trends to simplify the examples) in canonical, normalized JSON form as:</p> <pre><code>{ "as_of":1268069036, "trends":[ {"name":"Happy Women's Day","query":"\"Happy Women's Day\" OR \"Women's Day\""}, {"name":"#MusicMonday","query":"#MusicMonday"},{"name":"#MM","query":"#MM"} ] } </code></pre> <p>The <code>as_of</code> date is in Unix time, the number of seconds since 1/1/1970.</p> <p>In Go, this can be described by:</p> <pre><code>type Trend struct { Name string Query string } type Current struct { As_of int64 Trends []Trend } </code></pre> <p>Twitter mangles the canonical, normalized JSON form to become:</p> <pre><code>{ "as_of":1268069036, "trends":{ "2010-03-08 17:23:56":[ {"name":"Happy Women's Day","query":"\"Happy Women's Day\" OR \"Women's Day\""}, {"name":"#MusicMonday","query":"#MusicMonday"} ] } } </code></pre> <p>Sometimes, Twitter returns this equivalent JSON form.</p> <pre><code>{ "trends":{ "2010-03-08 17:23:56":[ {"name":"Happy Women's Day","query":"\"Happy Women's Day\" OR \"Women's Day\""}, {"name":"#MusicMonday","query":"#MusicMonday"} ] }, "as_of":1268069036 } </code></pre> <p><code>"2010-03-08 17:23:56":</code> is a JSON object name. However, it's -- nonsensically -- a string form of <code>as_of</code>.</p> <p>If we replace <code>"2010-03-08 17:23:56":</code> by the object name <code>"ntrends":</code> (for nested trends), overwriting the redundant <code>as_of</code> string time, we have the following revised Twitter JSON form:</p> <pre><code>{ "as_of":1268069036, "trends":{ "ntrends":[ {"name":"Happy Women's Day","query":"\"Happy Women's Day\" OR \"Women's Day\""}, {"name":"#MusicMonday","query":"#MusicMonday"} ] } } </code></pre> <p>It's easy to scan the Twitter JSON form for <code>"as_of":</code>, read the following number as the <code>as_of</code> Unix time, and convert it to JSON name form e.g.:</p> <pre><code>var aoUnixTime int64 // insert code to find and convert as_of Unix time to aoUnixTime aoName := time.SecondsToUTC(aoUnix).Format(`"2006-01-02 15:04:05":`) </code></pre> <p>Now we can scan the Twitter JSON form for the <code>aoName</code> value and replace it with <code>"ntrends":</code> to get the revised Twitter JSON form.</p> <p>In Go, the revised Twitter JSON form can be described by:</p> <pre><code>type Trend struct { Name string Query string } type NTrends struct { NTrends []Trend } type Current struct { As_of int64 Trends NTrends } </code></pre> <p>Note: the first character of the struct and field identifiers are uppercase so that they can be exported.</p> <p>I've programmed and tested this approach and it seems to work. Since this is a school project for you, I haven't published my code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload