├── LICENSE ├── Makefile ├── README.rst ├── TODO.rst ├── Tupfile.ini ├── Tuprules.tup ├── configure.sh ├── docs ├── Tupfile ├── _shaman ├── conf.py ├── shaman.rst ├── strfweather.3 └── strfweather.tex ├── src ├── Tupfile ├── shaman.c ├── usage.h ├── weather.c └── weather.h ├── test ├── Tupfile.bak ├── suite.c └── test.json └── tup.config /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PROGNM = shaman 2 | PREFIX ?= /usr/local 3 | DOCDIR ?= $(DESTDIR)$(PREFIX)/share/man 4 | BINDIR ?= $(DESTDIR)$(PREFIX)/bin 5 | INCDIR ?= $(DESTDIR)$(PREFIX)/include 6 | LIBDIR ?= $(DESTDIR)$(PREFIX)/lib 7 | ZSHDIR ?= $(DESTDIR)$(PREFIX)/share/zsh 8 | 9 | .PHONY: all clean gen clang-analyze cov-build simple install uninstall 10 | 11 | all: dist 12 | @tup upd 13 | 14 | clean: 15 | @rm -rf -- dist cov-int $(PROGNM).tgz make.sh ./src/*.plist 16 | 17 | dist: 18 | @mkdir -p ./dist/locale 19 | 20 | gen: clean 21 | @tup generate make.sh 22 | 23 | cov-build: gen dist 24 | @cov-build --dir cov-int ./make.sh 25 | @tar czvf $(PROGNM).tgz cov-int 26 | 27 | clang-analyze: 28 | @(pushd ./src; clang-check -analyze ./*.c) 29 | 30 | simple: gen dist 31 | @./make.sh 32 | 33 | install: 34 | @install -Dm755 dist/shaman $(BINDIR)/$(PROGNM) 35 | @install -Dm755 dist/libweather.so $(LIBDIR)/libweather.so 36 | @install -Dm644 dist/weather.h $(INCDIR)/shaman/weather.h 37 | @install -Dm644 dist/shaman.1 $(DOCDIR)/man1/$(PROGNM).1 38 | @install -Dm644 dist/strfweather.3 $(DOCDIR)/man3/strfweather.3 39 | @install -Dm644 dist/_$(PROGNM) $(ZSHDIR)/site-functions/_$(PROGNM) 40 | 41 | uninstall: 42 | @rm -f $(BINDIR)/$(PROGNM) 43 | @rm -f $(LIBDIR)/libweather.so 44 | @rm -f $(INCDIR)/shaman/weather.h 45 | @rm -f $(DOCDIR)/man1/$(PROGNM).1 46 | @rm -f $(DOCDIR)/man3/strfweather.3 47 | @rm -f $(ZSHDIR)/site-functions/_shaman 48 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Shaman 2 | ====== 3 | 4 | Originally inspired by a tool called weatherman, ``shaman`` is a small, native C utilty relying on a small, native C library (``libweather``) to fetch current weather conditions. 5 | For the moment, ``libweather`` and ``shaman`` support grabbing weather from `OpenWeatherMap `_ (which draws data from various sources accounting for nearly 80000 cities). 6 | However, because the library uses a source-agnostic data structure, other weather providers can be easily added. 7 | The only requirement for a new provider (which may become less stringent) is that they have a JSON API. 8 | 9 | If you would like to see support for a particular weather provider added to ``shaman`` and ``libweather``, please open a bug report listing the name of the provider, along with a link to their JSON API. 10 | *NOTE*: Implementing other providers is a lower priority than getting ``shaman`` and ``libweather`` usable with current functionality. 11 | 12 | Dependencies 13 | ------------ 14 | ``shaman`` and ``libweather`` are being written to use as few dependencies as possible while still trying to keep the utilities themselves light-weight. 15 | To that end, they now both require only two external dependencies: 16 | 17 | * `curl `_, which handles all network interaction very gracefully 18 | * `jansson `_, which allows for very efficient JSON Parsing 19 | * `tup `_, the build system that I will use going forward 20 | * `sphinx `_, for building the man-page from reStructuredText 21 | 22 | For a variety of reasons, mainly because ``shaman`` does not rely on parallelization at the moment and because of ``-Weverything``, I will no longer be officially supporting ``gcc``. 23 | Resultingly, to use the official build system, you will need ``clang`` installed. 24 | Ideally, when I get a continuous integration setup again, I will have the build tested against both compilers. 25 | 26 | Status & Documentation 27 | ---------------------- 28 | ``shaman`` is still under heavy active development. 29 | For the moment, the manual documentation is quite out-of-date. 30 | The best ways to follow its status and functionality are to track the commit list and keep up with the `To-do lists `_. 31 | 32 | Contributions 33 | ------------- 34 | All contributions are welcome (pull requests, constructive criticism, feature requests and musings), but pull requests should attempt to follow `these guidelines `_ for consistency. 35 | I am likely to reformat any commit that should deviate from these guidelines. 36 | -------------------------------------------------------------------------------- /TODO.rst: -------------------------------------------------------------------------------- 1 | To-Do Lists 2 | =========== 3 | 4 | General 5 | ------- 6 | 7 | * [ ] Use ``stdint.h``/``stdbool.h`` types 8 | * [ ] Improve internal and exposed error handling (use ``errno.h``) 9 | * [ ] Update ``configure.sh`` 10 | 11 | * [ ] Add checks for versions of libraries (? Might just stay rolling) 12 | * [ ] Add checks for the particular functions used in the checked headers 13 | 14 | * [ ] Update documentation 15 | 16 | * [-] Switch to rst2man instead of latex2man and generate docs in-build 17 | 18 | * [ ] Update ``_shaman`` (zsh completion) 19 | 20 | * [ ] Add Bash completion? 21 | 22 | libweather 23 | ---------- 24 | 25 | * [ ] Create a ``setopt`` interface rather than passing a huge number of arguments 26 | * [ ] Add support for formatting time variables 27 | * [ ] Fix escape sequences 28 | * [ ] Split ``fetch_data_file()``, ``fetch_data_owm()`` and ``read_weather()`` into a separate header (tentatively ``owm.h``) 29 | 30 | * [ ] Probably rename to avoid name conflicts (tentatively, ``fetch_owm_local()``, ``fetch_owm_remote()`` and ``read_owm()``) 31 | 32 | shaman 33 | ------ 34 | 35 | * [ ] Switch to `argp `_ instead of ``getopt`` (?) 36 | * [ ] Implement a placeholder structure (and option) to specify a provider 37 | 38 | test suite 39 | ---------- 40 | 41 | * [ ] Add unit tests for ``shaman`` 42 | * [-] Stop doing silly things in test suite (e.g., testing for equality on floats) 43 | -------------------------------------------------------------------------------- /Tupfile.ini: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HalosGhost/shaman/73c048d9d2eac59be90d6d11182a020515d8abd0/Tupfile.ini -------------------------------------------------------------------------------- /Tuprules.tup: -------------------------------------------------------------------------------- 1 | CFLAGS += -g -O2 -fstack-protector-all -std=c11 -Weverything -D_FORTIFY_SOURCE=2 2 | LDFLAGS += -lcurl -ljansson -lm -Wl,-z,relro,-z,now 3 | CC = clang 4 | -------------------------------------------------------------------------------- /configure.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | check_lib () { 4 | printf 'Checking for %s\t\t\t[ PEND ]\r' "$1" 5 | if [[ "$(pkg-config --exists $1; echo $?)" == 0 ]]; then 6 | printf 'Checking for %s\t\t\t[ \x1b[32mPASS\x1b[0m ]\n' "$1" 7 | else 8 | printf 'Checking for %s\t\t\t[ \x1b[31mFAIL\x1b[0m ]\n' "$1" 9 | return 1 10 | fi 11 | } 12 | 13 | check_header () { 14 | printf 'Checking for %s\t[ PEND ]\r' "$1" 15 | if [[ -f "$1" ]]; then 16 | printf 'Checking for %s\t[ \x1b[32mPASS\x1b[0m ]\n' "$1" 17 | else 18 | printf 'Checking for %s\t[ \x1b[31mFAIL\x1b[0m ]\n' "$1" 19 | return 1 20 | fi 21 | } 22 | 23 | printf 'Checking for /usr/bin/clang\t\t[ PEND ]\r' 24 | if [[ -f '/usr/bin/clang' ]]; then 25 | printf 'Checking for /usr/bin/clang\t\t[ \x1b[32mPASS\x1b[0m ]\n' 26 | else 27 | printf 'Checking for /usr/bin/clang\t\t[ \x1b[31mFAIL\x1b[0m ]\n' 28 | return 1 29 | fi 30 | 31 | for i in "libcurl" "jansson"; do 32 | check_lib "$i" 33 | done 34 | 35 | for i in "/usr/include/"{"stdio","stdlib","string","getopt","errno","time","sys/"{"stat","types"},"stdint","math"}".h"; do 36 | check_header "$i" 37 | done 38 | -------------------------------------------------------------------------------- /docs/Tupfile: -------------------------------------------------------------------------------- 1 | include_rules 2 | 3 | export LANG 4 | : shaman.rst |> sphinx-build -b man -Dversion=@(VERSION) -E . ../dist; rm -rf ../dist/.doctrees |> ../dist/shaman.1 5 | : strfweather.3 |> cp %f %o |> ../dist/strfweather.3 6 | : _shaman |> cp %f %o |> ../dist/_shaman 7 | -------------------------------------------------------------------------------- /docs/_shaman: -------------------------------------------------------------------------------- 1 | #compdef shaman 2 | _arguments -C -A "-*" \ 3 | -a'[print all info]' \ 4 | -c'[print current conditons]' \ 5 | -d'[print humidity]' \ 6 | -D'[print dewpoint]' \ 7 | -h'[print help]' \ 8 | -l'[print heat index]' \ 9 | -m'[print with metric units]' \ 10 | -p'[print pressure]' \ 11 | -r'[print reporter information]' \ 12 | -t'[print temperature]' \ 13 | -v'[print visibility]' \ 14 | -w'[print wind information]' \ 15 | -z'[suppress hazardous weather warnings]' \ 16 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | project = 'shaman' 5 | master_doc = 'shaman' 6 | source_suffix = '.rst' 7 | 8 | show_authors = False 9 | today_fmt = '%d %b %Y' 10 | 11 | # One entry per manual page. List of tuples 12 | # (source start file, name, description, authors, manual section). 13 | man_pages = [ 14 | ('shaman', 'shaman', 'A lightweight tool to fetch weather data', [], 1), 15 | ] 16 | -------------------------------------------------------------------------------- /docs/shaman.rst: -------------------------------------------------------------------------------- 1 | .. Copyright (C) 2012-2014, Sam Stuewe 2 | Copyright (C) 2014, Johannes Löthberg 3 | 4 | shaman 5 | ====== 6 | 7 | Synopsis 8 | -------- 9 | 10 | **shaman** [*options*] 11 | 12 | Description 13 | ----------- 14 | 15 | :program:`shaman` is a very lightweight application to fetch weather data. 16 | 17 | If shaman is not given a location, it will take the data from the cache file. If shaman is not given a format string, it will default to "%c (%t°U)" (where "U" is either "F" or "C" depending on the units requested). 18 | 19 | Caching and Refreshing 20 | ---------------------- 21 | 22 | Shaman will look for the cache in the following locations stopping upon the first file found: 23 | 24 | :file:`$XDG_CONFIG_HOME/shaman/SCALE.json` 25 | 26 | :file:`$HOME/.shaman/SCALE.json` 27 | 28 | In both of the paths listed above, ``SCALE`` may be either ``imperial`` or ``metric`` depending on the units requested. In accordance with OpenWeatherMap's API, a call for new data should be made no more often than once per 10 minutes per device. Shaman will, therefore, automatically use data from the cache (assuming the requested location and units are the same) until the file's last-modified timestamp is over 10 minutes old (or the ``--refresh`` flag has been passed). 29 | 30 | Options 31 | ------- 32 | 33 | -h, --help 34 | Print usage information and exit 35 | 36 | -c CACHE, --cache=CACHE 37 | Use the file located at ``CACHE`` for data caching 38 | 39 | -f FORMAT, --format=FORMAT 40 | Specify an output format string 41 | 42 | -i, --imperial 43 | Print information using Imperial units 44 | 45 | -l LOCATION, --location=LOCATION 46 | Specify location to query. Note that ``LOCATION`` should be formatted as "City Name,CC" where "CC" is the ISO 3166 two-digit Country Code. 47 | 48 | -m, --metric 49 | Print information using Metric units 50 | 51 | -q, --quiet 52 | Silence all output. See the section on Return Values for more information 53 | 54 | -r, --refresh 55 | Force updating the cache 56 | 57 | -v, --verbose 58 | Show more output 59 | 60 | Format string 61 | ------------- 62 | 63 | The following format specifiers may be used in the format string:: 64 | 65 | %% A literal percent sign 66 | %a Time of data receipt (unixtime) 67 | %b Barometric pressure (hectoPascals) 68 | %c Weather condition string 69 | %C Weather condition code 70 | %d Cloud coverage (percent) 71 | %h Low temperature (°F | °C) 72 | %H High temperature (°F | °C) 73 | %i City ID number 74 | %I City name 75 | %j Country Code (ISO3166) 76 | %l Latitude (degrees) 77 | %L Longitude (degrees) 78 | %p Precipitation (in | mm) 79 | %P Relative humidity (percent) 80 | %s Time of sunrise (unixtime) 81 | %S Time of sunset (unixtime) 82 | %t Current temperature (°F | °C) 83 | %w Sustained wind speed (mph|mps) 84 | %W Gusting wind speed (mph|mps) 85 | %x Wind heading (degrees) 86 | %X Wind heading (Cardinal direction) 87 | 88 | All other valid escape characters may also be used (e.g., \\n and \\\\). 89 | 90 | Return value 91 | ------------ 92 | 93 | When called without the ``-q`` flag, shaman will return 0 on success else it will return 1. However, when called with ``-q``, shaman's return code will be set from 1 to 9 (1 indicating failure, and each other code representing one of the following groups of weather conditions):: 94 | 95 | 2 Thunderstorm 96 | 3 Drizzle 97 | 5 Rain 98 | 6 Snow 99 | 7 Reduced Visibility 100 | 8 Clouds (includes "Clear Skies") 101 | 9 Extreme Hazards or Miscellaneous 102 | 103 | See also 104 | -------- 105 | 106 | :manpage:`libcurl(3)`, :manpage:`libweather(3)` 107 | -------------------------------------------------------------------------------- /docs/strfweather.3: -------------------------------------------------------------------------------- 1 | '\" t 2 | .\" Manual page created with latex2man on Fri Jul 11 02:23:45 CDT 2014 3 | .\" NOTE: This file is generated, DO NOT EDIT. 4 | .de Vb 5 | .ft CW 6 | .nf 7 | .. 8 | .de Ve 9 | .ft R 10 | 11 | .fi 12 | .. 13 | .TH "STRFWEATHER" "3" "11 July 2014" "Fetch Weather Data " "Fetch Weather Data " 14 | .SH NAME 15 | 16 | strfweather 17 | \- fetch weather data in native C 18 | .PP 19 | .SH SYNOPSIS 20 | 21 | #include 22 | .PP 23 | size_t strfweather (char * dest_str, size_t max, const char * format, const struct weather * weather); 24 | .PP 25 | .SH DESCRIPTION 26 | 27 | The strfweather 28 | function breaks down the weather struct according to the passed format and writes the result to dest_str to, at most, max bytes. 29 | .PP 30 | The format specification is a null\-terminated char array (a string) which consists of any number of conversion specifiers (each beginning with a %) and normal text. 31 | Any non\-conversion specifier character will be copied verbatim to dest_str (including the null byte). 32 | Conversion specifiers, however, will be replaced with the unit they represent. 33 | A list of conversion specifiers and their results are below. 34 | .PP 35 | .SH FORMAT 36 | 37 | The following format specifiers are permitted: 38 | .PP 39 | .Vb 40 | %% A literal percent sign 41 | %a Time of data receipt (unixtime) 42 | %b Barometric pressure (hectoPascals) 43 | %c Weather condition string 44 | %C Weather condition code 45 | %d Cloud coverage (percent) 46 | %h Low temperature (°F | °C) 47 | %H High temperature (°F | °C) 48 | %i City ID number 49 | %I City name 50 | %j Country Code 51 | %l Latitude (degrees) 52 | %L Longitude (degrees) 53 | %p Precipitation (in | mm) 54 | %P Relative humidity (percent) 55 | %s Time of sunrise (unixtime) 56 | %S Time of sunset (unixtime) 57 | %t Current temperature (°F | °C) 58 | %w Sustained wind speed (mph|kmph) 59 | %W Gusting wind speed (mph|kmph) 60 | %x Wind heading (degrees) 61 | %X Wind heading (Cardinal / Ordinal direction) 62 | .Ve 63 | .PP 64 | Since all non\-conversion specifiers are copied verbatim, escape sequences (e.g., \\n and \\r) are allowed. 65 | If the format specification calls for a string in the weather struct that is null, strfweather 66 | will write the string "Unavailable" in its place. 67 | .PP 68 | .SH RETURN VALUE 69 | 70 | Should the whole string be copied to dest_str without truncating, strfweather 71 | will return the number of bytes written (the length of dest_str). 72 | If, however, the output to dest_str should be truncated, strfweather 73 | will return 0. 74 | .PP 75 | .SH AUTHOR 76 | 77 | Copyright (C)2012\-2013 Sam Stuewe 78 | .br 79 | License GPLv2: GPL version 2 \fBhttps://www.gnu.org/licenses/gpl\-2.0.html\fP 80 | .br 81 | This is free software: you are free to change and redistribute it. 82 | .br 83 | There is NO WARRANTY, to the extent permitted by law. 84 | .PP 85 | Feature requests, bug reports and other comments may be submitted via GitHub (\fBhttps://github.com/HalosGhost/shaman.git\fP) 86 | or E\-mail (\fBhalosghost@archlinux.info\fP) 87 | .PP 88 | .SH SEE ALSO 89 | 90 | \fIshaman\fP(1), 91 | \fIlibowm\fP(3) 92 | .PP 93 | .\" NOTE: This file is generated, DO NOT EDIT. 94 | -------------------------------------------------------------------------------- /docs/strfweather.tex: -------------------------------------------------------------------------------- 1 | \documentclass{latex2man} 2 | 3 | \begin{Name}{3}{strfweather}{Sam Stuewe}{Fetch Weather Data}{strfweather - fetch weather data in native C} 4 | \Prog{strfweather} - fetch weather data in native C 5 | \end{Name} 6 | 7 | \section{Synopsis} 8 | #include 9 | 10 | size\_t strfweather (char * dest\_str, size\_t max, const char * format, const struct weather * weather); 11 | 12 | \section{Description} 13 | The \Prog{strfweather} function breaks down the weather struct according to the passed format and writes the result to dest\_str to, at most, max bytes. 14 | 15 | The format specification is a null-terminated char array (a string) which consists of any number of conversion specifiers (each beginning with a \%) and normal text. 16 | Any non-conversion specifier character will be copied verbatim to dest\_str (including the null byte). 17 | Conversion specifiers, however, will be replaced with the unit they represent. 18 | A list of conversion specifiers and their results are below. 19 | 20 | \section{Format} 21 | The following format specifiers are permitted: 22 | 23 | \begin{verbatim} 24 | %% A literal percent sign 25 | %a Time of data receipt (unixtime) 26 | %b Barometric pressure (hectoPascals) 27 | %c Weather condition string 28 | %C Weather condition code 29 | %d Cloud coverage (percent) 30 | %h Low temperature (°F | °C) 31 | %H High temperature (°F | °C) 32 | %i City ID number 33 | %I City name 34 | %j Country Code 35 | %l Latitude (degrees) 36 | %L Longitude (degrees) 37 | %p Precipitation (in | mm) 38 | %P Relative humidity (percent) 39 | %s Time of sunrise (unixtime) 40 | %S Time of sunset (unixtime) 41 | %t Current temperature (°F | °C) 42 | %w Sustained wind speed (mph|kmph) 43 | %W Gusting wind speed (mph|kmph) 44 | %x Wind heading (degrees) 45 | %X Wind heading (Cardinal / Ordinal direction) 46 | \end{verbatim} 47 | 48 | Since all non-conversion specifiers are copied verbatim, escape sequences (e.g., \Bs n and \Bs r) are allowed. 49 | If the format specification calls for a string in the weather struct that is null, \Prog{strfweather} will write the string "Unavailable" in its place. 50 | 51 | \section{Return Value} 52 | Should the whole string be copied to dest\_str without truncating, \Prog{strfweather} will return the number of bytes written (the length of dest\_str). 53 | If, however, the output to dest\_str should be truncated, \Prog{strfweather} will return 0. 54 | 55 | \section{Author} 56 | Copyright \copyright 2012-2013 Sam Stuewe\\ 57 | License GPLv2: GPL version 2 \URL{https://www.gnu.org/licenses/gpl-2.0.html} \\ 58 | This is free software: you are free to change and redistribute it. \\ 59 | There is NO WARRANTY, to the extent permitted by law. 60 | 61 | Feature requests, bug reports and other comments may be submitted via GitHub (\URL{https://github.com/HalosGhost/shaman.git}) or E-mail (\Email{halosghost@archlinux.info}) 62 | 63 | \section{See Also} 64 | \Cmd{shaman}{1}, \Cmd{libowm}{3} 65 | 66 | \LatexManEnd 67 | -------------------------------------------------------------------------------- /src/Tupfile: -------------------------------------------------------------------------------- 1 | include_rules 2 | 3 | : ../configure.sh |> ./%f |> 4 | : weather.c |> $(CC) $(CFLAGS) -Wno-disabled-macro-expansion -fPIC -c %f -shared -o %o |> ../dist/lib%B.so 5 | : weather.h |> cp %f %o |> ../dist/weather.h 6 | : shaman.c | ../dist/libweather.so |> $(CC) $(CFLAGS) $(LDFLAGS) -fPIE -pie -L../dist -lweather %f -o %o |> ../dist/shaman 7 | -------------------------------------------------------------------------------- /src/shaman.c: -------------------------------------------------------------------------------- 1 | /*******************************************************************\ 2 | * A small, native C utility for fetching weather * 3 | * Copyright (C) 2013-2014, Sam Stuewe * 4 | * * 5 | * This program is free software; you can redistribute it and/or * 6 | * modify it under the terms of the GNU General Public License * 7 | * as published by the Free Software Foundation; either version 2 * 8 | * of the License, or (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the Free Software * 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 18 | * Boston, MA 02110-1301, USA. * 19 | \*******************************************************************/ 20 | 21 | // Libraries // 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "weather.h" 33 | #include "usage.h" 34 | 35 | /** 36 | * If you fork this code, please DO NOT use this key. 37 | * Getting an API key with OWM is free, but this key is exclusively 38 | * for shaman 39 | */ 40 | #define OWMAPIKEY "83a3a133bc7541a6608536d490f7a11d" 41 | 42 | // Forward Declarations // 43 | char * 44 | locate_cache (char scale); 45 | 46 | // Main Function // 47 | signed 48 | main (signed argc, char * argv []) { 49 | 50 | char flag_scale = 'i', flag_refresh = 0, 51 | flag_verbose = 0, flag_quiet = 0; 52 | 53 | char * format = NULL, * location = NULL, 54 | * cache_path = NULL; 55 | 56 | if ( argc <= 1 ) { 57 | _usage(1); 58 | } else { 59 | static struct option os [] = { 60 | /* Flags */ 61 | { "help", no_argument, 0, 'h' }, 62 | { "imperial", no_argument, 0, 'i' }, 63 | { "metric", no_argument, 0, 'm' }, 64 | { "refresh", no_argument, 0, 'r' }, 65 | { "verbose", no_argument, 0, 'v' }, 66 | { "quiet", no_argument, 0, 'q' }, 67 | /* Swtiches */ 68 | { "cache", required_argument, 0, 'c' }, 69 | { "format", required_argument, 0, 'f' }, 70 | { "location", required_argument, 0, 'l' }, 71 | { 0, 0, 0, 0 }, 72 | }; 73 | 74 | for ( signed c = 0, oi = 0; c != -1; 75 | c = getopt_long(argc, argv, "himrvqc:f:l:", os, &oi) ) { 76 | 77 | unsigned long optarg_len; 78 | 79 | switch ( c ) { 80 | case 'h': 81 | _usage(0); 82 | 83 | case 'i': 84 | flag_scale = 'i'; break; 85 | 86 | case 'm': 87 | flag_scale = 'm'; break; 88 | 89 | case 'r': 90 | flag_refresh ++; break; 91 | 92 | case 'v': 93 | flag_verbose ++; break; 94 | 95 | case 'q': 96 | flag_quiet ++; break; 97 | 98 | case 'c': 99 | optarg_len = strlen(optarg) + 1; 100 | cache_path = malloc(optarg_len); 101 | strncpy(cache_path, optarg, optarg_len); 102 | break; 103 | 104 | case 'f': 105 | optarg_len = strlen(optarg) + 1; 106 | format = malloc(optarg_len); 107 | strncpy(format, optarg, optarg_len); 108 | break; 109 | 110 | case 'l': 111 | optarg_len = strlen(optarg) + 1; 112 | location = malloc(optarg_len); 113 | strncpy(location, optarg, optarg_len); 114 | break; 115 | } 116 | } 117 | } 118 | 119 | if ( !format ) { 120 | format = malloc(11); 121 | snprintf(format, 11, "%%c (%%t°%c)", (flag_scale == 'm' ? 'C' : 'F')); 122 | } 123 | 124 | if ( !location ) { _usage(1); } 125 | 126 | if ( !cache_path ) { cache_path = locate_cache(flag_scale); } 127 | 128 | signed fd = 0; 129 | if ( (fd = open(cache_path, O_CREAT, 0666)) == -1 ) { 130 | int errsv = errno; 131 | fprintf(stderr, "shaman: %s\n", strerror(errsv)); 132 | if ( cache_path ) { free(cache_path); } 133 | if ( location ) { free(location); } 134 | if ( format ) { free(format); } 135 | return 1; 136 | } close(fd); 137 | 138 | struct weather * wthr = owm_easy('q', location, flag_scale, cache_path, 139 | flag_refresh ? 0 : 600, OWMAPIKEY, 140 | flag_verbose); 141 | 142 | if ( cache_path ) { free(cache_path); } 143 | if ( location ) { free(location); } 144 | 145 | char output_string [BUFFER_SIZE]; 146 | strfweather(output_string, BUFFER_SIZE, format, wthr); 147 | 148 | if ( format ) { free(format); } 149 | 150 | if ( flag_quiet ) { 151 | exit((signed )wthr->weather_code / 100); // weather group as exit status 152 | } else { 153 | printf("%s\n", output_string); 154 | } return 0; 155 | } 156 | 157 | char * 158 | locate_cache (char scale) { 159 | 160 | char * cache_path; 161 | char * conf_prefix = getenv("XDG_CONFIG_HOME"); 162 | 163 | char location = conf_prefix ? 'x' : 'h'; 164 | if ( location == 'h' ) { conf_prefix = getenv("HOME"); }; 165 | 166 | size_t conf_prefix_len = strlen(conf_prefix); 167 | char * conf_dir = malloc(conf_prefix_len + 10); 168 | 169 | snprintf(conf_dir, conf_prefix_len + 9, "%s/%s", conf_prefix, 170 | (location == 'x' ? "shaman" : ".shaman")); 171 | signed error = mkdir(conf_dir, 172 | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 173 | // mode == 0755 174 | 175 | if ( error && errno != EEXIST ) { 176 | fprintf(stderr, "Error checking cache at %s: %s\n", conf_dir, 177 | strerror(errno)); 178 | 179 | free(conf_dir); 180 | exit(1); 181 | } else { 182 | size_t conf_dir_len = strlen(conf_dir); 183 | cache_path = malloc(conf_dir_len + 16); 184 | snprintf(cache_path, conf_dir_len + 15, 185 | (scale == 'i' ? "%s/imperial.json" : "%s/metric.json"), 186 | conf_dir); 187 | } 188 | 189 | free(conf_dir); 190 | return cache_path; 191 | } 192 | 193 | // vim: set ts=4 sw=4 et: 194 | -------------------------------------------------------------------------------- /src/usage.h: -------------------------------------------------------------------------------- 1 | #ifndef USAGE_H 2 | #define USAGE_H 3 | 4 | /*******************************************************************\ 5 | * Usage Functions for shaman * 6 | * Copyright (C) 2013-2014, Sam Stuewe * 7 | * * 8 | * This program is free software; you can redistribute it and/or * 9 | * modify it under the terms of the GNU General Public License * 10 | * as published by the Free Software Foundation; either version 2 * 11 | * of the License, or (at your option) any later version. * 12 | * * 13 | * This program is distributed in the hope that it will be useful, * 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 16 | * GNU General Public License for more details. * 17 | * * 18 | * You should have received a copy of the GNU General Public License * 19 | * along with this program; if not, write to the Free Software * 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 21 | * Boston, MA 02110-1301, USA. * 22 | \*******************************************************************/ 23 | 24 | // Libraries // 25 | #include 26 | #include 27 | 28 | // Usage // 29 | void 30 | _usage (signed error) __attribute__((noreturn)); 31 | 32 | void 33 | _usage (signed error) { 34 | 35 | fputs("Usage: shaman [options]\n\n" 36 | "Options:\n" 37 | " -h, --help\t\t\tprint help and exit\n" 38 | " -i, --imperial\t\tuse Imperial units (default)\n" 39 | " -m, --metric\t\t\tuse Metric units\n" 40 | " -q, --quiet\t\t\tsilence all output\n" 41 | " -r, --refresh\t\t\tforce update of local cache\n" 42 | " -v, --verbose\t\t\toutput more status information\n" 43 | " -c, --cache\t\t\tspecify location of local cache\n" 44 | " -f, --format=\"format\"\t\tformat output according to \"format\"\n" 45 | " -l, --location=\"location\"\tprint weather information for " 46 | "\"location\"\n\n" 47 | "See `man 1 shaman` for more information\n", 48 | (error ? stderr : stdout)); 49 | exit(error); 50 | } 51 | 52 | #endif // USAGE_H 53 | -------------------------------------------------------------------------------- /src/weather.c: -------------------------------------------------------------------------------- 1 | /*******************************************************************\ 2 | * A small, native C library for fetching weather * 3 | * Copyright (C) 2013-2014, Sam Stuewe * 4 | * * 5 | * This program is free software; you can redistribute it and/or * 6 | * modify it under the terms of the GNU General Public License * 7 | * as published by the Free Software Foundation; either version 2 * 8 | * of the License, or (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the Free Software * 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 18 | * Boston, MA 02110-1301, USA. * 19 | \*******************************************************************/ 20 | 21 | // Libraries // 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "weather.h" 35 | 36 | static const char * c_dir [] = { 37 | "N", "NbE", "NNE", "NEbN", 38 | "NE", "NEbE", "ENE", "EbN", 39 | "E", "EbS", "ESE", "SEbE", 40 | "SE", "SEbS", "SSE", "SbE", 41 | "S", "SbW", "SSW", "SWbS", 42 | "SW", "SWbW", "WSW", "WbS", 43 | "W", "WbN", "WNW", "NWbW", 44 | "NW", "NWbN", "NNW", "NbW" 45 | }; 46 | 47 | // Allow unicode representations of basic 48 | //static const char * icons [] = { 49 | // "\xe2\x98\x80", // Sun 50 | // "\xe2\x98\xbd", // Moon 51 | // "\xe2\x98\x81", // Clouds 52 | // "\xe2\x98\x94", // Rain 53 | // "\xe2\x9a\xa1", // Thunderstorm 54 | //}; 55 | 56 | // Function drawn from Petri Lehtinen's GitHub Jansson example 57 | static size_t 58 | write_data_buffer (void * ptr, size_t size, size_t nmemb, void * stream) { 59 | 60 | struct json_write_result * result = (struct json_write_result * )stream; 61 | if ( result->position + size * nmemb >= BUFFER_SIZE - 1 ) { 62 | fprintf(stderr, "Write Buffer is too small\n"); 63 | return 0; 64 | } 65 | 66 | memcpy(result->data + result->position, ptr, size * nmemb); 67 | result->position += size * nmemb; 68 | 69 | return size * nmemb; 70 | } 71 | 72 | // Fetch JSON from file 73 | struct json_write_result * 74 | owm_fetch_local (const char * json_file_path) { 75 | 76 | char * data = calloc(1, BUFFER_SIZE); 77 | static struct json_write_result written_result; 78 | written_result.data = data; 79 | written_result.position = 0; 80 | 81 | FILE * json_fp = fopen(json_file_path, "r"); 82 | 83 | fread(written_result.data, BUFFER_SIZE, 1, json_fp); 84 | 85 | if ( ferror(json_fp) ) { 86 | fclose(json_fp); 87 | fprintf(stderr, "Error reading file\n"); 88 | exit(1); 89 | } 90 | 91 | fclose(json_fp); 92 | 93 | return &written_result; 94 | } 95 | 96 | // Fetch JSON from OpenWeatherMap 97 | struct json_write_result * 98 | owm_fetch_remote (const char method, const char * location, const char scale, 99 | const char * file_cache_path, const char * api_key) { 100 | 101 | CURL * handle; 102 | CURLcode result; 103 | 104 | char * data = calloc(1, BUFFER_SIZE); 105 | static struct json_write_result written_result; 106 | written_result.data = data; 107 | written_result.position = 0; 108 | 109 | char url [256] = {'\0'}; 110 | char * fetch_method; 111 | char * fetch_scale; 112 | 113 | switch ( method ) { 114 | case 'i': 115 | fetch_method = "id"; 116 | break; 117 | 118 | default: 119 | fetch_method = "q"; 120 | break; 121 | } 122 | 123 | switch ( scale ) { 124 | case 'i': 125 | fetch_scale = "imperial"; 126 | break; 127 | 128 | default: 129 | fetch_scale = "metric"; 130 | break; 131 | } 132 | 133 | handle = curl_easy_init(); 134 | 135 | if ( handle ) { 136 | const char * provider = 137 | "http://api.openweathermap.org/data/2.5/weather"; 138 | 139 | char * encoded_location = curl_easy_escape(handle, location, 0); 140 | 141 | snprintf(url, sizeof(url), "%s?%s=%s&units=%s&APPID=%s", provider, 142 | fetch_method, encoded_location, fetch_scale, api_key); 143 | 144 | curl_easy_setopt(handle, CURLOPT_URL, url); 145 | curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data_buffer); 146 | curl_easy_setopt(handle, CURLOPT_WRITEDATA, &written_result); 147 | 148 | result = curl_easy_perform(handle); 149 | if ( result != CURLE_OK ) { 150 | fprintf(stderr, "Failed to retrieve data (%s)\n", 151 | curl_easy_strerror(result)); 152 | 153 | curl_easy_cleanup(handle); 154 | curl_free(encoded_location); 155 | exit(1); 156 | } 157 | 158 | curl_free(encoded_location); 159 | } 160 | 161 | if ( file_cache_path ) { 162 | json_error_t err; 163 | json_t * resjson = json_loads(written_result.data, 0, &err); 164 | size_t flags = JSON_PRESERVE_ORDER | JSON_INDENT(2); 165 | if ( resjson && json_dump_file(resjson, file_cache_path, flags) == -1 ) { 166 | fprintf(stderr, "Error caching file\n"); 167 | exit(1); 168 | } 169 | } curl_easy_cleanup(handle); 170 | 171 | return &written_result; 172 | } 173 | 174 | // Read JSON into a weather struct 175 | struct weather * 176 | owm_read (struct json_write_result * json) { 177 | 178 | json_error_t error; 179 | json_t * root = json_loads(json->data, 0, &error); 180 | free(json->data); 181 | 182 | if ( !root ) { 183 | fprintf(stderr, "Error on line %d (%s)\n", error.line, error.text); 184 | return NULL; 185 | } 186 | 187 | static struct weather fetched_weather; 188 | 189 | const char * root_key; 190 | json_t * root_value; 191 | json_object_foreach(root, root_key, root_value) { 192 | switch ( root_key[0] ) { 193 | case 'c': 194 | if ( json_typeof(root_value) == (signed )JSON_OBJECT ) { 195 | const char * sub_key; 196 | json_t * sub_value; 197 | json_object_foreach(root_value, sub_key, sub_value) { 198 | if ( root_key[1] == 'l' ) { 199 | fetched_weather.clouds = 200 | json_number_value(sub_value); 201 | } else if ( root_key[2] == 'o' ) { 202 | if ( sub_key[1] == 'o' ) { 203 | fetched_weather.longitude = 204 | json_number_value(sub_value); 205 | } else if ( sub_key[1] == 'a' ) { 206 | fetched_weather.latitude = 207 | json_number_value(sub_value); 208 | } 209 | } 210 | } 211 | } break; 212 | 213 | case 'd': 214 | fetched_weather.dt = json_integer_value(root_value); 215 | break; 216 | 217 | case 'i': 218 | fetched_weather.id = json_integer_value(root_value); 219 | break; 220 | 221 | case 'm': 222 | if ( root_key[1] == 'e' ) { 223 | fprintf(stderr, "%s\n", json_string_value(root_value)); 224 | free(root); 225 | exit(1); 226 | } else if ( json_typeof(root_value) == (signed )JSON_OBJECT ) { 227 | const char * sub_key; 228 | json_t * sub_value; 229 | json_object_foreach(root_value, sub_key, sub_value) { 230 | switch ( sub_key[0] ) { 231 | case 't': 232 | if ( strlen(sub_key) == 4 ) { 233 | fetched_weather.temperature = 234 | json_number_value(sub_value); 235 | } else if ( sub_key[6] == 'i' ) { 236 | fetched_weather.temp_min = 237 | json_number_value(sub_value); 238 | } else if ( sub_key[6] == 'a' ) { 239 | fetched_weather.temp_max = 240 | json_number_value(sub_value); 241 | } break; 242 | 243 | case 'p': 244 | fetched_weather.pressure = 245 | json_number_value(sub_value); 246 | break; 247 | 248 | case 'h': 249 | fetched_weather.humidity = 250 | json_number_value(sub_value); 251 | break; 252 | } 253 | } 254 | } break; 255 | 256 | case 'n': 257 | fetched_weather.name = json_string_value(root_value); 258 | if ( !fetched_weather.name ) { 259 | fetched_weather.name = "Unavailable"; 260 | } break; 261 | 262 | case 's': 263 | if ( json_typeof(root_value) == (signed )JSON_OBJECT ) { 264 | const char * sub_key; 265 | json_t * sub_value; 266 | json_object_foreach(root_value, sub_key, sub_value) { 267 | switch ( root_key[1] ) { 268 | case 'y': 269 | switch ( sub_key[4] ) { 270 | case 'i': 271 | fetched_weather.sunrise = 272 | json_integer_value(sub_value); 273 | break; 274 | 275 | case 'e': 276 | fetched_weather.sunset = 277 | json_integer_value(sub_value); 278 | break; 279 | 280 | case 't': 281 | fetched_weather.country = 282 | json_string_value(sub_value); 283 | 284 | if ( !fetched_weather.country ) { 285 | fetched_weather.country = 286 | "Unavailable"; 287 | } break; 288 | } break; 289 | 290 | case 'n': 291 | fetched_weather.precipitation_3h = 292 | json_number_value(sub_value); 293 | break; 294 | } 295 | } 296 | } break; 297 | 298 | case 'w': 299 | if ( json_typeof(root_value) == (signed )JSON_ARRAY ) { 300 | json_t * weather = json_array_get(root_value, 0); 301 | const char * sub_key; 302 | json_t * sub_value; 303 | json_object_foreach(weather, sub_key, sub_value) { 304 | switch ( sub_key[1] ) { 305 | case 'd': 306 | fetched_weather.weather_code = 307 | json_integer_value(sub_value); 308 | break; 309 | 310 | case 'e': 311 | fetched_weather.condition = 312 | json_string_value(sub_value); 313 | 314 | if ( !fetched_weather.condition ) { 315 | fetched_weather.condition = "Unavailable"; 316 | } break; 317 | } 318 | } 319 | } else if ( json_typeof(root_value) == (signed )JSON_OBJECT ) { 320 | const char * sub_key; 321 | json_t * sub_value; 322 | json_object_foreach(root_value, sub_key, sub_value) { 323 | switch ( sub_key[0] ) { 324 | case 's': 325 | fetched_weather.wind_speed = 326 | json_number_value(sub_value); 327 | break; 328 | 329 | case 'g': 330 | fetched_weather.wind_gust = 331 | json_number_value(sub_value); 332 | break; 333 | 334 | case 'd': 335 | fetched_weather.wind_direction = 336 | json_number_value(sub_value); 337 | break; 338 | } 339 | } 340 | } break; 341 | 342 | case 'r': 343 | if ( json_typeof(root_value) == (signed )JSON_OBJECT ) { 344 | const char * sub_key; 345 | json_t * sub_value; 346 | json_object_foreach(root_value, sub_key, sub_value) { 347 | if ( sub_key[0] == '3' ) { 348 | fetched_weather.precipitation_3h = 349 | json_number_value(sub_value); 350 | } 351 | } 352 | } break; 353 | } 354 | } 355 | 356 | free(root); 357 | 358 | return &fetched_weather; 359 | } 360 | 361 | static signed 362 | check_if_stale (const char * cache_path, const unsigned cache_update_time, 363 | const char verbosity) { 364 | 365 | struct stat cache_stats; 366 | signed status = stat(cache_path, &cache_stats); 367 | signed errsv = errno; 368 | 369 | if ( status == 0 ) { 370 | return ((time(NULL) - cache_stats.st_mtime) >= cache_update_time); 371 | } else { 372 | if ( verbosity > 0 ) { 373 | fprintf(stderr, 374 | "Determining if cache should be freshened failed: %s\n", 375 | strerror(errsv)); 376 | } return -1; 377 | } 378 | } 379 | 380 | struct weather * 381 | owm_easy (const char method, const char * location, const char scale, 382 | const char * file_cache_path, const unsigned cache_update_time, 383 | const char * api_key, const char verbosity) { 384 | 385 | struct json_write_result * json; 386 | struct weather * wthr; 387 | 388 | if ( file_cache_path ) { 389 | if ( check_if_stale(file_cache_path, cache_update_time, verbosity) ) { 390 | json = owm_fetch_remote(method, location, scale, file_cache_path, 391 | api_key); 392 | } else { 393 | json = owm_fetch_local(file_cache_path); 394 | char * city = malloc(strlen(location) - 3); 395 | sscanf(location, "%[^,]", city); 396 | if ( !strstr(json->data, city) ) { 397 | free(json->data); 398 | json = owm_fetch_remote(method, location, scale, 399 | file_cache_path, api_key); 400 | } free(city); 401 | } 402 | } else { 403 | json = owm_fetch_remote(method, location, scale, NULL, api_key); 404 | } 405 | 406 | wthr = owm_read(json); 407 | return wthr; 408 | } 409 | 410 | // TODO: Add support for formatting time variables 411 | // TODO: Add support for Apparent Temperature 412 | size_t 413 | strfweather (char * dest_str, size_t n, const char * format, 414 | const struct weather * w) { 415 | 416 | if ( n > INT_MAX ) { 417 | return 0; 418 | } size_t cl = 0; 419 | 420 | for ( ; *format; ++format) { 421 | if ( *format == '%' ) { 422 | switch ( *++format ) { 423 | case '\0': 424 | --format; 425 | break; 426 | 427 | case 'a': 428 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%ld", w->dt); 429 | continue; 430 | 431 | case 'b': 432 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->pressure); 433 | continue; 434 | 435 | case 'c': 436 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%s", w->condition); 437 | continue; 438 | 439 | case 'C': 440 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%lld", 441 | w->weather_code); 442 | continue; 443 | 444 | case 'd': 445 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->clouds); 446 | continue; 447 | 448 | case 'h': 449 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->temp_min); 450 | continue; 451 | 452 | case 'H': 453 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->temp_max); 454 | continue; 455 | 456 | case 'i': 457 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%lld", w->id); 458 | continue; 459 | 460 | case 'I': 461 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%s", w->name); 462 | continue; 463 | 464 | case 'j': 465 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%s", w->country); 466 | continue; 467 | 468 | case 'l': 469 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->latitude); 470 | continue; 471 | 472 | case 'L': 473 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->longitude); 474 | continue; 475 | 476 | case 'p': 477 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", 478 | w->precipitation_3h); 479 | continue; 480 | 481 | case 'P': 482 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->humidity); 483 | continue; 484 | 485 | case 's': 486 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%ld", w->sunrise); 487 | continue; 488 | 489 | case 'S': 490 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%ld", w->sunset); 491 | continue; 492 | 493 | case 't': 494 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->temperature); 495 | continue; 496 | 497 | //case 'T': 498 | // cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->au); 499 | //continue; 500 | 501 | case 'w': 502 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->wind_speed); 503 | continue; 504 | 505 | case 'W': 506 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", w->wind_gust); 507 | continue; 508 | 509 | case 'x': 510 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%g", 511 | w->wind_direction); 512 | continue; 513 | 514 | case 'X': { 515 | long dir = lrint(w->wind_direction / (360 / 32)) % 32; 516 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%s", c_dir[dir]); 517 | continue; 518 | } 519 | 520 | case '%': 521 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%c", *format); 522 | continue; 523 | 524 | default: 525 | break; 526 | } 527 | } else { 528 | cl += (size_t )snprintf(dest_str + cl, n - cl, "%c", *format); 529 | } 530 | } 531 | 532 | return (cl == n ? 0 : cl); 533 | } 534 | 535 | // vim: set ts=4 sw=4 et: 536 | -------------------------------------------------------------------------------- /src/weather.h: -------------------------------------------------------------------------------- 1 | #ifndef WEATHER_H 2 | #define WEATHER_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | /*******************************************************************\ 9 | * A small, native C library for fetching weather * 10 | * Copyright (C) 2013-2014, Sam Stuewe * 11 | * * 12 | * This program is free software; you can redistribute it and/or * 13 | * modify it under the terms of the GNU General Public License * 14 | * as published by the Free Software Foundation; either version 2 * 15 | * of the License, or (at your option) any later version. * 16 | * * 17 | * This program is distributed in the hope that it will be useful, * 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 20 | * GNU General Public License for more details. * 21 | * * 22 | * You should have received a copy of the GNU General Public License * 23 | * along with this program; if not, write to the Free Software * 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 25 | * Boston, MA 02110-1301, USA. * 26 | \*******************************************************************/ 27 | 28 | // Libraries // 29 | #include 30 | 31 | // Forward Declarations // 32 | #define BUFFER_SIZE 2048 // 2KiB 33 | 34 | struct json_write_result { 35 | char * data; 36 | unsigned long position; 37 | }; 38 | 39 | struct weather { 40 | long dt; // %a time data received 41 | double pressure; // %b barometric pressure 42 | const char * condition; // %c weather conditions 43 | double clouds; // %d cloud coverage 44 | double temp_min; // %h low temperature 45 | double temp_max; // %H high temperature 46 | long long id; // %i city id number 47 | const char * name; // %I city name 48 | const char * country; // %j country code 49 | double latitude; // %l latitude 50 | double longitude; // %L longitude 51 | double precipitation_3h; // %p precipitation 52 | double humidity; // %P relative humidity 53 | long sunrise; // %s time of sunrise 54 | long sunset; // %S time of sunset 55 | double temperature; // %t current temperature 56 | //double au; // %T apparent temperature 57 | double wind_speed; // %w sustained wind speed 58 | double wind_gust; // %W gusting wind speed 59 | double wind_direction; // %x wind heading 60 | long long weather_code; // %C weather condition code 61 | }; 62 | 63 | extern struct json_write_result * 64 | owm_fetch_local (const char *); 65 | 66 | extern struct json_write_result * 67 | owm_fetch_remote (const char, const char *, const char, const char *, 68 | const char *); 69 | 70 | extern struct weather * 71 | owm_read (struct json_write_result *); 72 | 73 | extern struct weather * 74 | owm_easy (const char, const char *, const char, const char *, const unsigned, 75 | const char *, const char); 76 | 77 | extern size_t 78 | strfweather (char *, size_t, const char *, const struct weather *); 79 | 80 | #ifdef __cplusplus 81 | } 82 | #endif 83 | 84 | #endif // WEATHER_H 85 | // vim: set ts=4 sw=4 et: 86 | -------------------------------------------------------------------------------- /test/Tupfile.bak: -------------------------------------------------------------------------------- 1 | include_rules 2 | 3 | : suite.c | ../dist/libweather.so |> $(CC) $(CFLAGS) $(LDFLAGS) -fPIE -pie -Wno-disabled-macro-expansion -L../dist -lweather %f -o %o |> ../dist/%B 4 | -------------------------------------------------------------------------------- /test/suite.c: -------------------------------------------------------------------------------- 1 | /*******************************************************************\ 2 | * A small, native C test suite for libweather * 3 | * Copyright (C) 2013-2014, Sam Stuewe * 4 | * * 5 | * This program is free software; you can redistribute it and/or * 6 | * modify it under the terms of the GNU General Public License * 7 | * as published by the Free Software Foundation; either version 2 * 8 | * of the License, or (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the Free Software * 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 18 | * Boston, MA 02110-1301, USA. * 19 | \*******************************************************************/ 20 | 21 | // Libraries // 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "../src/weather.h" 29 | 30 | // Forward Declarations // 31 | 32 | /** 33 | * If you fork this code, please DO NOT use this key. 34 | * Getting an API key with OWM is free, but this key is exclusively 35 | * for shaman 36 | */ 37 | #define OWMAPIKEY "83a3a133bc7541a6608536d490f7a11d" 38 | 39 | typedef bool (* test_p) (void); 40 | 41 | struct test { 42 | char * desc; 43 | test_p func; 44 | }; 45 | 46 | static struct json_write_result * json; 47 | 48 | #define APPROX_EQUALITY_EPSILON 1e-7 49 | 50 | /* Test Utilities */ 51 | bool 52 | run_test (char * test_name, test_p function); 53 | 54 | char * 55 | test_result_text (bool passed); 56 | 57 | bool 58 | approx_equal (double value1, double value2); 59 | 60 | /* Test Functions */ 61 | bool 62 | test_strfweather (void); 63 | 64 | bool 65 | test_owm_local_fetch (void); 66 | 67 | bool 68 | test_owm_local_parse (void); 69 | 70 | bool 71 | test_owm_remote_fetch (void); 72 | 73 | bool 74 | test_owm_cache (void); 75 | 76 | bool 77 | test_owm_remote_parse (void); 78 | 79 | bool 80 | test_shaman_owm (void); 81 | 82 | // Run Suite // 83 | int 84 | main (void) { 85 | 86 | struct test test_list [] = { 87 | { "Formatted Weather", (test_p )test_strfweather }, 88 | { "OWM Local Fetching", (test_p )test_owm_local_fetch }, 89 | { "OWM Local Parsing", (test_p )test_owm_local_parse }, 90 | { "OWM Remote Fetching", (test_p )test_owm_remote_fetch }, 91 | { "OWM Caching\t", (test_p )test_owm_cache }, 92 | { "OWM Remote Parsing", (test_p )test_owm_remote_parse }, 93 | { "Shaman with OWM\t", (test_p )test_shaman_owm } 94 | }; unsigned test_count = sizeof test_list / sizeof test_list[0]; 95 | 96 | unsigned failure_count = 0; 97 | for ( size_t i = 0; i < test_count; i ++ ) { 98 | bool passed = run_test(test_list[i].desc, test_list[i].func); 99 | failure_count += !passed; 100 | } 101 | 102 | bool suite_passed = (failure_count == 0); 103 | printf("\t\t\t\t\t--------\nPassed %u/%u tests\t\t\t[ %s \x1b[0m]\n", 104 | test_count - failure_count, test_count, 105 | test_result_text(suite_passed)); 106 | return !suite_passed; 107 | } 108 | 109 | bool 110 | run_test (char * test_nm, test_p test) { 111 | 112 | printf("Testing %s\t\t[ PEND ]\r", test_nm); 113 | bool passed = test(); 114 | printf("Testing %s\t\t[ %s \x1b[0m]\n", test_nm, test_result_text(passed)); 115 | return passed; 116 | } 117 | 118 | char * 119 | test_result_text(bool passed) { 120 | 121 | return (passed ? "\x1b[32mPASS" : "\x1b[31mFAIL"); 122 | } 123 | 124 | bool 125 | test_strfweather (void) { 126 | 127 | struct weather wthr = { 128 | .dt = 1402513288, 129 | .pressure = 1011, 130 | .condition = "Sky is Clear", 131 | .weather_code = 800, 132 | .clouds = 18, 133 | .temp_min = 305.15, 134 | .temp_max = 307.59, 135 | .id = 4693342, 136 | .name = "Georgetown", 137 | .country = "US", 138 | .latitude = 30.63, 139 | .longitude = -97.68, 140 | .precipitation_3h = 18, 141 | .humidity = 62, 142 | .sunrise = 1402486044, 143 | .sunset = 1402536816, 144 | .temperature = 306.35, 145 | .wind_speed = 1.54, 146 | .wind_gust = 5.14, 147 | .wind_direction = 214 148 | }; 149 | 150 | char * comp_str = "14025132881011Sky is Clear80018305.15307.594693342George" 151 | "townUS30.63-97.68186214024860441402536816306.351.545.14" 152 | "214"; 153 | 154 | char * dest_str = malloc(115); 155 | strfweather(dest_str, 115, "%a%b%c%C%d%h%H%i%I%j%l%L%p%P%s%S%t%w%W%x%X", 156 | &wthr); 157 | 158 | int test_result = strncmp(comp_str, dest_str, 114); 159 | 160 | if ( dest_str ) { free(dest_str); }; 161 | 162 | return (test_result == 0); 163 | } 164 | 165 | bool 166 | test_owm_local_fetch (void) { 167 | 168 | json = owm_fetch_local("test.json"); 169 | return (*json->data); 170 | } 171 | 172 | bool 173 | test_owm_local_parse (void) { 174 | 175 | struct weather * w = owm_read(json); 176 | int failed_test_counter = 0; 177 | 178 | if ( strcmp(w->country, "US") != 0 ) { failed_test_counter ++; }; 179 | if ( strcmp(w->name, "Georgetown") != 0 ) { failed_test_counter ++; }; 180 | if ( strcmp(w->condition, "Sky is Clear") != 0 ) { 181 | failed_test_counter ++; 182 | } 183 | 184 | if ( !approx_equal(w->latitude, 30.63) ) { failed_test_counter ++; }; 185 | if ( !approx_equal(w->longitude, -97.68) ) { failed_test_counter ++; }; 186 | if ( w->sunrise != 1402486044 ) { failed_test_counter ++; }; 187 | if ( w->sunset != 1402536816 ) { failed_test_counter ++; }; 188 | if ( w->weather_code != 800 ) { failed_test_counter ++; }; 189 | if ( !approx_equal(w->temperature, 306.35) ) { failed_test_counter ++; }; 190 | if ( !approx_equal(w->pressure, 1011) ) { failed_test_counter ++; }; 191 | if ( !approx_equal(w->temp_min, 305.15) ) { failed_test_counter ++; }; 192 | if ( !approx_equal(w->temp_max, 307.59) ) { failed_test_counter ++; }; 193 | if ( !approx_equal(w->humidity, 62) ) { failed_test_counter ++; }; 194 | if ( !approx_equal(w->wind_speed, 1.54) ) { failed_test_counter ++; }; 195 | if ( !approx_equal(w->wind_gust, 5.14) ) { failed_test_counter ++; }; 196 | if ( !approx_equal(w->wind_direction, 214) ) { failed_test_counter ++; }; 197 | if ( !approx_equal(w->precipitation_3h, 18) ) { failed_test_counter ++; }; 198 | if ( !approx_equal(w->clouds, 18) ) { failed_test_counter ++; }; 199 | if ( w->dt != 1402513288 ) { failed_test_counter ++; }; 200 | if ( w->id != 4693342 ) { failed_test_counter ++; }; 201 | 202 | return (failed_test_counter == 0); 203 | } 204 | 205 | bool 206 | test_owm_remote_fetch (void) { 207 | 208 | json = owm_fetch_remote('q', "Saint Paul,US", 'i', NULL, OWMAPIKEY); 209 | return ( *json->data ); 210 | } 211 | 212 | bool 213 | test_owm_cache (void) { 214 | 215 | char * test_path = ".cache_test.json"; 216 | json = owm_fetch_remote('q', "Saint Paul,US", 'i', test_path, OWMAPIKEY); 217 | 218 | if ( json->data ) { 219 | json = owm_fetch_local(test_path); 220 | unlink(test_path); 221 | 222 | return ( *json->data ); 223 | } return false; 224 | } 225 | 226 | bool 227 | test_owm_remote_parse (void) { 228 | 229 | struct weather * w = owm_read(json); 230 | 231 | if ( !w ) { 232 | return 1; 233 | } 234 | 235 | int failed_test_counter = 0; 236 | 237 | if ( strcmp(w->country, "US") != 0 ) { failed_test_counter ++; }; 238 | if ( strcmp(w->name, "Saint Paul") != 0 ) { failed_test_counter ++; }; 239 | if ( w->id != 5045360 ) { failed_test_counter ++; }; 240 | 241 | return (failed_test_counter == 0); 242 | } 243 | 244 | bool 245 | test_shaman_owm (void) { 246 | 247 | return 1; 248 | } 249 | 250 | bool 251 | approx_equal (double value1, double value2) { 252 | 253 | return fabs(value2 - value1) < APPROX_EQUALITY_EPSILON; 254 | } 255 | 256 | // vim: set ts=4 sw=4 et: 257 | -------------------------------------------------------------------------------- /test/test.json: -------------------------------------------------------------------------------- 1 | { 2 | "coord":{ 3 | "lon":-97.68, 4 | "lat":30.63 5 | }, 6 | "sys":{ 7 | "message":0.0344, 8 | "country":"US", 9 | "sunrise":1402486044, 10 | "sunset":1402536816 11 | }, 12 | "weather":[{ 13 | "id":800, 14 | "main":"Clear", 15 | "description":"Sky is Clear", 16 | "icon":"01d" 17 | }], 18 | "base":"cmc stations", 19 | "main":{ 20 | "temp":306.35, 21 | "pressure":1011, 22 | "temp_min":305.15, 23 | "temp_max":307.59, 24 | "humidity":62 25 | }, 26 | "wind":{ 27 | "speed":1.54, 28 | "gust":5.14, 29 | "deg":214 30 | }, 31 | "rain":{ 32 | "3h":18 33 | }, 34 | "clouds":{ 35 | "all":18 36 | }, 37 | "dt":1402513288, 38 | "id":4693342, 39 | "name":"Georgetown", 40 | "cod":200 41 | } 42 | -------------------------------------------------------------------------------- /tup.config: -------------------------------------------------------------------------------- 1 | CONFIG_VERSION=`git describe` 2 | --------------------------------------------------------------------------------