~santi698/gazette/restructure

« back to all changes in this revision

Viewing changes to src/Services/Weather.vala

  • Committer: eduardgotwig at googlemail
  • Date: 2013-04-17 13:28:00 UTC
  • mto: This revision was merged to the branch mainline in revision 31.
  • Revision ID: eduardgotwig@googlemail.com-20130417132800-d9c3f1d42dapgjhg
some experimental weather infos

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
        string text;
6
6
        string temp;
7
7
 
 
8
        string current_label;
 
9
 
 
10
        string current_content;
 
11
        string for_content;
 
12
 
 
13
public string[] get_attributes (string data, string tagname, string [] attrs, int offset = 0)
 
14
{
 
15
        var start = data.index_of ("<" + tagname, offset) + tagname.length + 1;
 
16
        var end = data.index_of ("/>", start);
 
17
        var tmp_data = data.substring (start, end - start);
 
18
 
 
19
        var res = new string[attrs.length];
 
20
        for (var i = 0; i < attrs.length; i++) {
 
21
                res[i] = get_attribute_value (tmp_data, attrs[i]);
 
22
        }
 
23
        return res;
 
24
}
 
25
 
 
26
public string get_attribute_value (string data, string attr)
 
27
{
 
28
        var start = data.index_of (attr + "=\"") + attr.length + 2;
 
29
        var end = data.index_of ("\"", start);
 
30
 
 
31
        return data.substring (start, end - start);
 
32
}
 
33
 
 
34
        ShadowedLabel cur;
 
35
        ShadowedLabel for1;
 
36
        ShadowedLabel for2;
 
37
 
 
38
        const string [] condition_codes = {
 
39
        "c", // tornado
 
40
        "0", // tropical storm
 
41
        "c", // hurricane
 
42
        "1", // severe thunderstorms
 
43
        "1", // thunderstorms
 
44
        "2", // mixed rain and snow
 
45
        "3", // mixed rain and sleet
 
46
        "2", // mixed snow and sleet
 
47
        "e", // freezing drizzle
 
48
        "3", // drizzle
 
49
        "7", // freezing rain
 
50
        "3", // showers
 
51
        "3", // showers
 
52
        "6", // snow flurries
 
53
        "G", // light snow showers G
 
54
        "#", // blowing snow #
 
55
        "h", // snow "
 
56
        "7", // hail
 
57
        "7", // sleet 
 
58
        "f", // dust
 
59
        "f", // foggy 
 
60
        "f", // haze
 
61
        "M", // smoky M
 
62
        "c", // blustery 
 
63
        "S", // windy S
 
64
        "h", // cold
 
65
        "N", // cloudy N
 
66
        "4", // mostly cloudy (night) 4
 
67
        "3", // mostly cloudy (day) 3
 
68
        "K", // partly cloudy (night) K
 
69
        "J", // partly cloudy (day) J
 
70
        "C", // clear (night) C
 
71
        "B", // sunny B
 
72
        "2", // fair (night) 2
 
73
        "B", // fair (day)  B
 
74
        "X", // mixed rain and hail X
 
75
        "1", // hot 1
 
76
        "1", // isolated thunderstorms
 
77
        "1", // scattered thunderstorms
 
78
        "1", // scattered thunderstorms
 
79
        "e", // scattered showers
 
80
        "#", // heavy snow #
 
81
        "2", // scattered snow showers
 
82
        "#", // heavy snow #
 
83
        "H", // partly cloudy H
 
84
        "1", // thundershowers
 
85
        "2", // snow showers
 
86
        "1", // isolated thundershowers
 
87
        ")" // not available )
 
88
};
 
89
 
8
90
        public Weather ()
9
91
        {
10
92
                var settings = new Settings ("org.pantheon.gazette.weather");
22
104
        url += "?u=" + unit;
23
105
        
24
106
        url += "&w=" + id;
25
 
 
26
 
                var session = new Soup.SessionSync ();
 
107
                
 
108
                var session = new Soup.SessionAsync ();
27
109
                var message = new Soup.Message ("GET", url);
28
110
 
29
 
                session.send_message (message);
30
 
 
31
 
                var output = (string)message.response_body.data;
32
 
 
33
 
                var start = output.index_of ("text=\"") + 6;
34
 
                text = output.substring (start, output.index_of ("\"", start) - start);
35
 
                start = output.index_of ("temp=\"") + 6;
36
 
                temp = output.substring (start, output.index_of ("\"", start) - start);
37
 
 
38
 
        }
 
111
 
 
112
                        session.queue_message (message, (session, m) => {
 
113
                        var data = (string)m.response_body.data;
 
114
                        var current = get_attributes (data, "yweather:condition", {"temp", "text", "code"});
 
115
                        var forecast = get_attributes (data, "yweather:forecast", {"day", "date", "low", "high", "text", "code"});
 
116
                        var forecast2 = get_attributes (data, "yweather:forecast", {"day", "date", "low", "high", "text", "code"},
 
117
                                data.index_of ("<yweather:forecast") + 20);
 
118
 
 
119
                        current_content = "<span face='gazetteweather' font='42'>" + condition_codes[int.parse (current[2])] + "</span>" +
 
120
                                "<span face='Open Sans Light' font='24'> " + current[1] +"</span>\n" +
 
121
                                "<span face='Raleway' weight='100' font='72'>" + current[0] + "</span>";
 
122
                        for_content = "<span face='gazetteweather' font='30'>" + condition_codes[int.parse (forecast[5])] + "</span>" +
 
123
                                "<span face='Open Sans Light' font='26'> " + forecast[0] + "</span>\n" +
 
124
                                "<span face='Open Sans Light' font='16'>" + forecast[4] + "</span>\n" +
 
125
                                "<span face='Raleway' font='32' weight='200'>" + forecast[2] + " - " + forecast[3] + "</span>" +
 
126
                                "<span face='gazetteweather' font='30'>" + condition_codes[int.parse (forecast2[5])] + "</span>" +
 
127
                                "<span face='Open Sans Light' font='26'> " + forecast2[0] + "</span>\n" +
 
128
                                "<span face='Open Sans Light' font='16'>" + forecast2[4] + "</span>\n" +
 
129
                                "<span face='Raleway' font='32' weight='200'>" + forecast2[2] + " - " + forecast2[3] + "</span>";
 
130
 
 
131
                        for1.y = cur.height;
 
132
                        for2.y = cur.height;
 
133
                        for2.x = for1.width;
 
134
                        cur.x = Math.floorf (width / 2 - cur.width / 2);
 
135
                });
 
136
        }
39
137
 
40
138
        public override void create ()
41
139
        {
49
147
                        unit = "C";
50
148
                }
51
149
 
52
 
                var label = new ShadowedLabel ("<span font='20' face='Raleway'>%s</span>".printf (_("Your Local Weather")+"\n")+
53
 
                        "<span face='Open Sans Light' font='52'>☔</span>"+
 
150
 
 
151
                var label = new ShadowedLabel ("<span font='20' face='Raleway'>%s</span>".printf (_("Your Local Weather"+"\n"))+
54
152
                        "<span face='Open Sans Light' font='24'><i>"+text+"</i></span>\n"+
55
 
                        "<span face='Raleway' font='72'>  "+temp+"°"+unit+"</span>");
 
153
                        "<span face='Raleway' font='72'>  "+current_content+"°"+unit
 
154
                        +for_content
 
155
                        +"</span>");
56
156
                add_child (label);
57
157
        }
58
158
}