def __init__(self, config): conf = config['General'] my_nation = utils.canonical(conf['my_nation']) self.password = conf['password'] user_agent = conf['user_agent'] if user_agent == '': raise exceptions.UserError('You need to set the user agent!') ns_api = nationstates.Nationstates(user_agent=user_agent) ns_api = api_adapter.NS_API(ns_api, my_nation) ns_site = executor.NSSite(user_agent) self.executor = executor.EndorseExecutor(ns_api, ns_site) cache_conf = config['Cache'] cache = data.Cache(info.CACHE_PATH, cache_conf['daily_dump_update_time']) self.ns_data = data.Data(ns_api, cache, info.DATA_DUMP_PATH, conf['my_region'], my_nation, cache_conf['update_from_dump']) self.endorseable_iter = None
def __init__(self, config): ns_api = nationstates.Nationstates(user_agent=config['general']['user_agent']) dispatch_api = api_adapter.DispatchAPI(ns_api) plugin_options = config['plugins'] loader_config = config['loader_config'] self.dispatch_loader = loader.DispatchLoader(plugin_options['dispatch_loader'], loader_config) self.var_loader = loader.VarLoader(plugin_options['var_loader'], loader_config) self.dispatch_config = None bb_config = config['bbcode'] template_config= config['template_renderer'] self.renderer = renderer.DispatchRenderer(self.dispatch_loader, self.var_loader, bb_config, template_config) self.cred_loader = loader.CredLoader(plugin_options['cred_loader'], loader_config) self.creds = utils.CredManager(self.cred_loader, dispatch_api) self.updater = updater.DispatchUpdater(dispatch_api, self.creds, self.renderer, self.dispatch_loader)
def test_create_ns(self): try: api = ns.Nationstates(USERAGENT) except Exception as Err: self.fail(Err)
from random import choice import datetime USERAGENT = "Automated Testing Builds by Travis CL for the nationstates API wrapper by The United Island Tribes. [email protected]" import os test_nation = 'Python Nationstates API wrapper' test_nation_r = 'pynationstates_telegram_recipient' PASSWORD = os.environ.get('password') tgid = os.environ.get('telegram_tgid') key = os.environ.get('telegram_key') client_key = os.environ.get('telegram_clientkey') del os sep_api = ns.Nationstates(USERAGENT) joint_api = ns.Nationstates(USERAGENT) test_nation_nonauth = joint_api.nation(test_nation) test_auth_nation = joint_api.nation(test_nation, password=PASSWORD) test_nation_r = joint_api.nation(test_nation_r) def grab_id(newfactbookresponse_text): part1 = newfactbookresponse_text.split('id=') return part1[1].split('">')[0] class SetupCallTest(unittest.TestCase): def test_create_ns(self): try:
'>>>If you want to start from the first post, press Enter for the next two prompts.<<<' ) print( 'The archive assigns a serial number 1,2,3... to each post. If you are starting from a later post,you may want to change the first serial number. Otherwise press Enter.' ) sid = input('Enter starting post ID : ') sn = input('Enter starting serial number : ') else: sid = sn = '' #This block sets up the API and other stuff. nt = datetime.utcnow().strftime('%Y-%m-%d at %H:%M:%S UTC') init = 1 if sn == '' else int(sn) n = init f = open(fn, mode='a+', errors='replace') api = ns.Nationstates(ua) reg = api.region(rnamef) pid = '0' if sid == '' else sid start = '\nArchive starts from Post ID ' + pid + ' as Post ' + str( n ) + '\n' + '=' * 80 if pid != '0' or n != 1 else '' # Adds archive start info to heading #Prints the heading to the file. f.write( '=' * 80 + '\n' + rname + ' NationStates RMB Archive\non ' + nt + '\n' + '=' * 80 + '\nPython script by Merni\npynationstates API wrapper by DolphDev (github)/United Island Tribes (NS)\n' + '=' * 80 + start) while True: #Gets messages as list 'msgs' of dicts
def test_api_user_agent(self): api = ns.Nationstates(ua) self.assertEqual(api.user_agent, ua) api.user_agent = "New user_agent" self.assertEqual(api.user_agent, "New user_agent")
def init(contact): """ Sets up connection to the nationstates api """ global api, world api = ns.Nationstates("Instance of https://github.com/bekaertruben/ns-census-maximizer [contact: {}]".format(contact)) world = api.world()
# Time Version # Must be left running import nationstates import time #When to rerun days = 15 api = nationstates.Nationstates("YOUR USERAGENT") nation = api.nation("testlandia", password="******") while True: nation.get_shards("ping") time.sleep(days*24*60) # CRON JOB VERSION # configure OS to run this script every 15 days or so import nationstates #This can be done in one line nationstates.Nationstates("YOUR USERAGENT").nation("testlandia", password="******").get_shards("ping")
import nationstates api = nationstates.Nationstates("Example for Nationstates API for python") print(api.nation("TestLandia").flag)
def post(gui, *args): nation = gui.nation config.read(config_file) password = config[nation]['password'] path = config[nation]["path"] useragent = config[nation]["useragent"] api = nationstates.Nationstates("Glaciosia-Dispatcher run by " + useragent) nation = api.nation(nation, password) with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): post = frontmatter.load(entry.path) category = post['category'] subcategory = post['subcategory'] category_number = cats[category] subcategory_number = subcats[category][subcategory] titletext = post['title'] content = post.content print(category, subcategory, category_number, subcategory_number, titletext, content) if 'id' in post.metadata: dispatch_id = post["id"] r = nation.command('dispatch', dispatch='edit', mode='prepare', dispatchid=dispatch_id, title=titletext, text=content, category=category_number, subcategory=subcategory_number) print(r) token = r['success'] nation.command('dispatch', token=token, dispatch='edit', mode='execute', dispatchid=dispatch_id, title=titletext, text=content, category=category_number, subcategory=subcategory_number) else: r = nation.command('dispatch', dispatch='add', mode='prepare', title=titletext, text=content, category=category_number, subcategory=subcategory_number) print(r) token = r['success'] r = nation.command('dispatch', token=token, dispatch='add', mode='execute', title=titletext, text=content, category=category_number, subcategory=subcategory_number) id = r['success'] digits = re.compile(r'[0-9]+') id = digits.findall(id) print(id[0]) post['id'] = id[0] frontmatter.dump(post, entry.path)
""" Nationstates API Wrapper. This example prints the number of nations in the world every 30 seconds """ import time import nationstates api = nationstates.Nationstates( "Collecting the number of nations in the world") world = api.world() while True: print(world.numnations) time.sleep(30)