Package: pytangle
pytangleA python wrapper for crowdtangleIn this repositorypytangle.py: main file with examplespytangle/api.py: object oriented interface to the apipytangle/connectivity.py: uses requests to forward calls to the api endpoints. performs rate limiting and network-related error handling.pytangle/endpoints.py: objects detailing the crowdtangle API endpointspytangle/utils.py: common utility procedurespytangle_config_sample.json: sample configuration file.pytangleuses this file to load your API token and to set logging
preferences. Copypytangle_config_sample.jsoninto a file namedpytangle_config.jsonbefore
customizing it. You most likely want to edit at least your API token. See the later section for further customizations.QuickstartInstall the package:pip3 install pytangleCreate a file namedpytangle_config.jsonin the folder from where you will callpytangle, and paste:{"token":"YOUR_CROWDTANGLE_TOKEN"}Check that everything works:frompytangle.apiimportAPIapi=API()# or "api = API(token=YOUR_CROWDTANGLE_TOKEN)"# if you did not create pytangle_config.json# fetch lists in the current dashboardfora_listinapi.lists():print(a_list)InstallationThe quickest way to download and install is:pip3installpytangleOtherwise, download the code, move into the directory, and install from the local file, e.g.:gitclonehttps://github.com/hide-ous/pytangle.gitcdpytangle
pip3install.To install only the dependencies, and use the library without installing,
run:pip3install-rrequirements.txtPython 3.5 and up. Tested on 3.7.ExamplesIt is easy to use the API. Just create an API instance, and start querying way. If you have customizedpytangle_config.jsonthe
API instance will automatically load your API token, otherwise you can assign explicitly via code. Rate limiting and
pagination are handled for you.frompytangle.apiimportAPIapi=API()# use the following line instead if you do not want to set up a configuration file# api = API(token="YOUR_TOKEN")All query methods return an iterator, one result object (i.e. one post, account, ...) at a time. For example, to fetch
the lists linked to the current dashboard:frompytangle.apiimportAPIapi=API()# fetch all lists linked to the current dashboardfora_listinapi.lists():print(a_list)The same applies to other types of objects, like posts:frompytangle.apiimportAPIapi=API()# get the 5 top overperforming postsfora_postinapi.posts(count=5):print(a_post)If you want information about a specific post, you can query it via either its facebook id or its crowdtangle id, by
specifying theendpoint(platformorctrespectively). For differences seethe official wiki.frompytangle.apiimportAPIapi=API()# get information about a specific postpost_id="1515871602074952_5362226790772728"fora_postinapi.post(id=post_id,endpoint='platform'):print(a_post)Get the leaderboard for the current dashboard:frompytangle.apiimportAPIapi=API()# get the first 5 entries in the leaderboard for the current dashboardfora_leaderboardinapi.leaderboard(count=5):print(a_leaderboard)Get the top 5 posts sharing a link (like in the crowdtangle chrome extension):frompytangle.apiimportAPIapi=API()# get the top 5 sharers of a linkan_url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"fora_postinapi.links(count=5,link=an_url):print(a_post)Get details about a specific account in a list:frompytangle.apiimportAPIapi=API()# get information on an accountlist_id=12345# the id of one of your listsforan_accountinapi.accounts_in_list(count=5,listId=list_id):print(an_account)Get all posts from a list in a specific date range (you can find which lists are in your dashboard viaapi.lists():frompytangle.apiimportAPIapi=API()# get all posts from a list from Jan until June 2020list_ids=[12345,]# ids of the lists of interestforn,a_postinenumerate(api.posts(listIds=list_ids,count=-1,batchSize=100,sortBy='date',startDate='2020-01-01',endDate='2020-06-30',timeframe=None,)):# do something with the postifnotn%1000:print(n)ConfiguringpytangleThe configuration filepytangle_config.jsonis a simple json file, containing two main sections:token: is the API token associated with a dashboard within crowdtangle. If you have access to
the API, you can locate your API token via your dashboard under Settings > API Access.logging: is a dictionary of items determining how pytangle should log. It follows the conventions
inlogging.dictConfig: for explanations on the various options see theofficial reference.The default locations forpytangle_config.jsonare:at the root of the presently used module, orin the user's home.An API instance will attempt to load the token from the configuration file, if a token is not explicitly passed.pytangle.pyshows an example of how to initialize loggers using the configuration inpytangle_config.json. However, the API
will not load the logging configuration by default.pytangle_config_sample.jsonprovides a reasonable starting point to customizepytangle: just copy the file
in one of the default locations under the namepytangle_config.json, and edit (at least) yourtoken.Changelog0.0.2bugfix: pass token via code instead of configuration file0.0.1 initial release