| Conditions | 13 |
| Total Lines | 137 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like weather.GetWeatherImage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package weather |
||
| 121 | func GetWeatherImage(ctx *bot.Context) (buf *bytes.Buffer, cityName string, err error) { |
||
| 122 | var ( |
||
| 123 | forecast Forecast |
||
| 124 | city = ctx.GetGuild().WeatherCity |
||
| 125 | ) |
||
| 126 | |||
| 127 | if len(ctx.Args) > 0 { |
||
| 128 | city = strings.Join(ctx.Args, "+") |
||
| 129 | } |
||
| 130 | |||
| 131 | loc, err := location.New(ctx.Conf.General.GeonamesUsername, city) |
||
| 132 | if err != nil { |
||
| 133 | fmt.Printf("Location API: %v", err) |
||
| 134 | return |
||
| 135 | } |
||
| 136 | |||
| 137 | cityName = loc.Geonames[0].CountryName + ", " + loc.Geonames[0].Name |
||
| 138 | |||
| 139 | // Get coordinates and get weather data |
||
| 140 | newlat, newlng := loc.GetCoordinates() |
||
| 141 | resp, err := http.Get(fmt.Sprintf("https://api.openweathermap.org/data/2.5/forecast?lat=%v&lon=%v&lang=%v&units=metric&appid=%v", |
||
| 142 | newlat, newlng, ctx.Conf.General.Language, ctx.Conf.Weather.WeatherToken)) |
||
| 143 | if err != nil { |
||
| 144 | fmt.Printf("Weather API: %v", err) |
||
| 145 | return |
||
| 146 | } |
||
| 147 | |||
| 148 | err = json.NewDecoder(resp.Body).Decode(&forecast) |
||
| 149 | if err != nil { |
||
| 150 | fmt.Printf("Weather Decode: %v", err) |
||
| 151 | return |
||
| 152 | } |
||
| 153 | |||
| 154 | // Drawing forecast |
||
| 155 | //dc := gg.NewContext(1500, 400) |
||
| 156 | //dc.SetRGBA255(36, 48, 64, 255) |
||
| 157 | //dc.Clear() |
||
| 158 | //for i := 0; i < 6; i++ { |
||
| 159 | // dc.DrawImage(DrawOne(int(forecast.Weather[i].Main.TempMin), |
||
| 160 | // forecast.Weather[i].Main.Humidity, |
||
| 161 | // int(forecast.Weather[i].Clouds.All), |
||
| 162 | // fmt.Sprintf("%.2v:00", forecast.Weather[i].TZTime(ctx.Conf.General.Timezone).Hour()), |
||
| 163 | // ctx.WeatherCode(fmt.Sprintf("%v", forecast.Weather[i].WDesc[0].Id))), 300*i, 0) |
||
| 164 | //} |
||
| 165 | |||
| 166 | gc := gg.NewContext(400, 700) |
||
| 167 | gc.Clear() |
||
| 168 | |||
| 169 | // Template |
||
| 170 | gc.SetRGB255(242, 97, 73) |
||
| 171 | gc.DrawRectangle(0, 0, 400, 700) |
||
| 172 | gc.Fill() |
||
| 173 | |||
| 174 | gc.SetRGB255(234, 89, 65) |
||
| 175 | gc.DrawRectangle(0, 250, 400, 100) |
||
| 176 | gc.DrawRectangle(0, 450, 400, 100) |
||
| 177 | gc.DrawRectangle(0, 650, 400, 100) |
||
| 178 | gc.Fill() |
||
| 179 | |||
| 180 | gc.SetLineWidth(2) |
||
| 181 | gc.SetRGBA(0, 0, 0,0.05) |
||
| 182 | gc.DrawLine(0, 250, 400, 250) |
||
| 183 | gc.DrawLine(0, 348, 400, 348) |
||
| 184 | gc.DrawLine(0, 450, 400, 450) |
||
| 185 | gc.DrawLine(0, 548, 400, 548) |
||
| 186 | gc.DrawLine(0, 650, 400, 650) |
||
| 187 | gc.DrawLine(0, 748, 400, 748) |
||
| 188 | gc.Stroke() |
||
| 189 | |||
| 190 | // Text |
||
| 191 | if err := gc.LoadFontFace("lato.ttf", 20); err != nil { |
||
| 192 | panic(err) |
||
| 193 | } |
||
| 194 | // Header |
||
| 195 | gc.SetRGBA(1, 1, 1, 0.7) |
||
| 196 | gc.DrawStringAnchored(cityName, 10, 15, 0, 0.5) |
||
| 197 | gc.SetRGBA(1, 1, 1, 0.4) |
||
| 198 | gc.DrawStringAnchored(time.Now().Format("Jan 2, 2006"), 280, 15, 0, 0.5) |
||
| 199 | |||
| 200 | // First weather data |
||
| 201 | gc.SetRGBA(1, 1, 1, 0.5) |
||
| 202 | if err := gc.LoadFontFace("lato.ttf", 30); err != nil { |
||
| 203 | panic(err) |
||
| 204 | } |
||
| 205 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Weather[0].TZTime(ctx.Conf.General.Timezone).Hour()), 50, 200, 0.5, 0.5) |
||
| 206 | gc.DrawStringAnchored(fmt.Sprintf("H:%v%%", forecast.Weather[0].Main.Humidity), 200, 200, 0.5, 0.5) |
||
| 207 | gc.DrawStringAnchored(fmt.Sprintf("C:%v%%", int(forecast.Weather[0].Clouds.All)), 350, 200, 0.5, 0.5) |
||
| 208 | |||
| 209 | gc.SetRGBA(1, 1, 1, 1) |
||
| 210 | if err := gc.LoadFontFace("lato.ttf", 90); err != nil { |
||
| 211 | panic(err) |
||
| 212 | } |
||
| 213 | |||
| 214 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Weather[0].Main.TempMin)), 30, 120, 0, 0.5) |
||
| 215 | |||
| 216 | if err := gc.LoadFontFace("owfont-regular.ttf", 90); err != nil { |
||
| 217 | panic(err) |
||
| 218 | } |
||
| 219 | |||
| 220 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Weather[0].WDesc[0].Id)), 250, 120, 0, 0.7) |
||
| 221 | |||
| 222 | if err := gc.LoadFontFace("lato.ttf", 30); err != nil { |
||
| 223 | panic(err) |
||
| 224 | } |
||
| 225 | |||
| 226 | // Time |
||
| 227 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Weather[1].TZTime(ctx.Conf.General.Timezone).Hour()), 100, 300, 0, 0.5) |
||
| 228 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Weather[2].TZTime(ctx.Conf.General.Timezone).Hour()), 100, 400, 0, 0.5) |
||
| 229 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Weather[3].TZTime(ctx.Conf.General.Timezone).Hour()), 100, 500, 0, 0.5) |
||
| 230 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Weather[4].TZTime(ctx.Conf.General.Timezone).Hour()), 100, 600, 0, 0.5) |
||
| 231 | |||
| 232 | if err := gc.LoadFontFace("lato.ttf", 50); err != nil { |
||
| 233 | panic(err) |
||
| 234 | } |
||
| 235 | |||
| 236 | // Temperature |
||
| 237 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Weather[1].Main.TempMin)), 250, 300, 0, 0.5) |
||
| 238 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Weather[2].Main.TempMin)), 250, 400, 0, 0.5) |
||
| 239 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Weather[3].Main.TempMin)), 250, 500, 0, 0.5) |
||
| 240 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Weather[4].Main.TempMin)), 250, 600, 0, 0.5) |
||
| 241 | |||
| 242 | if err := gc.LoadFontFace("owfont-regular.ttf", 60); err != nil { |
||
| 243 | panic(err) |
||
| 244 | } |
||
| 245 | |||
| 246 | // Weather icon |
||
| 247 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Weather[1].WDesc[0].Id)), 20, 300, 0, 0.7) |
||
| 248 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Weather[2].WDesc[0].Id)), 20, 400, 0, 0.7) |
||
| 249 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Weather[3].WDesc[0].Id)), 20, 500, 0, 0.7) |
||
| 250 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Weather[4].WDesc[0].Id)), 20, 600, 0, 0.7) |
||
| 251 | |||
| 252 | buf = new(bytes.Buffer) |
||
| 253 | pngerr := png.Encode(buf, gc.Image()) |
||
| 254 | if pngerr != nil { |
||
| 255 | fmt.Printf("Image: %v", pngerr) |
||
| 256 | } |
||
| 257 | return |
||
| 258 | } |
||
| 259 |