diff --git a/.gitignore b/.gitignore index aa408b8..7e51e8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ # Compiled Python Files *.pyc +*.swp +dist/* +*.egg-info/ diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..c838777 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Thomas Forbes + +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. diff --git a/README.md b/README.md index b8249d1..b015344 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,27 @@ ## Installation -You can install **active-campaign-python** by downloading the source. +You can install **active-campaign-python** from pypi -[Click here to download the source (.zip)](https://github.com/adulmec/active-campaign-python/zipball/master) which includes all dependencies. - -`from includes.ActiveCampaign import ActiveCampaign` - -Fill in your URL and API Key in the `includes/Config.py` file, and you are good to go! +`pip install active-campaign-python` ## Example Usage -### includes/Config.py -`from includes.Config import ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY` - -### examples.py -
-from includes.ActiveCampaign import ActiveCampaign -from includes.Config import ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY +from activecampaign import ActiveCampaign + +# url provided to you by ActiveCampaign +base_url = '-See our [examples file](https://github.com/adulmec/active-campaign-python/blob/master/examples.py) or the comments from files in **includes** folder for more in-depth samples. +Each of the endpoint subclasses have comments at the bottom ## Prerequisites 1. A valid ActiveCampaign **hosted** account (trial or paid). + +## That is all for now diff --git a/activecampaign/Account.py b/activecampaign/Account.py new file mode 100644 index 0000000..1316004 --- /dev/null +++ b/activecampaign/Account.py @@ -0,0 +1,107 @@ +from .ActiveCampaign import ( + ActiveCampaign, + fmt_params, + fmt_noparams +) +import requests as rq + + +class Account(ActiveCampaign): + + def __init__(self, url, api_key): + self.url = url + self.api_key = api_key + super(Account, self).__init__(url, api_key) + + def add(self, params, post_data={}): + rq_url = fmt_noparams( + self.url, + 'account_add', + self.output + ) + response = rq.post(rq_url, data=post_data) + return response + + def cancel(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'account_cancel', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def edit(self, params, post_data={}): + rq_url = fmt_noparams( + self.url, + 'account_edit', + self.output + ) + response = rq.post(rq_url, data=post_data) + return response + + def list_(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'account_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def name_check(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'account_name_check', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def plans(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'account_plans', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def status(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'account_status', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def status_set(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'account_status_set', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def view(self, params, post_data={}): + rq_url = fmt_noparams( + self.url, + 'account_view', + self.output + ) + response = rq.get(rq_url) + return response + + +""" + ## view + #print ac.api('account/view') +""" diff --git a/activecampaign/ActiveCampaign.py b/activecampaign/ActiveCampaign.py new file mode 100644 index 0000000..40a94dc --- /dev/null +++ b/activecampaign/ActiveCampaign.py @@ -0,0 +1,172 @@ +from importlib import import_module +from .Connector import Connector + +# formatters for making life easier, don't you want it that way? +fmt_params = '{}&api_action={}&api_output={}&{}'.format +fmt_noparams = '{}&api_action={}&api_output={}'.format + + +def get_mod(cls, parent): + source_module = import_module(".{}".format(cls), parent) + class1 = getattr(source_module, cls) # get Subscriber + return class1 + + +class ActiveCampaign(Connector): + + def __init__(self, url, api_key, api_user='', api_pass=''): + self.url = url + self.api_key = api_key + self.URL = url + self.APIKEY = api_key + super(ActiveCampaign, self).__init__(url, api_key, api_user, api_pass) + + def api(self, path, post_data={}): + # IE: "contact/view" + components = path.split('/') + component = components[0] + + if '?' in components[1]: + # query params appended to method + # IE: contact/edit?overwrite=0 + method_arr = components[1].split('?') + method = method_arr[0] + params = method_arr[1] + else: + # just a method provided + # IE: "contact/view + if components[1]: + method = components[1] + params = '' + else: + return 'Invalid method.' + + # adjustments + if component == 'branding': + # reserved word + component = 'design' + elif component == 'sync': + component = 'contact' + method = 'sync' + elif component == 'singlesignon': + component = 'auth' + + # "contact" becomes "Contact" + class1 = '{}'.format(component.capitalize()) + class1 = get_mod(class1, 'activecampaign') + class1 = class1(self.URL, self.APIKEY) # Subscriber() + + if method == 'list': + # reserved word + method = 'list_' + + if method in dir(class1): + if post_data: + return getattr(class1, method)(params, post_data) + else: + return getattr(class1, method)(params) + return None + + # extra methods I created for shorthand scripting + # get + def _get_contact_by_id(self, cid=None): + """Get a contact by ID + + Arguments: + cid:str contact/subscriber ID + """ + response = self.api('contact/view?id={}'.format(cid)) + return response + + def _get_contact_by_email(self, email=None): + """Get a contact by email + + Arguments: + email:str contact/company Email + """ + response = self.api('contact/view?email={}'.format(email)) + return response + + # delete + def _delete_contact_by_id(self, cid=None): + """Delete a contact/company + + Arguments: + cid:str contact/susbscriber ID + """ + response = self.api('contact/delete?id={}'.format(cid)) + return response + + # create + def _create_contact(self, data=None): + """Create a contact/company + + Arguments: + data:dict proper definition of a contact dict + """ + response = self.api('contact/ad', post_data=data) + return response + # end contact section + + # create + def _add_tags_by_id(self, cid=None, tag=None): + """ Add tags by id for company/contact + + Arguments: + cid:str contact/susbscriber ID + tags:list(str) list of tags as strings + """ + response = self.api('contact/tag_add', + post_data={'id': cid, 'tags':tag}) + return response + + # delete + def _delete_tags_by_id(self, cid=None, tag=None): + """ Delete a tag by a contact/susbscriber ID + + Arguments: + cid:str contact/susbscriber ID + tags:list(str) list of tags as strings + """ + response = self.api('contact/tag_remove', + post_data={'id': cid, 'tags':tag}) + return response + + def _add_note_by_id(self, cid=None, note=""): + """ Add a Note for a contact/company + + Arguments: + cid:str contact/susbscriber ID + note:str a note + """ + data = {"id": cid, "note": note} + response = self.api('contact/note_add?id={}'.format(cid), + post_data=data) + return response + + # delete + def _delete_note_by_id(self, nid=None): + """ Delete a note by a note ID + + Arguments: + nid:str note ID to delete + """ + response = self.api('contact/note_delete?noteid={}'.format(nid)) + return response + # end contact components (properties, tags...) + + # list contacts + def _list_contacts(self): + """List all contacts + + """ + response = self.api('contact/list?ids=all') + return response + + # list organizations, not very usefull but still + def _list_orgs(self): + """List all organizations + + """ + response = self.api('organization/list') + return response diff --git a/activecampaign/Campaign.py b/activecampaign/Campaign.py new file mode 100644 index 0000000..34367df --- /dev/null +++ b/activecampaign/Campaign.py @@ -0,0 +1,285 @@ +from .ActiveCampaign import ( + ActiveCampaign, + fmt_params, + fmt_noparams +) +import requests as rq + + +class Campaign(ActiveCampaign): + + def __init__(self, url, api_key): + self.url = url + self.api_key = api_key + super(Campaign, self).__init__(url, api_key) + + def create(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'campaign_create', + self.output + ) + response = rq.post(rq_url, data=post_data) + return response + + def delete(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_delete', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def delete_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_delete_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def list_(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def paginator(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_paginator', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_bounce_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_bounce_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_bounce_totals(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_bounce_totals', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_forward_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_forward_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_forward_totals(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_forward_totals', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_link_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_link_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_link_totals(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_link_totals', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_open_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_open_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_open_totals(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_open_totals', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_totals(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_totals', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_unopen_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_unopen_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_unsubscription_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_unsubscription_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def report_unsubscription_totals(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_report_unsubscription_totals', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def send(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_send', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def status(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'campaign_status', + self.output, + params + ) + response = rq.get(rq_url) + return response + + +""" +if __name__ == '__main__': + ac = ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY) + + ## create + sdate = datetime.datetime.now() + datetime.timedelta(hours=0, minutes=2) + campaign = { + 'type': 'single', + 'name': 'testActiveCampaign: {}'.formatdatetime.datetime.now(), + 'sdate': time.strftime('%Y-%m-%d %H:%M:%S', sdate.timetuple()), + 'status': 1, + 'public': 1, + 'tracklinks': 'all', + 'trackreads': 1, + 'htmlunsub': 1, + 'p[1]': 1, + 'm[35]': 100 + } + from time import time + time2 = time() + print ac.api('campaign/create', campaign) + print 'diff2 = %.5f seconds' %(time() - time2) + + ## delete +## print ac.api('campaign/delete?id=12') + + ## delete_list +## print ac.api('campaign/delete_list?ids=1,2') + + ## list +## print ac.api('campaign/list?ids=3,4') + + ## paginator +## print ac.api('campaign/paginator?sort=&offset=0&limit=20&' + 'filter=0&public=0') + + ## report_bounce_list +## print ac.api('campaign/report_bounce_list?campaignid=3') + + ## report_bounce_totals +## print ac.api('campaign/report_bounce_totals?campaignid=13&messageid=2') + + ## report_forward_list +## print ac.api('campaign/report_forward_list?campaignid=13&messageid=2') + + ## report_forward_totals +## print ac.api('campaign/report_forward_totals?campaignid=13&messageid=2') + + ## report_link_list +## print ac.api('campaign/report_link_list?campaignid=13&messageid=2') + + ## report_link_totals +## print ac.api('campaign/report_link_totals?campaignid=13&messageid=2') + + ## report_open_list +## print ac.api('campaign/report_open_list?campaignid=13&messageid=2') + + ## report_open_totals +## print ac.api('campaign/report_open_totals?campaignid=13&messageid=2') + + ## report_totals +## print ac.api('campaign/report_totals?campaignid=13&messageid=2') + + ## report_unopen_list +## print ac.api('campaign/report_unopen_list?campaignid=13&messageid=2') + + ## report_unsubscription_list +## print ac.api('campaign/report_unsubscription_list?campaignid=13') + + ## report_unsubscription_totals +## print ac.api('campaign/report_unsubscription_totals?campaignid=13&' + 'messageid=2') + + ## report_send +## print ac.api('campaign/send?campaignid=13&messageid=2&type=mime&' + 'action=send&email=person@example.com') + + ## report_status +## print ac.api('campaign/status?id=13&status=5') +""" diff --git a/activecampaign/Connector.py b/activecampaign/Connector.py new file mode 100644 index 0000000..f006264 --- /dev/null +++ b/activecampaign/Connector.py @@ -0,0 +1,35 @@ +import requests as rq + + +class Connector(object): + + def __init__(self, url, api_key, api_user='', api_pass=''): + self.output = 'json' + self.base = '' + + if url != 'https://www.activecampaign.com': + # not set reseller + self.base = '/admin' + if url[-1] == '/': + # remove trailing slash + url = url[:-1] + + if api_key: + self.url = '{}{}/api.php?api_key={}'\ + .format(url, self.base, api_key) + else: + self.url = '{}{}/api.php?api_user={}&api_pass={}'\ + .format(url, self.base, api_user, api_pass) + self.api_key = api_key + + def credentials_test(self): + test_url = '{}&api_action=group_view&api_output={}&id=3'\ + .format(self.url, self.output) + return rq.get(test_url).status_code == 200 + + +""" +if __name__ == '__main__': + c = Connector(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY) + print c.credentials_test() +""" diff --git a/activecampaign/Contact.py b/activecampaign/Contact.py new file mode 100644 index 0000000..eac6324 --- /dev/null +++ b/activecampaign/Contact.py @@ -0,0 +1,183 @@ +from .ActiveCampaign import ( + ActiveCampaign, + fmt_params, + fmt_noparams +) +import requests as rq + + +class Contact(ActiveCampaign): + + def __init__(self, url, api_key): + self.url = url + self.api_key = api_key + super(Contact, self).__init__(url, api_key) + + def add(self, params, post_data={}): + if params: + rq_url = fmt_params(self.url, 'contact_add', + self.output, params) + else: + rq_url = fmt_noparams(self.url, 'contact_add', + self.output) + response = rq.post(rq_url, data=post_data) + return response + + def automation_list(self, params): + rq_url = fmt_params(self.url, 'contact_add', + self.output, params) + response = rq.post(rq_url) + return response + + def delete_list(self, params): + rq_url = fmt_params(self.url, 'contact_delete_list', + self.output, params) + response = rq.get(rq_url) + return response + + def delete(self, params): + rq_url = fmt_params(self.url, 'contact_delete', + self.output, params) + response = rq.get(rq_url) + return response + + def edit(self, params, post_data={}): + rq_url = fmt_noparams(self.url, 'contact_edit', self.output) + response = rq.post(rq_url, data=post_data) + return response + + def list_(self, params): + rq_url = fmt_params(self.url, 'contact_list', + self.output, params) + response = rq.get(rq_url) + return response + + def note_add(self, params, post_data={}): + rq_url = fmt_params(self.url, 'contact_note_add', + self.output, params) + response = rq.post(rq_url, data=post_data) + return response + + def note_edit(self, params, post_data={}): + rq_url = fmt_params(self.url, 'contact_note_edit', + self.output, params) + response = rq.post(rq_url, data=post_data) + return response + + def note_delete(self, params): + rq_url = fmt_noparams(self.url, 'contact_note_delete', + self.output) + response = rq.post(rq_url) + return response + + def paginator(self, params): + rq_url = fmt_params(self.url, 'contact_paginator', + self.output, params) + response = rq.get(rq_url) + return response + + def sync(self, params, post_data): + if params: + rq_url = fmt_params(self.url, 'contact_sync', + self.output, params) + else: + rq_url = fmt_noparams(self.url, 'contact_sync', self.output) + response = rq.post(rq_url, data=post_data) + return response + + def tag_add(self, params, post_data={}): + if params: + rq_url = fmt_params(self.url, 'contact_tag_add', + self.output, params) + else: + rq_url = fmt_noparams(self.url, 'contact_tag_add', + self.output) + response = rq.post(rq_url, data=post_data) + return response + + def tag_remove(self, params, post_data={}): + if params: + rq_url = fmt_params(self.url, 'contact_tag_remove', + self.output, params) + else: + rq_url = fmt_noparams(self.url, 'contact_tag_remove', + self.output) + response = rq.post(rq_url, data=post_data) + return response + + def view(self, params, post_data={}): + if params.startswith('email='): + action = 'contact_view_email' + elif params.startswith('hash='): + action = 'contact_view_hash' + elif params.startswith('id='): + action = 'contact_view' + else: + action = 'contact_view' + rq_url = fmt_params(self.url, action, + self.output, params) + response = rq.get(rq_url) + return response + + +""" +if __name__ == '__main__': + ac = ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY) + + ## add +## user = { +## 'username': 'johnsmith', +## 'first_name': 'John', +## 'last_name': 'Smith', +## 'password': 'mypwd', +## 'password_r': 'mypwd', +## 'email': 'person@example.com', +## 'group[3]' : 3 +## } +## print ac.api('user/add', user) + + ## delete_list +## print ac.api('user/delete_list?ids=3,4') + + ## delete +## print ac.api('user/delete?id=5') + + ## edit +## user = { +## 'username': 'johnsmith', +## 'first_name': 'John', +## 'last_name': 'Smyth', +## 'password': 'mynwqpwd', +## 'password_r': 'mynewpwd', +## 'email': 'person@example.com', +## 'group[3]' : 3, +## 'id' : 6 +## } +## print ac.api('user/edit', user) + + ## list +## print ac.api('user/list?ids=1,6') + + ## me +## print ac.api('user/me') + + ## view email +## print ac.api('user/view?email=person@example.com') + + ## view username +## print ac.api('user/view?username=johnsmith') + + ## view id +## print ac.api('user/view?id=1') + + ## sync +## contact = { +## 'email': 'person@example.com', +## 'first_name': 'John', +## 'last_name': 'Smith', +## 'p[1]': 1, +## 'status[1]': 1, +## } +## print ac.api('contact/sync', contact) + +""" diff --git a/includes/Design.py b/activecampaign/Design.py similarity index 57% rename from includes/Design.py rename to activecampaign/Design.py index 3410b83..01f7dd3 100644 --- a/includes/Design.py +++ b/activecampaign/Design.py @@ -1,27 +1,38 @@ -from Config import ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY -from ActiveCampaign import ActiveCampaign -import json -import urllib2, urllib +from .ActiveCampaign import ( + ActiveCampaign, + fmt_params, + fmt_noparams +) +import requests as rq + class Design(ActiveCampaign): def __init__(self, url, api_key): self.url = url self.api_key = api_key - ActiveCampaign.__init__(self, url, api_key) + super(Design, self).__init__(url, api_key) def edit(self, params, post_data): - request_url = '%s&api_action=branding_edit&api_output=%s' % (self.url, self.output) - post_data = urllib.urlencode(post_data) - req = urllib2.Request(request_url, post_data) - response = json.loads(urllib2.urlopen(req).read()) + rq_url = fmt_noparams( + self.url, + 'branding_edit', + self.output + ) + response = rq.post(rq_url, data=post_data) return response - + def view(self, params, post_data): - request_url = '%s&api_action=branding_view&api_output=%s' % (self.url, self.output) - response = json.loads(urllib2.urlopen(request_url).read()) + rq_url = fmt_noparams( + self.url, + 'branding_view', + self.output + ) + response = rq.get(rq_url) return response - + + +""" if __name__ == '__main__': ac = ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY) @@ -44,3 +55,4 @@ def view(self, params, post_data): ## view print ac.api('branding/view') +""" diff --git a/activecampaign/Form.py b/activecampaign/Form.py new file mode 100644 index 0000000..bdc1878 --- /dev/null +++ b/activecampaign/Form.py @@ -0,0 +1,45 @@ +from .ActiveCampaign import ( + ActiveCampaign, + fmt_params, + fmt_noparams +) +import requests as rq + + +class Form(ActiveCampaign): + + def __init__(self, url, api_key): + self.url = url + self.api_key = api_key + super(Form, self).__init__(url, api_key) + + def getforms(self, params, post_data={}): + rq_url = fmt_noparams( + self.url, + 'form_getforms', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def html(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'form_html', + self.output, + params + ) + response = rq.get(rq_url) + return response + + +""" +if __name__ == '__main__': + ac = ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY) + + ## getforms + #print ac.api('form/getforms') + + ## html + #print ac.api('form/html?id=1142') +""" diff --git a/activecampaign/Group.py b/activecampaign/Group.py new file mode 100644 index 0000000..45fea6b --- /dev/null +++ b/activecampaign/Group.py @@ -0,0 +1,108 @@ +from .ActiveCampaign import ( + ActiveCampaign, + fmt_params, + fmt_noparams +) +import requests as rq + + +class Group(ActiveCampaign): + + def __init__(self, url, api_key): + self.url = url + self.api_key = api_key + super(Group, self).__init__(url, api_key) + + def add(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'group_add', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def delete_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'group_delete_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def delete(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'group_delete', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def edit(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'group_edit', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def list_(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'group_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def view(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'group_view', + self.output, + params + ) + response = rq.get(rq_url) + return response + + +""" +if __name__ == '__main__': + ac = ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY) + + ## add + # group = { + # 'title': 'second-test-group', + # 'descript' : 'This group is created from API', + # 'lists[1]' : 1, + # 'pg_form_edit' : 1, + # 'pg_contact_add' : 1, + # 'pg_contact_delete' : 0 + # } + # print ac.api('group/add', group) + + ## delete_list + #print ac.api('group/delete_list?ids=4,5') + + ## delete + #print ac.api('group/delete?id=6') + + ## edit + # group = { + # 'title': 'second-group', + # 'id' : 7 + # } + # print ac.api('group/edit', group) + + ## list + #print ac.api('group/list?ids=1,7') + + ## view + #print ac.api('group/view?id=7') +""" diff --git a/activecampaign/List.py b/activecampaign/List.py new file mode 100644 index 0000000..e23ce82 --- /dev/null +++ b/activecampaign/List.py @@ -0,0 +1,192 @@ +from .ActiveCampaign import ( + ActiveCampaign, + fmt_params, + fmt_noparams +) +import requests as rq + + +class List(ActiveCampaign): + + def __init__(self, url, api_key): + self.url = url + self.api_key = api_key + super(List, self).__init__(url, api_key) + + def add(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'list_add', + self.output + ) + response = rq.post(rq_url, data=post_data) + return response + + def delete(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'list_delete', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def delete_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'list_delete_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def edit(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'list_edit', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def field_add(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'list_field_add', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def field_delete(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'list_field_delete', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def field_edit(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'list_field_edit', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def field_view(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'list_field_view', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def list_(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'list_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def paginator(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'list_paginator', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def view(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'list_view', + self.output, + params + ) + response = rq.get(rq_url) + return response + + +""" + ## add + #list1 = { + # 'name': 'The List', + # 'sender_name': 'John Smith', + # 'sender_addr1': 'Bd. MK', + # 'sender_city': 'Bucharest', + # 'sender_zip': '123456', + # 'sender_country': 'Romania' + #} + #print ac.api('list/add', list1) + + ## delete + #print ac.api('list/delete?id=2') + + ## delete list + #print ac.api('list/delete?id=1,2') + + ## edit + #list1 = { + # 'id': 11, + # 'name': 'The List Edited', + # 'sender_name': 'John Smith', + # 'sender_addr1': 'Bd. MK', + # 'sender_city': 'Bucharest', + # 'sender_zip': '123456', + # 'sender_country': 'Romania' + #} + #print ac.api('list/edit', list1) + + ## field_add + #field = { + # 'title': 'My Field', + # 'type': 1, + # 'req': 1, + # 'show_in_list': 1, + # 'perstag': '%PERS_11%', + # 'p[11]': 11, + # 'options[label]': 'value' + #} + #print ac.api('list/field_add', field) + + ## field delete + #print ac.api('list/field_delete?id=7') + + ## field_edit + #field = { + # 'id': 7, + # 'title': 'My Field Edited', + # 'type': 1, + # 'req': 0, + # 'show_in_list': 1, + # 'perstag': '%PERS_11%', + # 'p[11]': 11, + # 'options[label]': 'value' + #} + #print ac.api('list/field_edit', field) + + ## field view + #print ac.api('list/field_view?ids=7') + + ## list + #print ac.api('list/list?ids=11,1,10') + + ## paginator + #print ac.api('list/paginator?sort=&offset=0&limit=20&filter=0&public=0') + + ## view + #print ac.api('list/view?id=1') +""" diff --git a/activecampaign/Message.py b/activecampaign/Message.py new file mode 100644 index 0000000..a1e3061 --- /dev/null +++ b/activecampaign/Message.py @@ -0,0 +1,250 @@ +from .ActiveCampaign import ( + ActiveCampaign, + fmt_params, + fmt_noparams +) +import requests as rq + + +class Message(ActiveCampaign): + + def __init__(self, url, api_key): + self.url = url + self.api_key = api_key + super(Message, self).__init__(url, api_key) + + def add(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'message_add', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def delete_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_delete_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def delete(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_delete', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def edit(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'message_edit', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def list_(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def template_add(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'message_template_add', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def template_delete_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_template_delete_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def template_delete(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_template_delete', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def template_edit(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'message_template_edit', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def template_export(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_template_export', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def template_import(self, params, post_data): + rq_url = fmt_noparams( + self.url, + 'message_template_import', + self.output + ) + response = rq.get(rq_url, data=post_data) + return response + + def template_list(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_template_list', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def template_view(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_template_view', + self.output, + params + ) + response = rq.get(rq_url) + return response + + def view(self, params, post_data={}): + rq_url = fmt_params( + self.url, + 'message_view', + self.output, + params + ) + response = rq.get(rq_url) + return response + + +""" +if __name__ == '__main__': + ac = ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY) + + ## add +## message = { +## 'format': 'mime', +## 'subject': 'Fetch at send: %s' % datetime.datetime.now(), +## 'fromemail': 'person@example.com', +## 'fromname': 'John Smith', +## 'reply2': 'reply2@example.com', +## 'priority': 3, +## 'charset': 'utf-8', +## 'encoding': 'quoted-printable', +## 'htmlconstructor': 'external', +## 'htmlfetch': 'http://example.com', +## 'htmlfetchwhen': 'send', +## 'textconstructor': 'external', +## 'textgetch': 'http://example.com', +## 'textfetchwhen': 'send', +## 'p[1]': 1 +## } +## print ac.api('message/add', message) + + ## delete +## print ac.api('message/delete?id=32') + + ## delete_list +## print ac.api('message/delete_list?ids=30,31') + + ## edit +## message = { +## 'id': 1, +## 'subject': 'Fetch at send: %s' % datetime.datetime.now(), +## 'fromemail': 'person@example.com', +## 'p[1]': 1 +## } +## print ac.api('message/edit', message) + + ## list +## print ac.api('message/list?ids=1,2') + + ## template_add +## template = { +## 'name': 'My New Template', +## 'subject': 'New Subject', +## 'html': '' +# api key provided to you by ActiveCampaign +api_key = ' ' -ac = ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY) -print ac.api('account/view') +ac = ActiveCampaign(base_url, api_key) +print(ac.api('account/view'))