1+ /*
2+ Fetch data from meetup.com API
3+ pythonmilano.xyz | @cstrap
4+ Credits: http://stackoverflow.com/a/1176810/1430290
5+ */
6+
7+ var query = "select json.name, json.time, yes_rsvp_count, json.link from json where url=" ;
8+ var query_current = query + "'https://api.meetup.com/Python-Milano/events/'" ;
9+ var query_past = query + "'https://api.meetup.com/Python-Milano/events/?status=past'" ;
10+
11+ function YQLQuery ( query , callback ) {
12+ this . query = query ;
13+ this . callback = callback || function ( ) { } ;
14+ this . fetch = function ( ) {
15+ if ( ! this . query || ! this . callback ) {
16+ throw new Error ( 'YQLQuery.fetch(): Parameters may be undefined' ) ;
17+ }
18+ var scriptEl = document . createElement ( 'script' ) ,
19+ uid = 'yql' + + new Date ( ) ,
20+ encodedQuery = encodeURIComponent ( this . query . toLowerCase ( ) ) ,
21+ instance = this ;
22+ YQLQuery [ uid ] = function ( json ) {
23+ instance . callback ( json ) ;
24+ delete YQLQuery [ uid ] ;
25+ document . body . removeChild ( scriptEl ) ;
26+ } ;
27+ scriptEl . src = 'http://query.yahooapis.com/v1/public/yql?q=' + encodedQuery + '&format=json&callback=YQLQuery.' + uid ;
28+ document . body . appendChild ( scriptEl ) ;
29+ } ;
30+ }
31+
32+ function get_event ( payload ) {
33+ if ( ! payload . query . results ) {
34+ var pastEvents = new YQLQuery ( query_past , get_event ) ;
35+ pastEvents . fetch ( ) ;
36+ return ;
37+ }
38+ var obj = payload . query . results . json . slice ( - 1 ) [ 0 ] . json ;
39+ var date = new Date ( parseInt ( obj . time ) ) ;
40+ var month = [ 'GENNAIO' , 'FEBBRAIO' , 'APRILE' , 'MAGGIO' , 'GIUGNO' , 'LUGLIO' , 'AGOSTO' , 'SETTEMBRE' , 'OTTOBRE' , 'NOVEMBRE' , 'DICEMBRE' ]
41+ result = {
42+ 'topic' : obj . name ,
43+ 'day' : date . getDate ( ) ,
44+ 'month' : month [ date . getMonth ( ) ] ,
45+ 'link' : obj . link ,
46+ }
47+ $ ( '#topic' ) . text ( result . topic ) ;
48+ $ ( '#day' ) . text ( result . day ) ;
49+ $ ( '#month' ) . text ( result . month ) ;
50+ $ ( '#meetup_link' ) . attr ( 'href' , result . link ) ;
51+ } ;
52+
53+ var currentEvent = new YQLQuery ( query_current , get_event ) ;
54+ currentEvent . fetch ( ) ;
0 commit comments