Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active June 21, 2020 17:46
Show Gist options
  • Save rcook/dfb7cbe66db11f66425250f9b6c451fd to your computer and use it in GitHub Desktop.
Save rcook/dfb7cbe66db11f66425250f9b6c451fd to your computer and use it in GitHub Desktop.
Twurl on Windows
twurl "/1.1/search/tweets.json?q=nasa&result_type=popular"
twurl "/1.1/search/tweets.json?q=nasa&result_type=popular" > nasa.json
#!/usr/bin/env python2
import subprocess
import json
import urllib
import urlparse
# twurl must have already been authorized using the "authorize" subcommand
TWURL_COMMAND = ["twurl"]
def make_url(url, qs):
qs_str = urllib.urlencode(qs)
return url if len(qs_str) == 0 else "{}?{}".format(url, qs_str)
def search(url, qs):
statuses = []
while True:
full_url = make_url(url, qs)
command = TWURL_COMMAND + [full_url]
obj = json.loads(subprocess.check_output(command))
statuses.extend(obj["statuses"])
search_metadata = obj["search_metadata"]
next_results = search_metadata.get("next_results")
if next_results is None:
break
qs = urlparse.parse_qsl(next_results[1:])
return statuses
statuses = search("/1.1/search/tweets.json", [
("q", "nasa"),
("result_type", "popular")
])
print(json.dumps(statuses, indent=2))
The MIT License (MIT)
Copyright (c) 2018 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import json
with open("nasa.json", "rt") as f:
obj = json.loads(f.read())
print(json.dumps(obj, indent=2))
require 'json'
obj = JSON.parse(File.read('nasa.json'))
puts JSON.pretty_generate(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment