def update(username, datadir="."): urls_fn = os.path.join(datadir, "urls.csv") subs_fn = os.path.join(datadir, "subs.csv") messages_fn = os.path.join(datadir, "messages.csv") session = tinyapi.Session(username, getpass()) messages = session.get_messages() urls = session.get_urls() subs = session.get_subscribers() # each of the above is a list of potentially nested dicts. Make tidy # ignore private keys starting with __. For urls, that's it. filtered_urls = [filter_private_keys(url) for url in urls] # probably not the most performant to make a dataframe just to immediately # toss into a csv... # bug currently in pandas where to_csv encoding default is system (e.g., # cp-1252 on windows), not utf-8: # https://github.com/pandas-dev/pandas/issues/17097 # so, explicitly use utf-8. pd.DataFrame(filtered_urls).to_csv(urls_fn, encoding="utf8") # dicts in subs contain two subdicts, 'stats' and 'data'. We only care # about 'stats'. for sub in subs: del sub['data'] flat_filter_subs = [flatten_dict(filter_private_keys(sub)) for sub in subs] pd.DataFrame(flat_filter_subs).to_csv(subs_fn, encoding="utf8") # for messages, also dump extra cruft of ['stats']['most_clicked_urls'] -- # duplicated by data in urls. for message in messages: del message['stats']['most_clicked_urls'] flat_filter_ms = [flatten_dict(filter_private_keys(m)) for m in messages] pd.DataFrame(flat_filter_ms).to_csv(messages_fn, encoding="utf8")
def __init__(self, bot, slack, combolist=None): super().__init__(bot, slack) self.directed = True if not combolist: self.combolist = os.environ['TINYLETTER_COMBOLIST'] else: self.combolist = combolist self.sessions = {} for combo in self.combolist.split(): try: u = combo.split(':')[0] p = combo.split(':')[1] allowed_domains = combo.split(':')[2].split(',') self.sessions[u] = { 'session': tinyapi.Session(username=u, password=p), 'allowed_domains': allowed_domains } except: print('Invalid credentials for account {}'.format(u)) continue self.config_section = 'tinyletter' self.monitor_changes()
def test_get_profile(self): session = tinyapi.Session(USERNAME, "incorrect-password")
def setUp(self): self.session = tinyapi.Session(USERNAME, PASSWORD)
import tinyapi from getpass import getpass session = tinyapi.Session("liguoqinjim", getpass()) draft = session.create_draft() draft.subject = "Testing TinyAPI" draft.body = "Just a test." draft.save() draft.send_preview()
import logging import os import tinyapi username = os.getenv("TinyUrlUsername") password = os.getenv("TinyUrlPassword") session = tinyapi.Session(username, password) def send_updates(latest_posts): for post in latest_posts: draft = session.create_draft() draft.subject = post.title draft.body = post.link draft.body += post['content'].pop(0).value draft.save() draft.send()
import tinyapi from getpass import getpass session = tinyapi.Session("tinyapi-test-account", getpass()) # Print data about your most-clicked URL: print(session.get_urls(count=1)) print("\n---\n") # Print the open rates of your 5 most recent letters: messages = session.get_messages(count=5) for m in messages: print(m["stub"] + ": " + m["stats"]["open_rate"])
import argparse import sys import datetime import os parser = argparse.ArgumentParser() parser.add_argument('--body', help='Which file to read', type=str) parser.add_argument('--title', help='A title for the newsletter', type=str) parser.add_argument('--id', help='The id of a newsletter', type=int) parser.add_argument('--quick', help="Send without saving draft", action='store_true') group = parser.add_mutually_exclusive_group() group.add_argument('-preview', help='preview a newsletter', action='store_true') group.add_argument('-send', help='Send a newsletter', action='store_true') args = parser.parse_args() session = tinyapi.Session(os.environ.get("TINYLETTER_USERNAME"), os.environ.get("TINYLETTER_PASSWORD")) #getpass()) if args.preview or args.send: if args.id: draft = session.edit_draft(message_id=args.id) if args.preview: draft.send_preview() if args.send: draft.send() else: drafts = session.get_drafts() for x in drafts: print("{0} - {1}. {2}".format(x['id'], x['subject'], datetime.datetime.fromtimestamp(x['updated_at']).strftime('%Y-%m-%d %H:%M:%S')))