diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 58e178d..0000000 --- a/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -MANIFEST - -*.py[cod] - -# C extensions -*.so - -# Packages -*.egg -*.egg-info -dist -build -eggs -parts -bin -var -sdist -develop-eggs -.installed.cfg -lib -lib64 - -# Installer logs -pip-log.txt - -# Unit test / coverage reports -.coverage -.tox -nosetests.xml - -# Translations -*.mo - -# Mr Developer -.mr.developer.cfg -.project -.pydevproject diff --git a/TwitterAPI/BearerAuth.py b/BearerAuth.py similarity index 100% rename from TwitterAPI/BearerAuth.py rename to BearerAuth.py diff --git a/CHANGES.txt b/CHANGES.txt deleted file mode 100644 index 7373370..0000000 --- a/CHANGES.txt +++ /dev/null @@ -1,115 +0,0 @@ -v0.1.0, 24 Nov 2012 -- Initial release. - -v0.1.1, 26 Nov 2012 -- Added twitterapi.test. New error handling. - -v0.1.2, 28 Nov 2012 -- Imporved error and quota notification. - -v0.1.3, 02 Dec 2012 -- Added TwSearch.past_results() for paging old tweets. - -v0.1.4, 02 Dec 2012 -- Added wait argument to TwSearch.past_results(). - -v0.1.6, 03 Dec 2012 -- Added code comments. - -v0.1.7, 10 Dec 2012 -- Added TwTrends. - -v0.1.9, 14 Dec 2012 -- Added read_file to TwCredentials. - Moved OAuth credentials out of test scripts and into credentials.txt. - -v0.1.10, 15 Dec 2012 -- Added socket timeout to TwStream. - Added new_results to TwSearch to search new tweets. - Added tools sub-folder containing a few useful scripts. - -v1.0.0, 30 Jan 2013 -- Uploaded to github. - -v2.0.0, 07 Jun 2013 -- Switched to requests module. - -v2.0.1, 10 Jun 2013 -- Updated documentation. - -v2.0.4, 16 Jun 2013 -- Works with python 3. - -v2.0.6, 25 Jun 2013 -- Fixed printing. - -v2.0.7, 06 Aug 2013 -- TwitterRestPager does not exit when no tweets. - -v2.0.8, 22 Aug 2013 -- Renamed TwitterOauth.py to TwitterOAuth.py. - -v2.0.9, 22 Aug 2013 -- Added flush to cli.py for python3 buffered stdout. - -v2.1.0, 16 Sep 2013 -- New class TwitterResponse. TwitterAPI no longer saves state. - -v2.1.1, 23 Sep 2013 -- Added sphinx-generated docs; added tags. - -v2.1.3, 07 Oct 2013 -- Fix circular imports in setup.py. - -v2.1.4, 07 Oct 2013 -- Fix endpoints with params in path. - -v2.1.5, 07 Oct 2013 -- Better timing in TwitterRestPager. - -v2.1.6, 07 Oct 2013 -- cli upports UTF-8 printing to console. - -v2.1.7, 07 Oct 2013 -- Fix endpoints with params in path. - -v2.1.8, 27 Dec 2013 -- Added OAuth 2.0 support. - -v2.1.9, 30 Jan 2014 -- Added image posting support. - -v2.1.10, 31 Jan 2014 -- Added proxy server support. - -v2.1.11, 22 Mar 2014 -- Fixed datetime import and oauth_test.py. - -v2.1.12, 22 Mar 2014 -- Ran code against autopep8. - -v2.1.13, 24 Mar 2014 -- Decreased streaming buffer size to 1. - -v2.2.0, 22 Jun 2014 -- Added indent option to cli.py. - -v2.2.3, 26 Aug 2014 -- Fix POST request for query string. - -v2.2.4, 25 Sep 2014 -- Merged tangentmonger-alternative branch with cleaner iterator syntax. - -v2.2.5, 25 Sep 2014 -- Added sphinx docs builder. - -v2.2.6, 12 Nov 2014 -- Silenced ValueError for streaming endpoints. - Fixed cli.py field search for streaming endpoints. - Updated constants.py with new endpoints - media/upload has new example. - Streaming always uses 'delimited=length'. - -v2.2.7, 12 Nov 2014 -- Fix streaming delimited. - -v2.2.9, 12 Nov 2014 -- Merged dilmerv fix in constants.py. - -v2.2.8, 12 Nov 2014 -- Fix error in BearerAuth.py for OAuth 2. - -v2.3.0, 10 Feb 2015 -- Added logging, error classes, stall warning for streams, - _RestIterable can iterate 'users' and 'ids'. - -v2.3.1, 18 Feb 2015 -- Changed 'is' to '==' for string comparison. - -v2.3.3, 21 Mar 2015 -- Merged revinewaters fix in TwitterError.py. - -v2.3.5, 28 Oct 2015 -- Removed deprecated classes RestIterator, StreamingIterator. - -v2.3.6, 20 Dec 2015 -- Added support in TwitterRestPager for endpoints with cursors. - -v2.4.0, 23 Jan 2016 -- Added support for Curator API and Collections API. - -v2.4.2, 14 Aug 2016 -- Merged karlicoss. Added method_override to request. - -v2.4.3, 29 Dec 2016 -- Create request session inside a with block. - -v2.4.4, 29 Dec 2016 -- Merged Socialery patch. Added optional arguments to json() method. - -v2.4.5, 25 Feb 2017 -- Merged NeilCTurner patch. Added statuses/unretweet API endpoint. - -v2.4.6, 16 Jul 2017 -- Merged RickRedSix patch. Added direct message endpoints. - -v2.4.7, 22 Oct 2017 -- Added endpoints for Ads API. - -v2.4.8, 03 Feb 2018 -- Merged RickRedSix patch. Added account activity endpoints. - -v2.4.9, 03 Feb 2018 -- Merged torufurukawa patch. Added webhook endpoint. - -v2.4.10, 11 Mar 2018 -- Merged torufurukawa patch. Added direct message endpoint. - -v2.5.0, 15 Mar 2018 -- Renamed get_rest_quota() to get_quota(). - Renamed TwitterRestPager to TwitterPager. diff --git a/README.rst b/README.md old mode 100755 new mode 100644 similarity index 80% rename from README.rst rename to README.md index 211d9a0..acbfc2d --- a/README.rst +++ b/README.md @@ -1,15 +1,9 @@ -|LOGO| -============================ -|BADGE_VERSION| |BADGE_CHAT| -============================ - -.. |LOGO| image:: https://raw.githubusercontent.com/geduldig/TwitterAPI/master/logo.png -.. |BADGE_VERSION| image:: http://img.shields.io/pypi/v/TwitterAPI.svg - :target: https://crate.io/packages/TwitterAPI -.. |BADGE_CHAT| image:: https://badges.gitter.im/Join%20Chat.svg - :alt: Join the chat at https://gitter.im/geduldig/TwitterAPI - :target: https://gitter.im/geduldig/TwitterAPI?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge - +This is a modified version of TwitterAPI, to be used by [TwitterMine](https://github.com/jonahar/TwitterMine). +**Disclaimer**: some files from the original repository were removed, so don't count on this README examples and instructions. + + + + TwitterAPI is a Python package for accessing Twitter's REST APIs and Streaming APIs. It supports OAuth 1.0 and OAuth 2.0 authentication. And, it works with the latest Python versions in both 2.x and 3.x branches. Installation diff --git a/TODO.txt b/TODO.txt deleted file mode 100644 index 5ee980e..0000000 --- a/TODO.txt +++ /dev/null @@ -1,4 +0,0 @@ -Build test suite with unittest. -(If anyone wants to contribute, would be much appreciated.) - -Test Ads API endpoints. diff --git a/TwitterAPI/TwitterAPI.py b/TwitterAPI.py similarity index 89% rename from TwitterAPI/TwitterAPI.py rename to TwitterAPI.py index 7f9cc6b..e0e4436 100644 --- a/TwitterAPI/TwitterAPI.py +++ b/TwitterAPI.py @@ -2,7 +2,6 @@ __date__ = "June 7, 2013" __license__ = "MIT" - from .BearerAuth import BearerAuth as OAuth2 from .constants import * from datetime import datetime @@ -18,7 +17,6 @@ class TwitterAPI(object): - """Access REST API or Streaming API resources. :param consumer_key: Twitter application consumer key @@ -65,11 +63,11 @@ def _prepare_url(self, subdomain, path): CURATOR_VERSION, path) elif subdomain == 'ads-api': - return '%s://%s.%s/%s/%s' % (PROTOCOL, - subdomain, - DOMAIN, - ADS_VERSION, - path) + return '%s://%s.%s/%s/%s' % (PROTOCOL, + subdomain, + DOMAIN, + ADS_VERSION, + path) else: return '%s://%s.%s/%s/%s.json' % (PROTOCOL, subdomain, @@ -132,7 +130,7 @@ def request(self, resource, params=None, files=None, method_override=None): url, data=data, params=params, - timeout=(CONNECTION_TIMEOUT,timeout), + timeout=(CONNECTION_TIMEOUT, timeout), files=files, proxies=self.proxies) except (ConnectionError, ProtocolError, ReadTimeout, ReadTimeoutError, @@ -142,14 +140,13 @@ def request(self, resource, params=None, files=None, method_override=None): class TwitterResponse(object): - """Response from either a REST API or Streaming API resource call. :param response: The requests.Response object returned by the API call :param stream: Boolean connection type (True if a streaming connection) """ - def __init__(self, response, stream): + def __init__(self, response: requests.Session.request, stream: bool): self.response = response self.stream = stream @@ -184,7 +181,14 @@ def get_iterator(self): :raises: TwitterConnectionError, TwitterRequestError """ if self.response.status_code != 200: - raise TwitterRequestError(self.response.status_code) + msg = None + try: + js = self.json() + if 'errors' in js: + msg = js['errors'][0]['message'] + except ValueError as e: + pass + raise TwitterRequestError(self.response.status_code, msg) if self.stream: return iter(_StreamingIterable(self.response)) @@ -205,14 +209,10 @@ def get_quota(self): :returns: Dictionary of 'remaining' (count), 'limit' (count), 'reset' (time) """ remaining, limit, reset = None, None, None - if self.response: - if 'x-rate-limit-remaining' in self.response.headers: - remaining = int( - self.response.headers['x-rate-limit-remaining']) - if remaining == 0: - limit = int(self.response.headers['x-rate-limit-limit']) - reset = int(self.response.headers['x-rate-limit-reset']) - reset = datetime.fromtimestamp(reset) + if 'x-rate-limit-remaining' in self.response.headers: + remaining = int(self.response.headers['x-rate-limit-remaining']) + limit = int(self.response.headers['x-rate-limit-limit']) + reset = int(self.response.headers['x-rate-limit-reset']) return {'remaining': remaining, 'limit': limit, 'reset': reset} def close(self): @@ -221,7 +221,6 @@ def close(self): class _RestIterable(object): - """Iterate statuses, errors or other iterable objects in a REST API response. :param response: The request.Response from a Twitter REST API request @@ -255,13 +254,12 @@ def __iter__(self): class _StreamingIterable(object): - """Iterate statuses or other objects in a Streaming API response. :param response: The request.Response from a Twitter Streaming API request """ - def __init__(self, response): + def __init__(self, response: requests.Session.request): self.stream = response.raw def _iter_stream(self): @@ -294,6 +292,7 @@ def _iter_stream(self): item = None item = self.stream.read(nbytes) break + yield item except (ConnectionError, ProtocolError, ReadTimeout, ReadTimeoutError, SSLError, ssl.SSLError, socket.error) as e: @@ -315,3 +314,7 @@ def __iter__(self): except ValueError as e: # invalid JSON string (possibly an unformatted error message) raise TwitterConnectionError(e) + else: + # raise an empty result, so whoever is using this iterator will not wait + # indefinitely, and will have the chance to stop. + yield dict() diff --git a/TwitterAPI/credentials.txt b/TwitterAPI/credentials.txt deleted file mode 100644 index e50b2c6..0000000 --- a/TwitterAPI/credentials.txt +++ /dev/null @@ -1,4 +0,0 @@ -consumer_key= -consumer_secret= -access_token_key= -access_token_secret= diff --git a/TwitterAPI/TwitterError.py b/TwitterError.py similarity index 83% rename from TwitterAPI/TwitterError.py rename to TwitterError.py index 23010d8..c79c138 100644 --- a/TwitterAPI/TwitterError.py +++ b/TwitterError.py @@ -2,18 +2,15 @@ __date__ = "November 30, 2014" __license__ = "MIT" - import logging class TwitterError(Exception): - """Base class for Twitter exceptions""" pass class TwitterConnectionError(TwitterError): - """Raised when the connection needs to be re-established""" def __init__(self, value): @@ -22,17 +19,16 @@ def __init__(self, value): class TwitterRequestError(TwitterError): - """Raised when request fails""" - def __init__(self, status_code): - if status_code >= 500: + def __init__(self, status_code, msg=None): + if status_code >= 500 and msg is None: msg = 'Twitter internal error (you may re-try)' - else: + elif msg is None: msg = 'Twitter request failed' - logging.info('Status code %d: %s' % (status_code, msg)) super(Exception, self).__init__(msg) + self.msg = msg self.status_code = status_code - + def __str__(self): return '%s (%d)' % (self.args[0], self.status_code) diff --git a/TwitterAPI/TwitterOAuth.py b/TwitterOAuth.py similarity index 100% rename from TwitterAPI/TwitterOAuth.py rename to TwitterOAuth.py diff --git a/TwitterAPI/TwitterPager.py b/TwitterPager.py similarity index 72% rename from TwitterAPI/TwitterPager.py rename to TwitterPager.py index 4ff5ba6..10519e4 100644 --- a/TwitterAPI/TwitterPager.py +++ b/TwitterPager.py @@ -2,16 +2,16 @@ __date__ = "June 8, 2013" __license__ = "MIT" - from requests.exceptions import ConnectionError, ReadTimeout, SSLError from requests.packages.urllib3.exceptions import ReadTimeoutError, ProtocolError from .TwitterError import * import requests import time +RATE_LIMIT_CODE = 88 -class TwitterPager(object): +class TwitterPager(object): """Continuous (stream-like) pagination of response from Twitter REST API resource. :param api: An authenticated TwitterAPI object @@ -24,12 +24,13 @@ def __init__(self, api, resource, params=None): self.resource = resource self.params = params - def get_iterator(self, wait=5, new_tweets=False): + def get_iterator(self, wait=0, new_tweets=False): """Iterate response from Twitter REST API resource. Resource is called in a loop to retrieve consecutive pages of results. - :param wait: Floating point number (default=5) of seconds wait between requests. + :param wait: Floating point number (default=0) of seconds wait between requests. Depending on the resource, appropriate values are 5 or 60 seconds. + UPDATE: the default was changed to 0 because the pager now handles rate limit :param new_tweets: Boolean determining the search direction. False (default) retrieves old results. True retrieves current results. @@ -43,6 +44,18 @@ def get_iterator(self, wait=5, new_tweets=False): # get one page of results start = time.time() r = self.api.request(self.resource, self.params) + json = r.json() + + # if rate limit reached, go to sleep + if 'errors' in json and json['errors'][0]['code'] == RATE_LIMIT_CODE: + reset = r.get_quota()['reset'] + 5 # plus 5 seconds just to be sure + time_to_wait = max(int(reset - time.time()), 0) + logging.info( + 'Rate limit reached for endpoint {0}. Going to sleep for {1} seconds'.format( + self.resource, time_to_wait)) + time.sleep(time_to_wait) + continue # go to the beginning of the loop. try the request again + it = r.get_iterator() if new_tweets: it = reversed(list(it)) @@ -50,17 +63,17 @@ def get_iterator(self, wait=5, new_tweets=False): # yield each item in the page id = None for item in it: - if 'id' in item: - id = item['id'] - if 'code' in item: - if item['code'] in [130, 131]: - # Twitter service error - raise TwitterConnectionError(item) + if type(item) == dict: + if 'id' in item: + id = item['id'] + if 'code' in item: + if item['code'] in [130, 131]: + # Twitter service error + raise TwitterConnectionError(item) yield item # if a cursor is present, use it to get next page # (otherwise, use id to get next page) - json = r.json() cursor = -1 if new_tweets and 'previous_cursor' in json: cursor = json['previous_cursor'] diff --git a/TwitterAPI/__init__.py b/__init__.py similarity index 91% rename from TwitterAPI/__init__.py rename to __init__.py index 5f643f0..094dfa4 100644 --- a/TwitterAPI/__init__.py +++ b/__init__.py @@ -13,7 +13,7 @@ try: - from .TwitterAPI import TwitterAPI, TwitterResponse + from TwitterAPI import TwitterAPI, TwitterResponse from .TwitterError import TwitterConnectionError, TwitterRequestError from .TwitterOAuth import TwitterOAuth from .TwitterPager import TwitterPager diff --git a/TwitterAPI/constants.py b/constants.py similarity index 100% rename from TwitterAPI/constants.py rename to constants.py diff --git a/docs/Makefile b/docs/Makefile deleted file mode 100644 index aa3d5d8..0000000 --- a/docs/Makefile +++ /dev/null @@ -1,177 +0,0 @@ -# Makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -PAPER = -BUILDDIR = _build - -# User-friendly check for sphinx-build -ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) -$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) -endif - -# Internal variables. -PAPEROPT_a4 = -D latex_paper_size=a4 -PAPEROPT_letter = -D latex_paper_size=letter -ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . -# the i18n builder cannot share the environment and doctrees with the others -I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . - -.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext - -help: - @echo "Please use \`make ' where is one of" - @echo " html to make standalone HTML files" - @echo " dirhtml to make HTML files named index.html in directories" - @echo " singlehtml to make a single large HTML file" - @echo " pickle to make pickle files" - @echo " json to make JSON files" - @echo " htmlhelp to make HTML files and a HTML help project" - @echo " qthelp to make HTML files and a qthelp project" - @echo " devhelp to make HTML files and a Devhelp project" - @echo " epub to make an epub" - @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" - @echo " latexpdf to make LaTeX files and run them through pdflatex" - @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" - @echo " text to make text files" - @echo " man to make manual pages" - @echo " texinfo to make Texinfo files" - @echo " info to make Texinfo files and run them through makeinfo" - @echo " gettext to make PO message catalogs" - @echo " changes to make an overview of all changed/added/deprecated items" - @echo " xml to make Docutils-native XML files" - @echo " pseudoxml to make pseudoxml-XML files for display purposes" - @echo " linkcheck to check all external links for integrity" - @echo " doctest to run all doctests embedded in the documentation (if enabled)" - -clean: - rm -rf $(BUILDDIR)/* - -html: - $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." - -dirhtml: - $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." - -singlehtml: - $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml - @echo - @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." - -pickle: - $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle - @echo - @echo "Build finished; now you can process the pickle files." - -json: - $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json - @echo - @echo "Build finished; now you can process the JSON files." - -htmlhelp: - $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp - @echo - @echo "Build finished; now you can run HTML Help Workshop with the" \ - ".hhp project file in $(BUILDDIR)/htmlhelp." - -qthelp: - $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp - @echo - @echo "Build finished; now you can run "qcollectiongenerator" with the" \ - ".qhcp project file in $(BUILDDIR)/qthelp, like this:" - @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/TwitterAPI.qhcp" - @echo "To view the help file:" - @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/TwitterAPI.qhc" - -devhelp: - $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp - @echo - @echo "Build finished." - @echo "To view the help file:" - @echo "# mkdir -p $$HOME/.local/share/devhelp/TwitterAPI" - @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/TwitterAPI" - @echo "# devhelp" - -epub: - $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub - @echo - @echo "Build finished. The epub file is in $(BUILDDIR)/epub." - -latex: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo - @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." - @echo "Run \`make' in that directory to run these through (pdf)latex" \ - "(use \`make latexpdf' here to do that automatically)." - -latexpdf: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through pdflatex..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -latexpdfja: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through platex and dvipdfmx..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -text: - $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text - @echo - @echo "Build finished. The text files are in $(BUILDDIR)/text." - -man: - $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man - @echo - @echo "Build finished. The manual pages are in $(BUILDDIR)/man." - -texinfo: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo - @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." - @echo "Run \`make' in that directory to run these through makeinfo" \ - "(use \`make info' here to do that automatically)." - -info: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo "Running Texinfo files through makeinfo..." - make -C $(BUILDDIR)/texinfo info - @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." - -gettext: - $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale - @echo - @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." - -changes: - $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes - @echo - @echo "The overview file is in $(BUILDDIR)/changes." - -linkcheck: - $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck - @echo - @echo "Link check complete; look for any errors in the above output " \ - "or in $(BUILDDIR)/linkcheck/output.txt." - -doctest: - $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest - @echo "Testing of doctests in the sources finished, look at the " \ - "results in $(BUILDDIR)/doctest/output.txt." - -xml: - $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml - @echo - @echo "Build finished. The XML files are in $(BUILDDIR)/xml." - -pseudoxml: - $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml - @echo - @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/docs/README.rst b/docs/README.rst deleted file mode 100644 index 9dc69ae..0000000 --- a/docs/README.rst +++ /dev/null @@ -1,24 +0,0 @@ -Instructions for Generating Documentation with Sphinx -===================================================== - -MAKE NEW DOCS IN MASTER BRANCH:: - - clone git@github.com:geduldig/TwitterAPI.git - cd TwitterAPI/docs - make html - -DOWNLOAD OLD DOCS FROM GH-PAGES BRANCH:: - - cd TwitterAPI-docs - clone -b gh-pages git@github.com:geduldig/TwitterAPI.git - -COPY NEW DOCS OVER OLD DOCS:: - - cp TwitterAPI/docs/_build/html/*.* TwitterAPI-docs/TwitterAPI - cp -r TwitterAPI/docs/_build/html/* TwitterAPI-docs/TwitterAPI - rm -r TwitterAPI/docs/_build - -UPLOAD NEW DOCS TO GH-PAGES BRANCH::: - - git commit -am "2.5 docs" - git push origin gh-pages diff --git a/docs/authentication.rst b/docs/authentication.rst deleted file mode 100644 index 8af0979..0000000 --- a/docs/authentication.rst +++ /dev/null @@ -1,40 +0,0 @@ -Authentication -============== - -Before you can do anything you must create an application on `apps.twitter.com `_ and generate oAuth keys. - -Twitter supports both user and application authentication, called oAuth 1 and oAuth 2, respectively. User authentication gives you access to all API endpoints, basically read and write persmission. It is also required in order to using the Streaming API. Application authentication gives you access to just the read portion of the API -- so, no creating or destroying tweets. Application authentication, however, has elevated rate limits. - -User Authentication -------------------- - -.. code-block:: python - - api = TwitterAPI(consumer_key, - consumer_secret, - access_token_key, - access_token_secret) - -Application Authentication --------------------------- - -.. code-block:: python - - api = TwitterAPI(consumer_key, - consumer_secret, - auth_type='oAuth2') - -Proxy Server Authentication ---------------------------- - -If you are behind a firewall, you may also need to authenticate with a web proxy server in order to reach Twitter's servers. For this situation you include an additional argument in the initializer: - -.. code-block:: python - - api = TwitterAPI(consumer_key, - consumer_secret, - access_token_key, - access_token_secret, - proxy_url='https://USER:PASSWORD@SERVER:PORT') - -Replace SERVER:PORT with your proxy server, and replace USER and PASSWORD with your proxy server credentials. \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py deleted file mode 100644 index 5f3d833..0000000 --- a/docs/conf.py +++ /dev/null @@ -1,262 +0,0 @@ -# -*- coding: utf-8 -*- -# -# TwitterAPI documentation build configuration file, created by -# sphinx-quickstart on Sat Sep 27 12:02:24 2014. -# -# This file is execfile()d with the current directory set to its -# containing dir. -# -# Note that not all possible configuration values are present in this -# autogenerated file. -# -# All configuration values have a default; values that are commented out -# serve to show the default. - -import sys -import os - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, '../') - -# -- General configuration ------------------------------------------------ - -# If your documentation needs a minimal Sphinx version, state it here. -#needs_sphinx = '1.0' - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ - 'sphinx.ext.autodoc', -] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# The suffix of source filenames. -source_suffix = '.rst' - -# The encoding of source files. -#source_encoding = 'utf-8-sig' - -# The master toctree document. -master_doc = 'index' - -# General information about the project. -project = u'TwitterAPI' -copyright = u'2014, geduldig' - -# The version info for the project you're documenting, acts as replacement for -# |version| and |release|, also used in various other places throughout the -# built documents. -# -# The short X.Y version. -version = '2.5' -# The full version, including alpha/beta/rc tags. -release = '2.5' - -# The language for content autogenerated by Sphinx. Refer to documentation -# for a list of supported languages. -#language = None - -# There are two options for replacing |today|: either, you set today to some -# non-false value, then it is used: -#today = '' -# Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -exclude_patterns = ['_build'] - -# The reST default role (used for this markup: `text`) to use for all -# documents. -#default_role = None - -# If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True - -# If true, the current module name will be prepended to all description -# unit titles (such as .. function::). -#add_module_names = True - -# If true, sectionauthor and moduleauthor directives will be shown in the -# output. They are ignored by default. -#show_authors = False - -# The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - -# A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] - -# If true, keep warnings as "system message" paragraphs in the built documents. -#keep_warnings = False - - -# -- Options for HTML output ---------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -html_theme = 'haiku' - -# Theme options are theme-specific and customize the look and feel of a theme -# further. For a list of options available for each theme, see the -# documentation. -#html_theme_options = {} - -# Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] - -# The name for this set of Sphinx documents. If None, it defaults to -# " v documentation". -#html_title = None - -# A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None - -# The name of an image file (relative to this directory) to place at the top -# of the sidebar. -#html_logo = None - -# The name of an image file (within the static path) to use as favicon of the -# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 -# pixels large. -#html_favicon = None - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] - -# Add any extra paths that contain custom files (such as robots.txt or -# .htaccess) here, relative to this directory. These files are copied -# directly to the root of the documentation. -#html_extra_path = [] - -# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, -# using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' - -# If true, SmartyPants will be used to convert quotes and dashes to -# typographically correct entities. -#html_use_smartypants = True - -# Custom sidebar templates, maps document names to template names. -#html_sidebars = {} - -# Additional templates that should be rendered to pages, maps page names to -# template names. -#html_additional_pages = {} - -# If false, no module index is generated. -#html_domain_indices = True - -# If false, no index is generated. -#html_use_index = True - -# If true, the index is split into individual pages for each letter. -#html_split_index = False - -# If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True - -# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True - -# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True - -# If true, an OpenSearch description file will be output, and all pages will -# contain a tag referring to it. The value of this option must be the -# base URL from which the finished HTML is served. -#html_use_opensearch = '' - -# This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None - -# Output file base name for HTML help builder. -htmlhelp_basename = 'TwitterAPIdoc' - - -# -- Options for LaTeX output --------------------------------------------- - -latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - #'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - #'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - #'preamble': '', -} - -# Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, -# author, documentclass [howto/manual]). -latex_documents = [ - ('index', 'TwitterAPI.tex', u'TwitterAPI Documentation', - u'geduldig', 'manual'), -] - -# The name of an image file (relative to this directory) to place at the top of -# the title page. -#latex_logo = None - -# For "manual" documents, if this is true, then toplevel headings are parts, -# not chapters. -#latex_use_parts = False - -# If true, show page references after internal links. -#latex_show_pagerefs = False - -# If true, show URL addresses after external links. -#latex_show_urls = False - -# Documents to append as an appendix to all manuals. -#latex_appendices = [] - -# If false, no module index is generated. -#latex_domain_indices = True - - -# -- Options for manual page output --------------------------------------- - -# One entry per manual page. List of tuples -# (source start file, name, description, authors, manual section). -man_pages = [ - ('index', 'twitterapi', u'TwitterAPI Documentation', - [u'geduldig'], 1) -] - -# If true, show URL addresses after external links. -#man_show_urls = False - - -# -- Options for Texinfo output ------------------------------------------- - -# Grouping the document tree into Texinfo files. List of tuples -# (source start file, target name, title, author, -# dir menu entry, description, category) -texinfo_documents = [ - ('index', 'TwitterAPI', u'TwitterAPI Documentation', - u'geduldig', 'TwitterAPI', 'One line description of project.', - 'Miscellaneous'), -] - -# Documents to append as an appendix to all manuals. -#texinfo_appendices = [] - -# If false, no module index is generated. -#texinfo_domain_indices = True - -# How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' - -# If true, do not generate a @detailmenu in the "Top" node's menu. -#texinfo_no_detailmenu = False - -html_show_sourcelink = False diff --git a/docs/errors.rst b/docs/errors.rst deleted file mode 100644 index 04471c0..0000000 --- a/docs/errors.rst +++ /dev/null @@ -1,37 +0,0 @@ -Error Handling -============== - -Besides tweet statuses, the REST API and Streaming API iterators can return errors and other messages. It is up to the application to test what type of object has been returned. Message types are documented `here `_ and `here `_. - -REST API Messages ------------------ - -REST API endpoints can return many more types of messages than Streaming API endpoints. Depending on the endpoint, you may want to handle a particular type of message, such as exceeding a rate limit or posting a duplicate tweet. Here is a general pattern for simply printing out any message and error code: - -.. code-block:: python - - r = api.request('search/tweets', {'q':'pizza'}) - for item in r.get_iterator(): - if 'text' in item: - print item['text'] - elif 'message' in item: - print '%s (%d)' % (item['message'], item['code']) - -Streaming API Messages ----------------------- - -Streaming API endpoints return a variety of messages. Some are not errors. For example, a "limit" message contains the number of tweets missing from the stream. This happens when the number of tweets matching your filter exceeds a threshold set by Twitter. Other useful messages are "disconnect" and "delete". - -.. code-block:: python - - r = api.request('statuses/filter', {'track':'pizza'}) - for item in r.get_iterator(): - if 'text' in item: - print item['text'] - elif 'limit' in item: - print '%d tweets missed' % item['limit']['track'] - elif 'disconnect' in item: - print 'disconnecting because %s' % item['disconnect']['reason'] - break - -Even if you are not interested in handling errors it is necessary to test that the object returned by an iterator is a valid tweet status before using the object. Valid tweet objects have a 'text' property (or a 'full_text' property if it is an extended tweet). \ No newline at end of file diff --git a/docs/examples.rst b/docs/examples.rst deleted file mode 100644 index 93d44af..0000000 --- a/docs/examples.rst +++ /dev/null @@ -1,76 +0,0 @@ -Tiny Examples -============= - -All the examples assume `api` is an authenticated instance of `TwitterAPI <./twitterapi.html>`_. Typically, this is done as follows: - -.. code-block:: python - - api = TwitterAPI(consumer_key, - consumer_secret, - access_token_key, - access_token_secret) - - -Get your last 50 tweets ------------------------ - -.. code-block:: python - - r = api.request('statuses/home_timeline', {'count':50}) - for item in r.get_iterator(): - if 'text' in item: - print item['text'] - -Get your entire timeline ------------------------- - -.. code-block:: python - - pager = TwitterPager(api, 'statuses/home_timeline', {'count':200}) - for item in pager.get_iterator(wait=60): - if 'text' in item: - print item['text'] - -Post a tweet ------------- - -.. code-block:: python - - r = api.request('statuses/update', {'status': 'I need pizza!'}) - print 'SUCCESS' if r.status_code == 200 else 'FAILURE' - -Post a tweet with a picture ---------------------------- - -.. code-block:: python - - # STEP 1 - upload image - file = open('./image_of_pizza.png', 'rb') - data = file.read() - r = api.request('media/upload', None, {'media': data}) - print('UPLOAD MEDIA SUCCESS' if r.status_code == 200 else 'UPLOAD MEDIA FAILURE') - - # STEP 2 - post tweet with reference to uploaded image - if r.status_code == 200: - media_id = r.json()['media_id'] - r = api.request('statuses/update', {'status':'I found pizza!', 'media_ids':media_id}) - print('UPDATE STATUS SUCCESS' if r.status_code == 200 else 'UPDATE STATUS FAILURE') - -Delete a tweet --------------- - -.. code-block:: python - - r = api.request('statuses/destroy/:%d' % TWEET_ID) - print 'SUCCESS' if r.status_code == 200 else 'FAILURE' - - -Stream tweets -------------- - -.. code-block:: python - - r = api.request('statuses/filter', {'track':'pizza'}) - for item in r.get_iterator(): - if 'text' in item: - print item['text'] diff --git a/docs/faulttolerance.rst b/docs/faulttolerance.rst deleted file mode 100644 index f8059a3..0000000 --- a/docs/faulttolerance.rst +++ /dev/null @@ -1,61 +0,0 @@ -Fault Tolerant Streams and Pages -================================ - -There are a number of reasons for a stream to stop. Twitter will break your connection if you have more than two streams open with the same credentials, or if your credentials are not valid. Occassionally, the problem will be internal to Twitter and you will be disconnected. Other causes might be network instability or connection timeout. - -Endless Stream --------------- - -In order to keep a Streaming API request going indefinitely, you will need to re-make the request whenever the connection drops. TwitterAPI defines two exception classes for this purpose. - -`TwitterRequestError <./twittererror.html>`_ is thrown whenever the request fails (i.e. when the response status code is not 200). A status code of 500 or higher indicates a server error which is safe to ignore. Any other status code indicates an error with your request which you should fix before re-trying. - -`TwitterConnectionError <./twittererror.html>`_ is thrown when the connection times out or is interrupted. You can always immediately try making the request again. - -Sometimes Twitter will inform you to close the connection by sending you a "disconnect" message. The message will contain a code which indicates the reason. Messages with a code of 2, 5, 6, or 7 are serious and you will need to fix the problem before making a new request. You can ignore all other messages. - -.. code-block:: python - - while True: - try: - iterator = api.request('statuses/filter', {'track':'pizza'}).get_iterator() - for item in iterator: - if 'text' in item: - print(item['text']) - elif 'disconnect' in item: - event = item['disconnect'] - if event['code'] in [2,5,6,7]: - # something needs to be fixed before re-connecting - raise Exception(event['reason']) - else: - # temporary interruption, re-try request - break - except TwitterRequestError as e: - if e.status_code < 500: - # something needs to be fixed before re-connecting - raise - else: - # temporary interruption, re-try request - pass - except TwitterConnectionError: - # temporary interruption, re-try request - pass - -Last Week's Pages ------------------ - -Requests for REST API endpoints can throw `TwitterRequestError <./twittererror.html>`_ and `TwitterConnectionError <./twittererror.html>`_. They do not, however, return "disconnect" messages. Twitter returns error messages for these endpoints with "message". Most of these errors require attention before re-trying the request, except those with codes of 130 or 131, which are internal server errors. - -For making continuos REST API requests (i.e. paging), TwitterAPI provides `TwitterPager <./paging.html>`_. If you use this class to request tweets that have been posted back to one week old, for example, the class's iterator will handle both types of exceptions automatically. The iterator also handles "message" objects with 130 or 131 codes for you. Any other "message" object gets passed on for you to handle. - -One final consideration is the endpoint's rate limit, determinted by the endpoint and whether you authenticate with oAuth 1 or oAuth 2. By default, the iterator waits 5 seconds between requests. This is sufficient for 180 requests in 15 minutes, the rate limit for "search/tweets" with oAuth 1. You can do better with oAuth 2. It permits 450 requests every 15 minutes, or 1 request per 2 seconds. The example below sets the wait assuming oAuth 2 rate limits. - -.. code-block:: python - - iterator = TwitterPager(api, 'search/tweets', {'q':'pizza'}).get_iterator(wait=2) - for item in iterator: - if 'text' in item: - print(item['text']) - elif 'message' in item: - # something needs to be fixed before re-connecting - raise Exception(item['message']) \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst deleted file mode 100644 index 439c4aa..0000000 --- a/docs/index.rst +++ /dev/null @@ -1,53 +0,0 @@ -Introduction -============ - -*Minimal Python wrapper for Twitter's REST and Streaming APIs* - -The principle behind TwitterAPI's design is to provide a single method for accessing the Twitter API. The ``request`` method allows one to call *any* endpoint found on Twitter's `developer site `_, the complete reference for all endpoints. The benefits of a single-method approach are: 1) less code for me to maintain, and 2) a single method for you to learn. Here is a quck example: - -.. code-block:: python - - from TwitterAPI import TwitterAPI - api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret) - r = api.request('search/tweets', {'q':'pizza'}) - print r.status_code - -Get Twitter's entire response as one long string containing tweets (in this example) by calling ``r.text``. More often an iterator is useful: - -.. code-block:: python - - for item in r.get_iterator(): - print item['user']['screen_name'], item['text'] - -The iterator returns JSON objects. What makes the iterator very powerful is it works with both REST API and Streaming API endpoints. No syntax changes required -- supply any endpoint and parameters that are found on Twitter's developer site. - -TwitterAPI is compatible with Python 2 and Python 3. It authenticates using either OAauth 1 or OAuth 2. It also supports web proxy server authentication. All this with minimal code change for you. - -Topics -====== - -.. toctree:: - :maxdepth: 1 - - authentication.rst - paging.rst - errors.rst - faulttolerance.rst - examples.rst - -Modules -======= - -.. toctree:: - :maxdepth: 2 - - twitterapi.rst - twittererror.rst - -Optional: - -.. toctree:: - :maxdepth: 2 - - twitterpager.rst - twitteroauth.rst diff --git a/docs/paging.rst b/docs/paging.rst deleted file mode 100644 index 7e53e47..0000000 --- a/docs/paging.rst +++ /dev/null @@ -1,16 +0,0 @@ -Paging Results -============== - -Whether one is searching for tweets with `search/tweets` or downloading a user's timeline with `statuses/user_timeline` Twitter limits the number of tweets. So, in order to get more tweets, one must make successive requests and with each request skip the previously acquired tweets. This is done by specifying the tweet id from where to start. Twitter has a description `here `_. If you don't want to implement paging yourself, you can use the `TwitterPager <./twitterpager.html>`_ helper class with any REST API endpoint that returns multiples of something. The following, for example, searches for all tweets containing 'pizza' that Twitter has stored -- up to about a week's worth maximum. - -.. code-block:: python - - r = TwitterPager(api, 'search/tweets', {'q':'pizza', 'count':100}) - for item in r.get_iterator(): - if 'text' in item: - print item['text'] - elif 'message' in item and item['code'] == 88: - print 'SUSPEND, RATE LIMIT EXCEEDED: %s\n' % item['message'] - break - -By default there is a built-in wait time of 5 seconds between successive calls. This value is overridden with an argument to `get_iterator()`. See the documentation also to learn how to wait for new tweets. In other words, the iterator can be setup to poll for newer pages of tweets rather than older pages. \ No newline at end of file diff --git a/docs/twitterapi.rst b/docs/twitterapi.rst deleted file mode 100644 index d3d52e2..0000000 --- a/docs/twitterapi.rst +++ /dev/null @@ -1,12 +0,0 @@ -TwitterAPI.TwitterAPI -===================== - -.. automodule:: TwitterAPI.TwitterAPI - -.. autoclass:: TwitterAPI - :members: - :undoc-members: - -.. autoclass:: TwitterResponse - :members: - :undoc-members: diff --git a/docs/twittererror.rst b/docs/twittererror.rst deleted file mode 100644 index 9342e42..0000000 --- a/docs/twittererror.rst +++ /dev/null @@ -1,6 +0,0 @@ -TwitterAPI.TwitterError -=========================== - -.. automodule:: TwitterAPI.TwitterError - :members: - :undoc-members: diff --git a/docs/twitteroauth.rst b/docs/twitteroauth.rst deleted file mode 100644 index 44f882c..0000000 --- a/docs/twitteroauth.rst +++ /dev/null @@ -1,6 +0,0 @@ -TwitterAPI.TwitterOAuth -======================= - -.. automodule:: TwitterAPI.TwitterOAuth - :members: - :undoc-members: diff --git a/docs/twitterpager.rst b/docs/twitterpager.rst deleted file mode 100644 index ffd6103..0000000 --- a/docs/twitterpager.rst +++ /dev/null @@ -1,6 +0,0 @@ -TwitterAPI.TwitterPager -=========================== - -.. automodule:: TwitterAPI.TwitterPager - :members: - :undoc-members: diff --git a/examples/cli/README.rst b/examples/cli/README.rst deleted file mode 100755 index db9e49a..0000000 --- a/examples/cli/README.rst +++ /dev/null @@ -1,21 +0,0 @@ -Command-Line Utility --------------------- -For syntax help:: - - python cli.py -h - -You will need to supply your Twitter application OAuth credentials. The easiest option is to save them in TwitterAPI/credentials.txt. It is the default place where cli.py will look for them. You also may supply an alternative credentials file as a command-line argument. - -Call any REST API endpoint:: - - python cli.py -endpoint statuses/update -parameters status='my tweet' - -Another example (here using abbreviated option names) that parses selected output fields:: - - python cli.py -e search/tweets -p q=zzz count=10 -field screen_name text - -Calling any Streaming API endpoint works too:: - - python cli.py -e statuses/filter -p track=zzz -f screen_name text - -After the ``-field`` option you must supply one or more key names from the raw JSON response object. This will print values only for these keys. When the ``-field`` option is omitted cli.py prints the entire JSON response object. diff --git a/examples/cli/Unicode_win32.py b/examples/cli/Unicode_win32.py deleted file mode 100644 index b7e704a..0000000 --- a/examples/cli/Unicode_win32.py +++ /dev/null @@ -1,267 +0,0 @@ -""" Stdout, stdin, stderr and argv support for unicode. """ -############################################## -# Support for unicode in windows cmd.exe -# Posted on Stack Overflow [1], available under CC-BY-SA [2] -# -# Question: "Windows cmd encoding change causes Python crash" [3] by Alex [4], -# Answered [5] by David-Sarah Hopwood [6]. -# -# [1] https://stackoverflow.com -# [2] https://creativecommons.org/licenses/by-sa/3.0/ -# [3] https://stackoverflow.com/questions/878972 -# [4] https://stackoverflow.com/users/85185 -# [4] https://stackoverflow.com/a/3259271/118671 -# [5] https://stackoverflow.com/users/393146 -# -############################################## - -from __future__ import print_function -import sys -stdin = sys.stdin -stdout = sys.stdout -stderr = sys.stderr -argv = sys.argv - -if sys.version_info[0] > 2: - unicode = str - -if sys.platform == "win32": - import codecs - from ctypes import WINFUNCTYPE, windll, POINTER - from ctypes import byref, c_int, create_unicode_buffer - from ctypes.wintypes import BOOL, HANDLE, DWORD, LPWSTR, LPCWSTR - try: - from ctypes.wintypes import LPVOID - except ImportError: - from ctypes import c_void_p as LPVOID - - original_stderr = sys.stderr - - # If any exception occurs in this code, we'll probably try to print it on stderr, - # which makes for frustrating debugging if stderr is directed to our wrapper. - # So be paranoid about catching errors and reporting them to original_stderr, - # so that we can at least see them. - def _complain(message): - print(isinstance(message, str) and message or repr(message), file=original_stderr) - - # Work around . - codecs.register(lambda name: name == 'cp65001' and codecs.lookup('utf-8') or None) - - # Make Unicode console output work independently of the current code page. - # This also fixes . - # Credit to Michael Kaplan - # and TZOmegaTZIOY - # . - try: - # - # HANDLE WINAPI GetStdHandle(DWORD nStdHandle); - # returns INVALID_HANDLE_VALUE, NULL, or a valid handle - # - # - # DWORD WINAPI GetFileType(DWORD hFile); - # - # - # BOOL WINAPI GetConsoleMode(HANDLE hConsole, LPDWORD lpMode); - - GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(("GetStdHandle", windll.kernel32)) - STD_INPUT_HANDLE = DWORD(-10) - STD_OUTPUT_HANDLE = DWORD(-11) - STD_ERROR_HANDLE = DWORD(-12) - GetFileType = WINFUNCTYPE(DWORD, DWORD)(("GetFileType", windll.kernel32)) - FILE_TYPE_CHAR = 0x0002 - FILE_TYPE_REMOTE = 0x8000 - GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))(("GetConsoleMode", windll.kernel32)) - INVALID_HANDLE_VALUE = DWORD(-1).value - - def not_a_console(handle): - if handle == INVALID_HANDLE_VALUE or handle is None: - return True - return ((GetFileType(handle) & ~FILE_TYPE_REMOTE) != FILE_TYPE_CHAR - or GetConsoleMode(handle, byref(DWORD())) == 0) - - old_stdin_fileno = None - old_stdout_fileno = None - old_stderr_fileno = None - - if hasattr(sys.stdin, 'fileno'): - old_stdin_fileno = sys.stdin.fileno() - if hasattr(sys.stdout, 'fileno'): - old_stdout_fileno = sys.stdout.fileno() - if hasattr(sys.stderr, 'fileno'): - old_stderr_fileno = sys.stderr.fileno() - - STDIN_FILENO = 0 - STDOUT_FILENO = 1 - STDERR_FILENO = 2 - real_stdin = (old_stdin_fileno == STDIN_FILENO) - real_stdout = (old_stdout_fileno == STDOUT_FILENO) - real_stderr = (old_stderr_fileno == STDERR_FILENO) - - if real_stdin: - hStdin = GetStdHandle(STD_INPUT_HANDLE) - if not_a_console(hStdin): - real_stdin = False - - if real_stdout: - hStdout = GetStdHandle(STD_OUTPUT_HANDLE) - if not_a_console(hStdout): - real_stdout = False - - if real_stderr: - hStderr = GetStdHandle(STD_ERROR_HANDLE) - if not_a_console(hStderr): - real_stderr = False - - if real_stdin: - ReadConsoleW = WINFUNCTYPE(BOOL, HANDLE, LPVOID, DWORD, POINTER(DWORD), - LPVOID)(("ReadConsoleW", windll.kernel32)) - - class UnicodeInput: - - """Unicode terminal input class.""" - - def __init__(self, hConsole, name, bufsize=1024): - self._hConsole = hConsole - self.bufsize = bufsize - self.buffer = create_unicode_buffer(bufsize) - self.name = name - self.encoding = 'utf-8' - - def readline(self): - maxnum = DWORD(self.bufsize - 1) - numrecv = DWORD(0) - result = ReadConsoleW(self._hConsole, self.buffer, maxnum, byref(numrecv), None) - if not result: - raise Exception("stdin failure") - return self.buffer.value[:numrecv.value].encode(self.encoding) - - if real_stdout or real_stderr: - # BOOL WINAPI WriteConsoleW(HANDLE hOutput, LPWSTR lpBuffer, DWORD nChars, - # LPDWORD lpCharsWritten, LPVOID lpReserved); - - WriteConsoleW = WINFUNCTYPE(BOOL, HANDLE, LPWSTR, DWORD, POINTER(DWORD), - LPVOID)(("WriteConsoleW", windll.kernel32)) - - class UnicodeOutput: - - """Unicode terminal output class.""" - - def __init__(self, hConsole, stream, fileno, name): - self._hConsole = hConsole - self._stream = stream - self._fileno = fileno - self.closed = False - self.softspace = False - self.mode = 'w' - self.encoding = 'utf-8' - self.name = name - self.flush() - - def isatty(self): - return False - - def close(self): - # don't really close the handle, that would only cause problems - self.closed = True - - def fileno(self): - return self._fileno - - def flush(self): - if self._hConsole is None: - try: - self._stream.flush() - except Exception as e: - _complain("%s.flush: %r from %r" - % (self.name, e, self._stream)) - raise - - def write(self, text): - try: - if self._hConsole is None: - if isinstance(text, unicode): - text = text.encode('utf-8') - self._stream.write(text) - else: - if not isinstance(text, unicode): - text = bytes(text).decode('utf-8') - remaining = len(text) - while remaining > 0: - n = DWORD(0) - # There is a shorter-than-documented limitation on the - # length of the string passed to WriteConsoleW (see - # . - retval = WriteConsoleW(self._hConsole, text, - min(remaining, 10000), - byref(n), None) - if retval == 0 or n.value == 0: - raise IOError("WriteConsoleW returned %r, n.value = %r" - % (retval, n.value)) - remaining -= n.value - if remaining == 0: - break - text = text[n.value:] - except Exception as e: - _complain("%s.write: %r" % (self.name, e)) - raise - - def writelines(self, lines): - try: - for line in lines: - self.write(line) - except Exception as e: - _complain("%s.writelines: %r" % (self.name, e)) - raise - - if real_stdin: - stdin = UnicodeInput(hStdin, name='') - - if real_stdout: - stdout = UnicodeOutput(hStdout, None, STDOUT_FILENO, - '') - else: - stdout = UnicodeOutput(None, sys.stdout, old_stdout_fileno, - '') - - if real_stderr: - stderr = UnicodeOutput(hStderr, None, STDERR_FILENO, - '') - else: - stderr = UnicodeOutput(None, sys.stderr, old_stderr_fileno, - '') - except Exception as e: - _complain("exception %r while fixing up sys.stdout and sys.stderr" % (e,)) - - # While we're at it, let's unmangle the command-line arguments: - - # This works around . - GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32)) - CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(("CommandLineToArgvW", windll.shell32)) - - argc = c_int(0) - argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc)) - - argv = [argv_unicode[i].encode('utf-8') for i in range(0, argc.value)] - - if not hasattr(sys, 'frozen'): - # If this is an executable produced by py2exe or bbfreeze, then it will - # have been invoked directly. Otherwise, unicode_argv[0] is the Python - # interpreter, so skip that. - argv = argv[1:] - - # Also skip option arguments to the Python interpreter. - while len(argv) > 0: - arg = argv[0] - if not arg.startswith(b"-") or arg == u"-": - break - argv = argv[1:] - if arg == u'-m': - # sys.argv[0] should really be the absolute path of the module source, - # but never mind - break - if arg == u'-c': - argv[0] = u'-c' - break - - if argv == []: - argv = [u''] diff --git a/examples/cli/cli.py b/examples/cli/cli.py deleted file mode 100644 index 4ad8d0b..0000000 --- a/examples/cli/cli.py +++ /dev/null @@ -1,136 +0,0 @@ -""" - A Command-Line Interface to Twitter's REST API and Streaming API. - ----------------------------------------------------------------- - - Run this command line script with any Twitter endpoint. The json-formatted - response is printed to the console. The script works with both Streaming API and - REST API endpoints. - - IMPORTANT: Before using this script, you must enter your Twitter application's OAuth - credentials in TwitterAPI/credentials.txt. Log into http://dev.twitter.com to create - your application. - - Examples: - - :: - - python cli -endpoint search/tweets -parameters q=zzz - python cli -endpoint statuses/filter -parameters track=zzz - - These examples print the raw json response. You can also print one or more fields - from the response, for instance the tweet 'text' field, like this: - - :: - - python cli -endpoint statuses/filter -parameters track=zzz -fields text - - Documentation for all Twitter endpoints is located at: - https://dev.twitter.com/docs/api/1.1 -""" - - -__author__ = "geduldig" -__date__ = "June 7, 2013" -__license__ = "MIT" - - -from TwitterAPI import TwitterAPI, __version__ -from TwitterAPI.TwitterOAuth import TwitterOAuth -import argparse -import codecs -import json -import sys - - -# print UTF-8 to the console -if sys.platform == "win32": - from Unicode_win32 import stdout - sys.stdout = stdout - - -def _search(name, obj): - """Breadth-first search for name in the JSON response and return value.""" - q = [] - q.append(obj) - while q: - obj = q.pop(0) - if hasattr(obj, '__iter__') and type(obj) is not str: - isdict = isinstance(obj, dict) - if isdict and name in obj: - return obj[name] - for k in obj: - q.append(obj[k] if isdict else k) - else: - return None - - -def _to_dict(param_list): - """Convert a list of key=value to dict[key]=value""" - if param_list: - return { - name: value for (name, value) in - [param.split('=') for param in param_list]} - else: - return None - - -if __name__ == '__main__': - print('TwitterAPI %s by geduldig' % __version__) - - parser = argparse.ArgumentParser( - description='Request any Twitter Streaming or REST API endpoint') - parser.add_argument( - '-oauth', - metavar='FILENAME', - type=str, - help='file containing OAuth credentials') - parser.add_argument( - '-endpoint', - metavar='ENDPOINT', - type=str, - help='Twitter endpoint', - required=True) - parser.add_argument( - '-parameters', - metavar='NAME_VALUE', - type=str, - help='parameter NAME=VALUE', - nargs='+') - parser.add_argument( - '-fields', - metavar='NAME', - type=str, - help='print a top-level field in the json response', - nargs='+') - parser.add_argument( - '-indent', - metavar='SPACES', - type=int, - help='number of spaces to indent json output', - default=None) - args = parser.parse_args() - - try: - params = _to_dict(args.parameters) - oauth = TwitterOAuth.read_file(args.oauth) - - api = TwitterAPI(oauth.consumer_key, - oauth.consumer_secret, - oauth.access_token_key, - oauth.access_token_secret) - response = api.request(args.endpoint, params) - - for item in response.get_iterator(): - if not args.fields: - print( json.dumps(item, ensure_ascii='False', indent=args.indent)) - else: - for name in args.fields: - value = _search(name, item) - if value: - print('%s: %s' % (name, value)) - - except KeyboardInterrupt: - print('Terminated by user') - - except Exception as e: - print('STOPPED: %s' % e) diff --git a/examples/connect_using_login.py b/examples/connect_using_login.py deleted file mode 100644 index a3020d6..0000000 --- a/examples/connect_using_login.py +++ /dev/null @@ -1,41 +0,0 @@ -import requests -from requests_oauthlib import OAuth1 -from urlparse import parse_qs -from TwitterAPI import TwitterAPI - -consumer_key = -consumer_secret = - -# obtain request token -oauth = OAuth1(consumer_key, consumer_secret) -r = requests.post( - url='https://api.twitter.com/oauth/request_token', - auth=oauth) -credentials = parse_qs(r.content) -request_key = credentials.get('oauth_token')[0] -request_secret = credentials.get('oauth_token_secret')[0] - -# obtain authorization from resource owner -print( - 'Go here to authorize:\n https://api.twitter.com/oauth/authorize?oauth_token=%s' % - request_key) -verifier = raw_input('Enter your authorization code: ') - -# obtain access token -oauth = OAuth1(consumer_key, - consumer_secret, - request_key, - request_secret, - verifier=verifier) -r = requests.post(url='https://api.twitter.com/oauth/access_token', auth=oauth) -credentials = parse_qs(r.content) -access_token_key = credentials.get('oauth_token')[0] -access_token_secret = credentials.get('oauth_token_secret')[0] - -# access resource -api = TwitterAPI(consumer_key, - consumer_secret, - access_token_key, - access_token_secret) -for item in api.request('statuses/filter', {'track': 'zzz'}): - print(item['text']) \ No newline at end of file diff --git a/examples/connect_using_oauth.py b/examples/connect_using_oauth.py deleted file mode 100644 index 46ef4cc..0000000 --- a/examples/connect_using_oauth.py +++ /dev/null @@ -1,31 +0,0 @@ -from TwitterAPI import TwitterAPI - -# If you are behind a firewall you may need to provide proxy server -# authentication. -proxy_url = None # Example: 'https://USERNAME:PASSWORD@PROXYSERVER:PORT' - -# Using OAuth 1.0 to authenticate you have access all Twitter endpoints. -api = TwitterAPI(, - , - , - , - auth_type='oAuth1', - proxy_url=proxy_url) -# Using OAuth 2.0 to authenticate you lose access to user specific endpoints -# (ex. statuses/update and statuses/filter), but you increase your rate limits. -# api = TwitterAPI(, -# , -# auth_type='oAuth2', -# proxy_url=proxy_url) - -r = api.request('application/rate_limit_status') - -# Print HTTP status code (=200 when no errors). -print(r.status_code) - -# Print the raw response. -print(r.text) - -# Parse the JSON response. -j = r.response.json() -print(j['resources']['search']) \ No newline at end of file diff --git a/examples/delete_last_tweet.py b/examples/delete_last_tweet.py deleted file mode 100644 index 3707079..0000000 --- a/examples/delete_last_tweet.py +++ /dev/null @@ -1,34 +0,0 @@ -from threading import Thread -from TwitterAPI import TwitterAPI - -NUMBER_OF_TWEETS_TO_DELETE = 1 - -api = TwitterAPI(, - , - , - ) - -class DeleteTweet(Thread): - - def __init__(self, tweet_id, count): - Thread.__init__(self) - self.tweet_id = tweet_id - self.count = count - - def run(self): - r = api.request('statuses/destroy/:%d' % self.tweet_id) - print(self.count if r.status_code == 200 else 'PROBLEM: ' + r.text) - -try: - count = 0 - r = api.request( - 'statuses/user_timeline', {'count': NUMBER_OF_TWEETS_TO_DELETE}) - for item in r: - if 'id' in item: - count += 1 - tweet_id = item['id'] - DeleteTweet(tweet_id, count).start() - else: - raise Exception(item) -except Exception as e: - print('Stopping: %s' % str(e)) \ No newline at end of file diff --git a/examples/direct_message.py b/examples/direct_message.py deleted file mode 100644 index 4e234aa..0000000 --- a/examples/direct_message.py +++ /dev/null @@ -1,27 +0,0 @@ -from TwitterAPI import TwitterAPI -import json - -api = TwitterAPI(, - , - , - ) - -user_id = -message_text = - -event = { - "event": { - "type": "message_create", - "message_create": { - "target": { - "recipient_id": user_id - }, - "message_data": { - "text": message_text - } - } - } -} - -r = api.request('direct_messages/events/new', json.dumps(event)) -print('SUCCESS' if r.status_code == 200 else 'PROBLEM: ' + r.text) \ No newline at end of file diff --git a/examples/dump_timeline.py b/examples/dump_timeline.py deleted file mode 100644 index 7f56230..0000000 --- a/examples/dump_timeline.py +++ /dev/null @@ -1,23 +0,0 @@ -# Print a user's timeline. This will get up to 3,200 tweets, which -# is the maximum the Twitter API allows. - -from TwitterAPI import TwitterAPI, TwitterPager - -SCREEN_NAME = 'TheTweetOfGod' - -api = TwitterAPI(, - , - auth_type='oAuth2') - -pager = TwitterPager(api, - 'statuses/user_timeline', - {'screen_name':SCREEN_NAME, 'count':200}) - -count = 0 -for item in pager.get_iterator(wait=3.5): - if 'text' in item: - count = count + 1 - print(count, item['text']) - elif 'message' in item: - print(item['message']) - break \ No newline at end of file diff --git a/examples/logging.py b/examples/logging.py deleted file mode 100644 index 6bb29b2..0000000 --- a/examples/logging.py +++ /dev/null @@ -1,31 +0,0 @@ -from TwitterAPI import TwitterAPI -import logging - -# SET UP LOGGING TO FILE AND TO CONSOLE -formatter = logging.Formatter('%(levelname)s %(asctime)s %(message)s', - '%m/%d/%Y %I:%M:%S %p') -fh = logging.FileHandler('logging.log') -fh.setFormatter(formatter) -ch = logging.StreamHandler() -ch.setFormatter(formatter) -logger = logging.getLogger() -logger.setLevel(logging.INFO) -logger.addHandler(fh) -logger.addHandler(ch) - -api = TwitterAPI(, - , - , - ) - -logging.info('START SAMPLE STREAM') - -try: - r = api.request('statuses/sample') - for item in r: - if 'text' in item: - print(item['text']) - elif: - logging.info('NOT A TWEET: %s' % item.text) -except Exception as e: - logging.warning('STOPPING: %s' % str(e)) \ No newline at end of file diff --git a/examples/lookup_tweet.py b/examples/lookup_tweet.py deleted file mode 100644 index 1a8f950..0000000 --- a/examples/lookup_tweet.py +++ /dev/null @@ -1,12 +0,0 @@ -from TwitterAPI import TwitterAPI - -TWEET_ID = '964575983633252353' - -api = TwitterAPI(, - , - auth_type='oAuth2') - -r = api.request('statuses/show/:' + TWEET_ID) -tweet = r.json() -print(tweet['user']['screen_name'] + ':' + tweet['text'] if r.status_code == 200 - else 'PROBLEM: ' + r.text) \ No newline at end of file diff --git a/examples/lookup_user.py b/examples/lookup_user.py deleted file mode 100644 index 1b1a81a..0000000 --- a/examples/lookup_user.py +++ /dev/null @@ -1,10 +0,0 @@ -from TwitterAPI import TwitterAPI - -SCREEN_NAME = 'TheTweetOfGod' - -api = TwitterAPI(, - , - auth_type='oAuth2') - -r = api.request('users/lookup', {'screen_name':SCREEN_NAME}) -print(r.json()[0]['id'] if r.status_code == 200 else 'PROBLEM: ' + r.text) diff --git a/examples/page_tweets.py b/examples/page_tweets.py deleted file mode 100644 index 6a80931..0000000 --- a/examples/page_tweets.py +++ /dev/null @@ -1,13 +0,0 @@ -from TwitterAPI import TwitterAPI, TwitterPager - -SEARCH_TERM = 'pizza' - -api = TwitterAPI(, - , - , - ) - -pager = TwitterPager(api, 'search/tweets', {'q': SEARCH_TERM}) - -for item in pager.get_iterator(): - print(item['text'] if 'text' in item else item) \ No newline at end of file diff --git a/examples/post_tweet.py b/examples/post_tweet.py deleted file mode 100644 index d5977f2..0000000 --- a/examples/post_tweet.py +++ /dev/null @@ -1,11 +0,0 @@ -from TwitterAPI import TwitterAPI - -TWEET_TEXT = "Ce n'est pas un tweet tweet." - -api = TwitterAPI(, - , - , - ) - -r = api.request('statuses/update', {'status': TWEET_TEXT}) -print('SUCCESS' if r.status_code == 200 else 'PROBLEM: ' + r.text) \ No newline at end of file diff --git a/examples/sample_frequency.py b/examples/sample_frequency.py deleted file mode 100644 index 8615f0d..0000000 --- a/examples/sample_frequency.py +++ /dev/null @@ -1,68 +0,0 @@ -from TwitterAPI import TwitterAPI, TwitterConnectionError, TwitterRequestError -import time - -api = TwitterAPI(, - , - , - ) - -class Frequency: - - """Track tweet download statistics""" - - def __init__(self): - self.interval = 5 # seconds - self.total_count = 0 - self.total_start = time.time() - self.interval_count = 0 - self.interval_start = self.total_start - - def update(self): - self.interval_count += 1 - self.total_count += 1 - now = time.time() - elapsed = now - self.interval_start - if elapsed >= self.interval: - # timestamp : tps : total cumulative tweets : average tps - print('%s -- %d\t%d\t%d' % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now)), - int(self.interval_count / elapsed), - self.total_count, - int(self.total_count / (now - self.total_start)))) - self.interval_start = now - self.interval_count = 0 - -freq = Frequency() - -# THIS DEMONSTRATES HOW TO HANDLE ALL TYPES OF STREAMING ERRORS. -# SO, THIS IS APPLICAPLE TO 'statuses/filter' AS WELL. ONLY WHEN -# APPROPRIATE, A DROPPED CONNECTION IS RE-ESTABLISHED. - -while True: - try: - r = api.request('statuses/sample') - for item in r: - if 'text' in item: - freq.update() - elif 'limit' in item: - print('TWEETS SKIPPED: %s' % item['limit']['track']) - elif 'warning' in item: - print(item['warning']) - elif 'disconnect' in item: - event = item['disconnect'] - if event['code'] in [2,5,6,7]: - # streaming connection rejected - raise Exception(event) - print('RE-CONNECTING: %s' % event) - break - except TwitterRequestError as e: - if e.status_code < 500: - print('REQUEST FAILED: %s' % e) - break - except TwitterConnectionError: - pass - except KeyboardInterrupt: - print('TERMINATED BY USER') - break - except Exception as e: - print('STOPPED: %s %s' % (type(e), e)) - break \ No newline at end of file diff --git a/examples/search_tweets.py b/examples/search_tweets.py deleted file mode 100644 index 4067281..0000000 --- a/examples/search_tweets.py +++ /dev/null @@ -1,15 +0,0 @@ -from TwitterAPI import TwitterAPI - -SEARCH_TERM = 'pizza' - -api = TwitterAPI(, - , - , - ) - -r = api.request('search/tweets', {'q': SEARCH_TERM}) - -for item in r: - print(item['text'] if 'text' in item else item) - -print('\nQUOTA: %s' % r.get_quota()) \ No newline at end of file diff --git a/examples/stream_tweets.py b/examples/stream_tweets.py deleted file mode 100644 index 51da26b..0000000 --- a/examples/stream_tweets.py +++ /dev/null @@ -1,13 +0,0 @@ -from TwitterAPI import TwitterAPI - -TRACK_TERM = 'pizza' - -api = TwitterAPI(, - , - , - ) - -r = api.request('statuses/filter', {'track': TRACK_TERM}) - -for item in r: - print(item['text'] if 'text' in item else item) \ No newline at end of file diff --git a/examples/unfollow_friends.py b/examples/unfollow_friends.py deleted file mode 100644 index abc1ef7..0000000 --- a/examples/unfollow_friends.py +++ /dev/null @@ -1,18 +0,0 @@ -# Unfollow friends that do not follow you. - -from TwitterAPI import TwitterAPI - -api = TwitterAPI(, - , - , - ) - -followers = set(id for id in api.request('followers/ids')) -friends = set(id for id in api.request('friends/ids')) -unfollow = set(followers) - set(friends) - -for id in unfollow: - r = api.request('friendships/destroy', {'user_id': id}) - if r.status_code == 200: - status = r.json() - print 'unfollowed %s' % status['screen_name'] \ No newline at end of file diff --git a/examples/upload_image.py b/examples/upload_image.py deleted file mode 100644 index 11aa5a2..0000000 --- a/examples/upload_image.py +++ /dev/null @@ -1,23 +0,0 @@ -# Post an image with tweet. Requires two API calls. - -from TwitterAPI import TwitterAPI - -TWEET_TEXT = 'some tweet text' -IMAGE_PATH = './some_image.png' - -api = TwitterAPI(, - , - , - ) - -# STEP 1 - upload image -file = open(IMAGE_PATH, 'rb') -data = file.read() -r = api.request('media/upload', None, {'media': data}) -print('UPLOAD MEDIA SUCCESS' if r.status_code == 200 else 'UPLOAD MEDIA FAILURE: ' + r.text) - -# STEP 2 - post tweet with a reference to uploaded image -if r.status_code == 200: - media_id = r.json()['media_id'] - r = api.request('statuses/update', {'status': TWEET_TEXT, 'media_ids': media_id}) - print('UPDATE STATUS SUCCESS' if r.status_code == 200 else 'UPDATE STATUS FAILURE: ' + r.text) \ No newline at end of file diff --git a/examples/upload_tweet.py b/examples/upload_tweet.py deleted file mode 100644 index f9e7b45..0000000 --- a/examples/upload_tweet.py +++ /dev/null @@ -1,11 +0,0 @@ -from TwitterAPI import TwitterAPI - -TWEET_TEXT = 'some tweet text' - -api = TwitterAPI(, - , - , - ) - -r = api.request('statuses/update', {'status':TWEET_TEXT}) -print('SUCCESS' if r.status_code == 200 else 'PROBLEM: ' + r.text) \ No newline at end of file diff --git a/examples/upload_video.py b/examples/upload_video.py deleted file mode 100644 index dc29c94..0000000 --- a/examples/upload_video.py +++ /dev/null @@ -1,48 +0,0 @@ -# Twitter places several restrictions on uploaded video. If your upload fails -# with error 324, your video probably violates these restrictions. -# -# Read about them here: -# https://dev.twitter.com/rest/public/uploading-media#videorecs - -from TwitterAPI import TwitterAPI -import os -import sys - -VIDEO_FILENAME = 'test.mp4' -TWEET_TEXT = 'Video upload test' - -api = TwitterAPI(, - , - , - ) - -bytes_sent = 0 -total_bytes = os.path.getsize(VIDEO_FILENAME) -file = open(VIDEO_FILENAME, 'rb') - -def check_status(r): - # EXIT PROGRAM WITH ERROR MESSAGE - if r.status_code < 200 or r.status_code > 299: - print(r.status_code) - print(r.text) - sys.exit(0) - -r = api.request('media/upload', {'command':'INIT', 'media_type':'video/mp4', 'total_bytes':total_bytes}) -check_status(r) - -media_id = r.json()['media_id'] -segment_id = 0 - -while bytes_sent < total_bytes: - chunk = file.read(4*1024*1024) - r = api.request('media/upload', {'command':'APPEND', 'media_id':media_id, 'segment_index':segment_id}, {'media':chunk}) - check_status(r) - segment_id = segment_id + 1 - bytes_sent = file.tell() - print('[' + str(total_bytes) + ']', str(bytes_sent)) - -r = api.request('media/upload', {'command':'FINALIZE', 'media_id':media_id}) -check_status(r) - -r = api.request('statuses/update', {'status':TWEET_TEXT, 'media_ids':media_id}) -check_status(r) \ No newline at end of file diff --git a/logo.png b/logo.png deleted file mode 100755 index d697092..0000000 Binary files a/logo.png and /dev/null differ diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 1cc432e..0000000 --- a/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -requests -requests_oauthlib diff --git a/setup.py b/setup.py deleted file mode 100644 index 8c73284..0000000 --- a/setup.py +++ /dev/null @@ -1,28 +0,0 @@ -from distutils.core import setup -from TwitterAPI import __version__ -import io - - -def read(*filenames, **kwargs): - encoding = kwargs.get('encoding', 'utf-8') - sep = kwargs.get('sep', '\n') - buf = [] - for filename in filenames: - with io.open(filename, encoding=encoding) as f: - buf.append(f.read()) - return sep.join(buf) - -setup( - name='TwitterAPI', - version=__version__, - author='geduldig', - author_email='boxnumber03@gmail.com', - packages=['TwitterAPI'], - package_data={'': ['credentials.txt']}, - url='https://github.com/geduldig/TwitterAPI', - download_url='https://github.com/geduldig/TwitterAPI/tarball/master', - license='MIT', - keywords='twitter', - description='Minimal wrapper for Twitter\'s REST and Streaming APIs', - install_requires=['requests', 'requests_oauthlib'] -) diff --git a/tests/test_iterators.py b/tests/test_iterators.py deleted file mode 100644 index b85fe6c..0000000 --- a/tests/test_iterators.py +++ /dev/null @@ -1,53 +0,0 @@ -import unittest -import TwitterAPI - - -class IteratorTest(unittest.TestCase): - - """Test REST API and Streaming API iterators.""" - - def setUp(self): - """Read credentials from TwitterAPI/credentials.txt. You - must copy your credentials into this text file. - """ - oa = TwitterAPI.TwitterOAuth.read_file() - self.api = TwitterAPI.TwitterAPI(oa.consumer_key, - oa.consumer_secret, - oa.access_token_key, - oa.access_token_secret) - - def test_rest_iterator(self): - r = self.api.request('search/tweets', {'q': 'pizza'}) - self.assertIsInstance(r, TwitterAPI.TwitterResponse) - # 200 means success - self.assertEqual(r.status_code, 200) - it = r.get_iterator() - self.use_iterator(it) - - def test_streaming_iterator(self): - r = self.api.request('statuses/filter', {'track': 'pizza'}) - self.assertIsInstance(r, TwitterAPI.TwitterResponse) - # 200 means success - # 420 means too many simultaneous connections, ok - self.assertIn(r.status_code, [200, 420]) - if r.status_code == 200: - it = r.get_iterator() - self.use_iterator(it) - - def test_paging_iterator(self): - pager = TwitterAPI.TwitterPager(self.api, - 'search/tweets', - {'q': 'pizza'}) - self.assertIsInstance(pager, TwitterAPI.TwitterPager) - it = pager.get_iterator() - self.use_iterator(it) - - def use_iterator(self, it): - """Checks the first item for a tweet id.""" - self.assertTrue(hasattr(it, '__iter__')) - item = next(it) - self.assertIn('id', item) - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_oauth.py b/tests/test_oauth.py deleted file mode 100644 index 39715a7..0000000 --- a/tests/test_oauth.py +++ /dev/null @@ -1,38 +0,0 @@ -from TwitterAPI import TwitterAPI, TwitterOAuth -import unittest - - -class OAuthTest(unittest.TestCase): - - """Test user and application authentication.""" - - def setUp(self): - """Read credentials from TwitterAPI/credentials.txt. You - must copy your credentials into this text file. - """ - self.oa = TwitterOAuth.read_file() - - def test_oauth_1(self): - """Test user authentication.""" - api = TwitterAPI(self.oa.consumer_key, self.oa.consumer_secret, - self.oa.access_token_key, self.oa.access_token_secret) - status_code = self.verify_credentials(api) - # 200 means success - self.assertEqual(status_code, 200) - - def test_oauth_2(self): - """Test application authentication.""" - api = TwitterAPI(self.oa.consumer_key, self.oa.consumer_secret, - auth_type='oAuth2') - status_code = self.verify_credentials(api) - # 403 means no access, which is correct since no user credentials - # provided - self.assertEqual(status_code, 403) - - def verify_credentials(self, api): - r = api.request('account/verify_credentials') - return r.status_code - - -if __name__ == '__main__': - unittest.main()