Skip to content

Commit

Permalink
ct_get_posts function
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseRZapata committed Jun 26, 2020
1 parent 8b4f633 commit 4b80cbc
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
48 changes: 48 additions & 0 deletions PyCrowdTangle/PyCrowdTangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests

# save in a file called api-key.txt file
# your api-key at your proyect folder
try:
with open("apikey.txt", "r") as file:
apikey = file.readline()
api_token = apikey.strip('\n')
except:
print("api-key.txt not found")


def ct_get_posts(count=100, start_date="", end_date="", api_token=""):
"""Retrieve a set of posts for the given parameters get post from crowdtangle
Args:
count (int, optional): The number of posts to return. Defaults to 100. options [1-100]
start_date (str, optional): [description]. Defaults to "".
end_date (str, optional): The latest date at which a post could be posted.
Time zone is UTC. Format is “yyyy-mm-ddThh:mm:ss”
or “yyyy-mm-dd” (defaults to time 00:00:00).
Defaults to "now".
api_token (str, optional): [description]. Defaults to "".
Returns:
[dict]: The Response contains both a status code and a result. The status will always
be 200 if there is no error. The result contains an array of post objects and
a pagination object with URLs for both the next and previous page, if they exist
"""

# api-endpoint
URL_BASE = "https://api.crowdtangle.com/posts"
# defining a params dict for the parameters to be sent to the API
PARAMS = {'count': count, 'token': api_token}

# add params parameters
if start_date:
PARAMS['startDate'] = start_date
if start_date:
PARAMS['endDate'] = end_date

# sending get request and saving the response as response object
r = requests.get(url=URL_BASE, params=PARAMS)
# status = r.status_code
return r.json()
5 changes: 5 additions & 0 deletions PyCrowdTangle/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

"""Top-level package for PyCrowdTangle"""

__author__ = """Jose R. Zapata"""
__version__ = '0.0.1'
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.24.0
27 changes: 27 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages

with open("README.md", "r") as readme_file:
readme = readme_file.read()

requirements = ["requests>=2"]

setup(
name="PyCrowdTangle",
version="0.0.1",
author="Jose R. Zapata",
author_email="[email protected]",
description="A Python Wrapper To Retrieve Data From The CrowdTangle API",
long_description=readme,
long_description_content_type="text/markdown",
url="https://github.com/UPB-SS1/PyCrowdTangle",
packages=find_packages(),
install_requires=requirements,
classifiers=[
"Programming Language :: Python :: 3.7",
"License :: OSI Approved :: MIT License",
],
keywords=['crowdtangle', 'wrapper-api'],
)
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2 changes: 2 additions & 0 deletions tests/test_PyCrwodTangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

0 comments on commit 4b80cbc

Please sign in to comment.