bear / python-twitter

A Python wrapper around the Twitter API.
Apache License 2.0
3.41k stars 957 forks source link

Working with `urllib.parse.quote` #608

Closed csbrown closed 5 years ago

csbrown commented 5 years ago

Per the docs, we can use this to perform a raw query search:

results = api.GetSearch(
    raw_query="q=twitter%20&result_type=recent&since=2014-07-19&count=100")

However, sometimes it is necessary to url encode the various weird symbols that go into a query. Python provides a handy function for this, urllib.parse.quote. However, it does not play well with api.GetSearch:

results = api.GetSearch(
        raw_query=urllib.parse.quote("q=twitter%20&result_type=recent&since=2014-07-19&count=100"))

gives

twitter.error.TwitterError: [{'code': 25, 'message': 'Query parameters are missing.'}]

How do I get the api to play well with url encoded parameters?

jeremylow commented 5 years ago

urllib.parse.quote will encode the &s as %26s, so you could use something like:

urllib.parse.quote("q=twitter&result_type=recent&since=2014-07-19&count=100", safe='&=/%')

However, if you have a & in the query string (like, "twi&tter" (not sure why you'd want to but it's possible)), then that would get passed unquoted and mess up the search.

I think the best option would be to construct a dictionary like this:

raw_query_params = {
    'q': 'El Niño',
    'result_type': 'recent',
    'since': '2019-03-07',
    'count': 100
}

results = api.GetSearch(raw_query=urllib.parse.urlencode(raw_query_params))
# constructs the following string:
# 'q=El+Ni%C3%B1o&result_type=recent&since=2019-03-07&count=100'