Add verbosity arguments

This commit is contained in:
Phil Gyford
2019-06-14 17:14:27 +01:00
parent 694048e3a1
commit 9b7a86426e
2 changed files with 69 additions and 9 deletions

View File

@@ -3,10 +3,12 @@
A python script that will generate an iCal (`.ics`) feed of your checkins on [Foursquare][4sq]/[Swarm][swarm]. If you set it up to save this file to a publicly-visible location on a webserver, and run the script regularly, you can subscribe to the feed in your favourite calendar application.
Foursquare [used to have such feeds][feeds] but they've stopped working for me.
[I wrote a bit about this.][blog]
[4sq]: https://foursquare.com
[swarm]: https://www.swarmapp.com
[feeds]: https://foursquare.com/feeds/
[blog]: https://www.gyford.com/phil/writing/2019/05/13/foursquare-swarm-ical-feed/
## Installation
@@ -19,11 +21,11 @@ Go to https://foursquare.com/developers/apps and create a new App.
### 2. Install python requirements
Either:
Either using [pipenv](https://pipenv.readthedocs.io/en/latest/):
$ pipenv install
or:
or [pip](https://pip.pypa.io/en/stable/):
$ pip install -r requirements.txt
@@ -80,18 +82,51 @@ You're ready to go:
$ ./generate_feeds.py
This should create an `.ics` file.
By default it only fetches the most recent 250 checkins. To fetch ALL of your
checkins add the `--all` flag:
$ ./generate_feeds.py --all
This should create an `.ics` file containing up to 250 of your most recent
checkins (see `--all` argument below to get more).
If the file is generated in a location on your website that's publicly-visible, you should be able to subscribe to it from a calendar application. Then run the script periodically to have it update.
Note that the file might contain private checkins or information you don't want to be public. In which case, it's probably best to make the name of any such publicly-readable file very obscure.
### `--all`
By default the script only fetches the most recent 250 checkins. To fetch ALL checkins add the `--all` flag:
$ ./generate_feeds.py --all
Depending on how many checkins you have you might only want to run it with
`--all` the first time and, once that's imported into a calendar application,
subsequently only fetch recent checkins.
Note that the file might contain private checkins or information you don't want to be public. In which case, it's probably best to make the name of any such publicly-readable file very obscure.
### `-v` or `--verbose`
By default the script will only output text if something goes wrong. To get
brief output use `-v` or `--verbose`:
$ ./generate_feeds.py -v
Fetched 250 checkins from the API
Generated calendar file ./mycalendar.ics
If fetching `--all` checkins then increasing the verbosity with another `-v`
will show more info than the above:
$ ./generate_feeds.py -vv --all
5746 checkins to fetch
Fetched checkins 1-250
Fetched checkins 251-500
[etc]
Fetched checkins 5501-5750
Fetched 5744 checkins from the API
Generated calendar file ./mycalendar.ics
(No I don't know why it fetched 2 fewer checkins than I have.)
## About
By Phil Gyford
phil@gyford.com
https://www.gyford.com
https://github.com/philgyford/foursquare-feeds

View File

@@ -48,11 +48,16 @@ class FeedGenerator:
else:
checkins = self._get_recent_checkins()
plural = "" if len(checkins) == 1 else "s"
logger.info("Fetched {} checkin{} from the API".format(len(checkins), plural))
calendar = self._generate_calendar(checkins)
with open(self.ics_filepath, "w") as f:
f.writelines(calendar)
logger.info("Generated calendar file {}".format(self.ics_filepath))
exit(0)
def _get_recent_checkins(self):
@@ -68,11 +73,16 @@ class FeedGenerator:
total_checkins = 9999999999
while offset < total_checkins:
results = self._get_checkins_from_api(offset)
if offset == 0:
# First time, set the correct total:
total_checkins = results["checkins"]["count"]
plural = "" if total_checkins == 1 else "s"
logger.debug("{} checkin{} to fetch".format(total_checkins, plural))
logger.debug("Fetched {}-{}".format((offset + 1), (offset + 250)))
checkins += results["checkins"]["items"]
offset += 250
@@ -199,8 +209,23 @@ if __name__ == "__main__":
default=False,
)
parser.add_argument(
"-v",
"--verbose",
action="count",
help="-v or --verbose for brief output; -vv for more.",
required=False,
)
args = parser.parse_args()
if args.verbose == 1:
logger.setLevel(logging.INFO)
elif args.verbose == 2:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.WARNING)
if args.all:
to_fetch = "all"
else: