def __init__(self, id=None): DB_CON.register([TopicDoc]) datastroe = DB_CON[DB_NAME] col_name = TopicDoc.__collection__ collection = datastroe[col_name] doc = collection.TopicDoc() API.__init__(self, id=id, col_name=col_name, collection=collection, doc=doc)
def show_categories(section_id): """ Categories page :param section_id: Selected section ID :return: """ api = API() for item in api.get_categories(int(section_id)): # list item: li = ListItem(item['name'].capitalize(), iconImage='{0}/{1}'.format(config['urls']['calm_arts_host'], item['image']), thumbnailImage='{0}/{1}'.format(config['urls']['calm_arts_host'], item['image'])) li.setArt({ 'fanart': '{0}{1}'.format(config['urls']['calm_blurred_arts_host'], item['image']) }) # directory item: addDirectoryItem( PLUGIN.handle, PLUGIN.url_for(show_channels, section_id=section_id, category_id=item['id']), li, True ) # end of directory: endOfDirectory(PLUGIN.handle) executebuiltin('Container.SetViewMode({0})'.format( config['viewmodes']['thumbnail'][getSkinDir() if getSkinDir() in config['viewmodes']['thumbnail'] else 'skin.confluence'] ))
def __init__(self, db_name=DB_NAME): DB_CON.register([EventDoc]) datastore = DB_CON[db_name] col_name = EventDoc.__collection__ collection = datastore[col_name] doc = collection.EventDoc() API.__init__(self, db_name=db_name, col_name=col_name, collection=collection, doc=doc)
def __init__(self): DB_CON.register([UserDoc]) datastore = DB_CON[DB_NAME] col_name = UserDoc.__collection__ collection = datastore[col_name] doc = collection.UserDoc() API.__init__(self, col_name=col_name, collection=collection, doc=doc)
def __init__(self): DB_CON.register([TimeLineDoc]) datastore = DB_CON[DB_NAME] col_name = TimeLineDoc.__collection__ collection = datastore[col_name] doc = collection.TimeLineDoc() self.staff = StaffAPI() API.__init__(self, col_name=col_name, collection=collection, doc=doc)
def __init__(self): DB_CON.register([CollectDoc]) datastore = DB_CON[DB_NAME] col_name = CollectDoc.__collection__ collection = datastore[col_name] doc = collection.CollectDoc() self.vol = Volume() API.__init__(self, col_name=col_name, collection=collection, doc=doc)
def get_audio_list(owner_id, count=6000): api = API() params = { 'method': 'audio.get', 'owner_id': owner_id, 'count': count, } return api.call_api(**params)['response']
def handle_delete(self): if self.resource_id is None: return api = API(self._url(), self._auth(), self.properties["verify"]) api.application_delete(self.resource_id) return self.resource_id
def __init__(self): DB_CON.register([StaffDoc]) datastore = DB_CON[DB_NAME] col_name = StaffDoc.__collection__ collection = datastore[col_name] doc = collection.StaffDoc() self.p = Permission() API.__init__(self, col_name=col_name, collection=collection, doc=doc)
def get_username(self): if self.username is None: api = API(self) user = api.verify_credentials() if user: self.username = user.screen_name else: raise WeibopError("Unable to get username, invalid oauth token!") return self.username
def __init__(self): DB_CON.register([ItemDoc]) datastore = DB_CON[DB_NAME] col_name = ItemDoc.__collection__ collection = datastore[col_name] doc = collection.ItemDoc() self.rpl = Reply() self.stf = Staff() self.alt = Alert() API.__init__(self, col_name=col_name, collection=collection, doc=doc)
def __init__(self, uid, eid, stamp): self.uid = uid self.eid = eid self.stamp = stamp DB_CON.register([TimelineDoc]) self.datastroe = DB_CON[DB_NAME] col_name = stamp+'timeline' collection = self.datastroe[col_name] doc = collection.TimelineDoc() API.__init__(self, id=None, col_name=col_name, collection=collection, doc=doc)
def update_userpage(username, cache_minutes=20): try: cachetime = UserInfo.objects.get(pk=username).cache except UserInfo.DoesNotExist: # Force updating cache cachetime = timezone.now() - datetime.timedelta(days=1) if cachetime + datetime.timedelta(minutes=cache_minutes) < timezone.now(): API.userpage(username=username) return None else: return cachetime
def main(): global api root_app = bottle.Bottle() result = config.validate(validator, copy = True) if result is False: print "Config file validation failed" sys.exit(1) api = API(app, config, VERSION) ConfigAPI(api, config) tellstick = TellstickAPI(api, config, VERSION) scheduler = Scheduler(config, tellstick) SchedulerAPI(api, config) class ConfigWatcher(object): def notify(self, observable, key): print "writing" config.write() watcher = ConfigWatcher() config.observe(watcher) if config['installed'] != VERSION: api.install() if config['webroot']: root_app.mount(config['webroot'], app) else: root_app.merge(app) session_opts = { 'session.type': 'cookie', 'session.validate_key': config['cookieKey'], 'session.auto': True, } bottle.run(SessionMiddleware(root_app, session_opts), host = config['host'], port = config['port'], debug = config['debug'], reloader = False, server = 'cherrypy') if scheduler: scheduler.stop() # The goal was to have all changes to the config call the watcher # method which writes to file. Unfortunately subsections wouldn't # play along, so write to file once we've finished with the service config.write()
def show_favorites(): """ User's favorite channels list :return: """ api = API() user = User() is_authenticated = user.authenticate() if is_authenticated: favorites = api.get_favorites(user.username, user.token) if len(favorites) > 0: for item in favorites: # list item: li = ListItem(u'{0} {1}'.format(item['title'].replace('CALM RADIO -', '').title(), '(VIP)' if 'free' not in item['streams'] else '', item['description']), iconImage='{0}/{1}'.format(config['urls']['calm_arts_host'], item['image']), thumbnailImage='{0}/{1}'.format(config['urls']['calm_arts_host'], item['image'])) li.setArt({ 'fanart': '{0}{1}'.format(config['urls']['calm_blurred_arts_host'], item['image']) }) li.addContextMenuItems( [(ADDON.getLocalizedString(32301), 'RunPlugin(plugin://{0}/favorites/remove/{1})' .format(ADDON_ID, item['id']))] ) # directory item: addDirectoryItem( PLUGIN.handle, PLUGIN.url_for(play_channel, category_id=item['category'], channel_id=item['id']), li ) # set the content of the directory setContent(ADDON_HANDLE, 'songs') # end of directory: endOfDirectory(PLUGIN.handle) executebuiltin('Container.SetViewMode({0})'.format( config['viewmodes']['thumbnail'][getSkinDir() if getSkinDir() in config['viewmodes']['thumbnail'] else 'skin.confluence'] )) # favorites list is empty: else: executebuiltin('Notification("{0}", "{1}")' .format(ADDON.getLocalizedString(30000), ADDON.getLocalizedString(32306))) # user is not authenticated: else: executebuiltin('Notification("{0}", "{1}")' .format(ADDON.getLocalizedString(30000), ADDON.getLocalizedString(32110)))
def main_game(game_id): global API API.clear() last_command_id = 0 end = False data_user = API.user() while not end: pipes = API.pipes(game_id, last_command_id) last_command_id = pipes["last_id"] for command in pipes["commands"]: if command["type"] == "1" and command["user_id"] == data_user["id"] or command["type"] == "0": exec(command["command"]) if not pipes["commands"]: time.sleep(1)
def update_artwork(channel, recent_tracks_url): """ Update current channel info :param channel: Channel object :param recent_tracks_url: Recent tracks URL :return: """ artwork = ArtworkWindow() api = API() last_album_cover = '' # update now playing fanart, channel name & description: artwork.channel.setLabel('[B]' + channel['title'] + '[/B]') artwork.description.setText(channel['description']) artwork.show() artwork_id = getCurrentWindowDialogId() while getCondVisibility('Window.IsVisible({0})'.format(artwork_id)): recent_tracks = api.get_json('{0}?{1}'.format(recent_tracks_url, str(int(time())))) if last_album_cover != recent_tracks['now_playing']['album_art']: last_album_cover = recent_tracks['now_playing']['album_art'] # urlretrieve('{0}/{1}'.format(config['urls']['calm_arts_host'], # recent_tracks['now_playing']['album_art']), # '{0}{1}'.format(ADDON_DATA_FOLDER, recent_tracks['now_playing']['album_art'])) # artwork.cover.setImage('{0}/{1}'.format(ADDON_DATA_FOLDER, recent_tracks['now_playing']['album_art'])) artwork.cover.setImage('{0}/{1}'.format(config['urls']['calm_arts_host'], recent_tracks['now_playing']['album_art'])) artwork.song.setLabel('[B]' + recent_tracks['now_playing']['title'] + '[/B]') artwork.album.setLabel('[B]Album[/B]: ' + (recent_tracks['now_playing']['album'] if recent_tracks['now_playing']['album'] else 'N/A')) artwork.artist.setLabel('[B]Artist[/B]: ' + recent_tracks['now_playing']['artist']) # next track: artwork.next_1.setLabel('- [B]{0}[/B] by {1}'.format( recent_tracks['next_playing']['title'], recent_tracks['next_playing']['artist'] )) # recent tracks: artwork.recent_1.setLabel('- [B]{0}[/B] by {1}'.format( recent_tracks['recently_played'][0]['title'], recent_tracks['recently_played'][0]['artist'] )) artwork.recent_2.setLabel('- [B]{0}[/B] by {1}'.format( recent_tracks['recently_played'][1]['title'], recent_tracks['recently_played'][1]['artist'] )) artwork.recent_3.setLabel('- [B]{0}[/B] by {1}'.format( recent_tracks['recently_played'][2]['title'], recent_tracks['recently_played'][2]['artist'] )) sleep(5000) log('Artwork closed') del artwork
def update_comments(itemid, cache_minutes=20): try: # cachetime = HNCommentsCache.objects.get(pk=itemid).time cachetime = HNComments.objects.get(pk=itemid).cache except HNComments.DoesNotExist: try: cachetime = Stories.objects.get(pk=itemid).cache except Stories.DoesNotExist: # Force updating cache cachetime = timezone.now() - datetime.timedelta(days=1) if not cachetime or cachetime + datetime.timedelta(minutes=cache_minutes) < timezone.now(): API.comments(itemid=itemid, cache_minutes=cache_minutes) return None else: return cachetime
def __init__(self, parent = None, url = '', width = None, height = None, isDialog = False): super(Window, self).__init__(parent if isDialog else None) assets.windows.append(self) if width is None: width = assets.manifest['width'] if height is None: height = assets.manifest['height'] windowFlags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint if isDialog: windowFlags |= Qt.Dialog else: windowFlags |= Qt.CustomizeWindowHint self.dragParams = {'type': 0, 'x': 0, 'y': 0, 'size': 5, 'margin': 0, 'draging': False} self.webView = WebView(self, url) self.api = API(self) self.parent = parent self.resize(width, height) self.setMouseTracking(True) self.setWindowFlags(windowFlags) self.setWindowTitle('Hybrid App Engine') self.setAttribute(Qt.WA_QuitOnClose, True) self.setAttribute(Qt.WA_DeleteOnClose, True) self.setAttribute(Qt.WA_TranslucentBackground, True) self.setVisible(assets.manifest['visible']) self.setResizable(assets.manifest['resizable']) self.setFrameless(assets.manifest['frameless']) self.setWindowIcon(QIcon(assets.manifest['icon'])) self.setTransBackground(assets.manifest['transBackground'])
def add_to_favorites(channel_id): """ Adds a channels to user's favorites list :param channel_id: Channel ID :return: """ api = API() user = User() is_authenticated = user.authenticate() if is_authenticated: api.add_to_favorites(user.username, user.token, channel_id) executebuiltin('Notification("{0}", "{1}"'.format(ADDON.getLocalizedString(30000), ADDON.getLocalizedString(32302))) else: executebuiltin('Notification("{0}", "{1}")'.format(ADDON.getLocalizedString(30000), ADDON.getLocalizedString(32110)))
def set_access_token(self, access_token, expires): ''' set access_token and expires time to this instance ''' self.access_token = access_token self.expires = float(expires) self.api = API(access_token, self.client_secret)
def doProcessRender(self, request): renderObj = WebServiceObject() try: if request.method == "GET": renderObj = API.getGeneLink(request) elif request.method == "POST": renderObj = API.createGeneLink(request) elif request.method == "DELETE": renderObj = API.deleteGeneLink(request) else: renderObj.setError(Errors.INVALID_METHOD.setCustom(request.method)) except Errors.WebServiceException as e: renderObj.setError(e) self.setJsonObject(renderObj.getObject()) self.setStatus(renderObj.getCode())
def __init__(self, wrapper): self.wrapper = wrapper self.api = API(wrapper, "Web", internal=True) self.log = log.PluginLog(self.wrapper.log, "Web") self.config = wrapper.config self.socket = False self.data = storage.Storage("web", self.log) if "keys" not in self.data: self.data["keys"] = [] #if not self.config["Web"]["web-password"] == None: # self.log.info("Changing web-mode password because web-password was changed in wrapper.properties") # self.data["password"] = md5.md5(self.config["Web"]["web-password"]).hexdigest() # self.config["Web"]["web-password"] = None # self.wrapper.configManager.save() self.api.registerEvent("server.consoleMessage", self.onServerConsole) self.api.registerEvent("player.message", self.onPlayerMessage) self.api.registerEvent("player.join", self.onPlayerJoin) self.api.registerEvent("player.leave", self.onPlayerLeave) self.api.registerEvent("irc.message", self.onChannelMessage) self.consoleScrollback = [] self.chatScrollback = [] self.memoryGraph = [] self.loginAttempts = 0 self.lastAttempt = 0 self.disableLogins = 0
def play_channel(category_id, channel_id): """ Plays selected song :param category_id: Selected category ID :param channel_id: Selected channel ID :return: """ api = API() user = User() is_authenticated = user.authenticate() recent_tracks_url = '' channel = [item for item in api.get_channels(int(category_id)) if item['id'] == int(channel_id)][0] url = api.get_streaming_url(channel['streams'], user.username, user.token, user.is_authenticated()) if is_authenticated: recent_tracks_url = channel['recent_tracks']['vip'] elif 'free' in channel['recent_tracks']: recent_tracks_url = channel['recent_tracks']['free'] # is there a valid URL for channel? if url: url = quote(url, safe=':/?=@') li = ListItem(channel['title'], channel['description'], channel['image']) li.setArt({'thumb': '{0}/{1}'.format(config['urls']['calm_arts_host'], channel['image']), 'fanart': '{0}{1}'.format(config['urls']['calm_blurred_arts_host'], channel['image'])}) li.setInfo('music', {'Title': channel['title'].replace('CALM RADIO -', '').title(), 'Artist': channel['description']}) li.setProperty('mimetype', 'audio/mpeg') li.setProperty('IsPlayable', 'true') li.setInfo('music', { 'Title': channel['title'].replace('CALM RADIO -', '').title() }) Player().play(item=url, listitem=li) log('Playing url: {0}'.format(url)) update_artwork(channel, recent_tracks_url) else: # members only access dialog = Dialog() ret = dialog.yesno(ADDON.getLocalizedString(32200), ADDON.getLocalizedString(32201)) if ret == 1: ADDON.openSettings()
def __init__(self): print("Initializing library...") parser = ConfigParser.ConfigParser() parser.read("pifm_cfg.config") self.musicpath = parser.get("PiFM", "musicdir") self.api = API() self.connection = sqlite3.connect("pifm_music.db") self.checkIfDBExists()
def push(self): parameter = core.parameter_encode(self.attributes) parameter["command"] = "update_project" res = API.post(parameter) if res: print res return True else: return False
def user_threads(): username = request.args.get('id') api = API.using_request(request) api_response = api.user(username, 'threads') rss_title = 'Hacker News: %s threads RSS feed' % username rss_link = 'https://news.ycombinator.com/threads?id=%s' % username rss = RSS(api_response, rss_title, rss_link) return rss.response()
def is_macedonian_user(api, iduser, all_users, max_followers=70000, threshold_followers=0.01, threshold_friends=0.01): user = api.get_user(id=iduser) # We already have the users with most followers, so if there is someone with more followers, then he is not macedonian if user.followers_count > max_followers or user.friends_count > max_followers: return False, set(), set() friends = API.get_friends_ids(api, iduser) followers = API.get_followers_ids(api, iduser) macedonian_friends = len(friends & all_users) macedonian_followers = len(followers & all_users) # If greater part of friends and followers are macedonians, then this user is probably also macedonian print user.screen_name return 1.0 * macedonian_friends / (1 + len(friends)) > threshold_friends and \ 1.0 * macedonian_followers / (1 + len(followers)) > threshold_followers, \ [(iduser, f) for f in friends], [(iduser, f) for f in followers]
def delete(self): parameter = self.get_parameters() parameter["command"] = "remove_task_metadata" res = API.post(parameter) if res: print 'Delete ', res return True else: return False
def new_comments(): query = request.args.get('q') api = API.using_request(request) if query: rss_title = 'Hacker News: "%s" comments' % query else: rss_title = 'Hacker News: New Comments' rss = RSS(api.comments(), rss_title, 'https://news.ycombinator.com/newcomments') return rss.response()
type=int, default=0, help="Execute tasks due in next X days.") parser.add_argument("-v", dest="verbose", action="store_true", help="Verbose output.") parser.add_argument("--template", help="Run template file TEMPLATE") args = parser.parse_args() directory = pathlib.Path(__file__).resolve().parent if __name__ == "__main__": conf_file = directory / "todoist_scheduler.conf" if not conf_file.is_file(): api = API.create_new(directory, missing_conf=True) else: conf = toml.load(str(conf_file)) token_location = conf.get("token") if not token_location: api = API.create_new(directory, missing_location=True) else: token = pickle.load(open(token_location, "rb")) if not token: api = API.create_new(directory, missing_token=True) else: api = API(token) if api.valid: if args.template: execute_template(args.template, api) else:
from api.metron.endpoint import metronThreats from api.notification.endpoint import manageNotifications #from api.metron_data.endpoint import asset_discovery, threat_intel @app.errorhandler(400) def bad_request(error): return make_response(jsonify({'error': 'Bad request'}), 400) @app.errorhandler(404) def not_found(error): return make_response(jsonify({'error': 'Not found'}), 404) API.add_resource(manageNotifications, '/api/notifications/email', '/api/notifications/sms', '/api/notifications/alert') API.add_resource(metronThreats, '/api/metron/threats/<string:_device_>') API.add_resource( manageAssets, '/api/assets', '/api/assets/<string:_company_name_>/<string:_sites_>/<string:_asset_ip_>' ) #'/api/assets/<string:_company_name_>', '/api/assets/<string:_company_name_>/<string:_sites_>' API.add_resource(piController, '/api/picontroller/time', '/api/picontroller/<string:_mac_address_>') API.add_resource(manageAgent, '/api/agent/<string:_mac_address_>') API.add_resource(manageAgentList, '/api/agent/list') API.add_resource(userAuth, '/api/auth/<string:_username>/<string:_password>') API.add_resource(manageUser, '/api/user/<string:_username_>') API.add_resource(manageUserList, '/api/user/list')
#!/usr/bin/python2.7 from inputargs import Parser from api import API from Log import Logger logger = Logger() parser = Parser() args = parser.argparser.parse_args() api = API(args) def main(): if args.debug: logger.setlevel('DEBUG') if args.verbose: logger.setlevel('INFO') if args.quiet: logger.setlevel('ERROR') if hasattr(args, 'mngr_subparser'): if args.mngr_subparser == 'get': api.manager_get() if hasattr(args, 'mngr_subparser'): if args.mngr_subparser == 'set': api.manager_set() if hasattr(args, 'ini_subparser'): if args.ini_subparser == 'get':
#!/usr/bin/python # coding:utf-8 from api import API import w2v_dialog import MeCab from crontab import CronTab with API() as api: # ツイートする # api.tweet('tweet test2') SV = w2v_dialog.sentence_vectorizer('/work/asakura/bot/matrix50.model') # リプを取得 for mention in api.get_mentions(): text = mention['text'] tweet_id = mention['id_str'] screen_name = mention['user']['screen_name'] # ユーザ情報(プロフィール) name = mention['user']['name'] zone = mention['user']['time_zone'] location = mention['user']['location'] description = mention['user']['description'] print mention #print text, tweet_id, screen_name, name, zone, location, description tagger = MeCab.Tagger('-Owakati') #result = tagger.parse(' '.join(text.split()[1:]).decode('utf-8')) result = tagger.parse(' '.join(text.split()[1:]).encode('utf-8'))
def get_json_response(self, method): """ Pega a resposta da requisição em json. """ api = API(self.params["license_plate"], self.params["renavam"], method) return api.fetch()
from api import API from middleware import Middleware app = API() # app() @app.route("/home") def home(request, response): response.text = "Hello from HOME page" @app.route("/about") def about(request, response): response.text = "Hello from ABOUT page" @app.route("/hello/{name}") def greeting(request, response, name): response.text = f"Hello, {name}" @app.route("/sum/{num_1:d}/{num_2:d}") def sum(request, response, num_1, num_2): total = int(num_1) + int(num_2) response.text = f"{num_1} + {num_2} = {total}" @app.route("/book") class BooksResource: def get(self, request, response):
def show(): api = API.using_request(request) rss = RSS(api.show_hn(), 'Hacker News: Show HN') return rss.response()
def all(cls, project_id, flags): api = API(project_id) accounts = api.accounts() for account in accounts: account_name = account.get("name") locations = api.locations(account_name) for location in locations: location_name = location.get("name") if flags[INSIGHTS]: api.insights(location_name) if flags[DIRECTIONS]: api.directions(location_name) if flags[HOURLY_CALLS]: api.hourly_calls(location_name) if flags[REVIEWS]: api.reviews(location_name) if flags[SENTIMENT]: api.sentiments()
def polls(): api = API.using_request(request) rss = RSS(api.polls(), 'Hacker News: Polls') return rss.response()
def sentiment_only(cls, project_id): api = API(project_id) api.sentiments()
from system import System from config import Config from convolver import Convolver from api import API app = Flask(__name__, static_url_path='/', static_folder='dist') DEV = False if DEV: from flask_cors import CORS CORS(app) system = System() config = Config(system) convolver = Convolver(config) api = API(config) @app.route('/api', methods=['get']) def get(): resp = jsonify({ 'config': dict(config.config), 'system': api.get_system_status(), 'convolver': { 'choices': convolver.get_filter_choices() } }) return resp @app.route('/api/iwlist', methods=['get'])
# -*- coding: utf-8 -*- # # # Author: alex # Created Time: 2018年04月02日 星期一 14时53分50秒 from api import API, run class Example: def hello(self, name='world'): return 'Hello {name}!'.format(name=name) if __name__ == '__main__': API(Example) run(debug=True)
def for_account_and_location(cls, project_id, account_id, location_id, flags): api = API(project_id) location_name = u"accounts/{}/locations/{}".format( account_id, location_id) api.locations(u"accounts/{}".format(account_id), location_id=location_name) if flags[INSIGHTS]: api.insights(location_name) if flags[DIRECTIONS]: api.directions(location_name) if flags[HOURLY_CALLS]: api.hourly_calls(location_name) if flags[REVIEWS]: api.reviews(location_name) if flags[SENTIMENT]: api.sentiments()
def __init__(self, config_file): self.config_file = config_file self.conf = Config(self.config_file) self.auth = AuthController(self.config_file) self.api = API(self.config_file)
def run_experiment(namespace, config, filename): handle = API(namespace, edl_job_config=config) results = [] stop_flag = False start_time = time.time() def monitor(handle): while not stop_flag: res = handle.get_expriment1_cpus(start_time) results.append(res) time.sleep(2) monitor_thread = Thread(target=monitor, args=(handle, )) monitor_thread.daemon = True job_count = 2 # 1. create jobs edl_jobs = handle.create_edl_jobs(job_count) monitor_thread.start() if filename == "experiment1-studygroup": # 2. start edl jobs for job in edl_jobs: handle.start_edl_job(job) time.sleep(100) # 3. wait for some edl jobs complete handle.wait_edl_job_completed(edl_jobs[0]) handle.delete_edl_job(edl_jobs[0]) handle.wait_edl_job_completed(edl_jobs[1]) handle.delete_edl_job(edl_jobs[1]) else: handle.start_edl_job(edl_jobs[0]) handle.wait_edl_job_completed(edl_jobs[0]) handle.delete_edl_job(edl_jobs[0]) handle.start_edl_job(edl_jobs[1]) handle.wait_edl_job_completed(edl_jobs[1]) handle.delete_edl_job(edl_jobs[1]) # 4. stop job stop_flag = True monitor_thread.join() # 5. save results with open(filename + ".csv", "w") as f: f.write("time,edl_job\n") for res in results: res = [str(r) for r in res] f.write(",".join(res) + "\n")
def for_account(cls, project_id, account_id, flags): api = API(project_id) locations = api.locations(u"accounts/{}".format(account_id)) for location in locations: location_name = location.get("name") if flags[INSIGHTS]: api.insights(location_name) if flags[DIRECTIONS]: api.directions(location_name) if flags[HOURLY_CALLS]: api.hourly_calls(location_name) if flags[REVIEWS]: api.reviews(location_name) if flags[SENTIMENT]: api.sentiments()
def note(note_id): token = request.cookies["token"] _, content = API.get_note_detail(token, note_id) return render_template("note.html", content=content, note_id=note_id)
}, 'test': { 'datasets': { 'redd': { 'path': path, 'buildings': { 1: { 'start_time': '2011-05-11', 'end_time': '2011-05-24' }, 2: { 'start_time': '2011-04-27', 'end_time': '2011-05-22' }, 3: { 'start_time': '2011-05-23', 'end_time': '2011-05-30' }, # 4: { # 'start_time': '2011-05-23', # 'end_time': '2011-06-03' # } } } } } } res = API(ex_train_microwave)
class BasePlugin: mqttClient = None def onStart(self): self.debugging = Parameters["Mode6"] if self.debugging == "Verbose": Domoticz.Debugging(2 + 4 + 8 + 16 + 64) if self.debugging == "Debug": Domoticz.Debugging(2) Domoticz.Debug("onStart called") self.install() self.base_topic = Parameters["Mode1"].strip() self.subscribed_for_devices = False mqtt_server_address = Parameters["Address"].strip() mqtt_server_port = Parameters["Port"].strip() mqtt_client_id = Parameters["Mode3"].strip() self.mqttClient = MqttClient(mqtt_server_address, mqtt_server_port, mqtt_client_id, self.onMQTTConnected, self.onMQTTDisconnected, self.onMQTTPublish, self.onMQTTSubscribed) self.api = API(Devices, self.onApiCommand) self.devices_manager = DevicesManager() self.groups_manager = GroupsManager() def checkDevices(self): Domoticz.Debug("checkDevices called") def onStop(self): Domoticz.Debug("onStop called") self.uninstall() def onCommand(self, Unit, Command, Level, Color): Domoticz.Debug("onCommand: " + Command + ", level (" + str(Level) + ") Color:" + Color) message = None device = Devices[ Unit] #Devices is Domoticz collection of devices for this hardware device_params = device.DeviceID.split('_') entity_id = device_params[0] if (self.devices_manager.get_device_by_id(entity_id) != None): message = self.devices_manager.handle_command( Devices, device, Command, Level, Color) elif (self.groups_manager.get_group_by_deviceid(device.DeviceID) != None): message = self.groups_manager.handle_command( device, Command, Level, Color) else: Domoticz.Log('Can\'t process command from device "' + device.Name + '"') if (message != None): self.publishToMqtt(message['topic'], message['payload']) def onApiCommand(self, command, data): if command == 'publish_mqtt': return self.publishToMqtt(data['topic'], data['payload']) elif command == 'remove_device': return self.devices_manager.remove(Devices, data) else: Domoticz.Error('Internal API command "' + command + '" is not supported by plugin') def publishToMqtt(self, topic, payload): self.mqttClient.publish(self.base_topic + '/' + topic, payload) def onConnect(self, Connection, Status, Description): Domoticz.Debug("onConnect called") self.mqttClient.onConnect(Connection, Status, Description) def onDisconnect(self, Connection): self.mqttClient.onDisconnect(Connection) def onDeviceModified(self, unit): if (unit == 255): self.api.handle_request(Devices[unit].sValue) return def onMessage(self, Connection, Data): self.mqttClient.onMessage(Connection, Data) def onHeartbeat(self): self.mqttClient.onHeartbeat() def onMQTTConnected(self): self.mqttClient.subscribe([self.base_topic + '/bridge/#']) def onMQTTDisconnected(self): self.subscribed_for_devices = False def onMQTTSubscribed(self): Domoticz.Debug("onMQTTSubscribed") def onMQTTPublish(self, topic, message): Domoticz.Debug("MQTT message: " + topic + " " + str(message)) topic = topic.replace(self.base_topic + '/', '') self.api.handle_mqtt_message(topic, message) if (topic == 'bridge/config/permit_join' or topic == 'bridge/config/devices'): return if (topic == 'bridge/config'): permit_join = 'enabled' if message['permit_join'] else 'disabled' Domoticz.Debug('Zigbee2mqtt log level is ' + message['log_level']) Domoticz.Log('Joining new devices is ' + permit_join + ' on the zigbee bridge') return if (topic == 'bridge/state'): Domoticz.Log('Zigbee2mqtt bridge is ' + message) if message == 'online': self.publishToMqtt('bridge/config/devices', '') self.publishToMqtt('bridge/config/groups', '') return if (topic == 'bridge/log'): is_connected = message['type'] == 'device_connected' is_removed = message['type'] == 'device_removed' is_paired = message['type'] == 'pairing' and message[ 'message'] == 'interview_successful' if message['type'] == 'devices': Domoticz.Log('Received available devices list from bridge') self.devices_manager.clear() self.devices_manager.update(Devices, message['message']) if self.subscribed_for_devices == False: self.mqttClient.subscribe([self.base_topic + '/+']) self.subscribed_for_devices = True if message['type'] == 'groups': Domoticz.Log('Received groups list from bridge') self.groups_manager.register_groups(Devices, message['message']) if is_connected or is_removed or is_paired: self.publishToMqtt('bridge/config/devices', '') if message['type'] == 'ota_update': Domoticz.Log(message['message']) if message['type'] == 'zigbee_publish_error': #an error occured on publish to the zigbee network deviceMeta = message['meta'] Domoticz.Error("A Zigbee publish error occured for device '" + deviceMeta['friendly_name'] + "' with error message: " + message['message']) return if (self.devices_manager.get_device_by_name(topic) != None): self.devices_manager.handle_mqtt_message(Devices, topic, message) elif (self.groups_manager.get_group_by_name(topic) != None): self.groups_manager.handle_mqtt_message(topic, message) def install(self): Domoticz.Log('Installing plugin custom page...') try: source_path = os.path.dirname( os.path.abspath(__file__)) + '/frontend' templates_path = os.path.abspath(source_path + '/../../../www/templates') dst_plugin_path = templates_path + '/zigbee2mqtt' Domoticz.Debug('Copying files from ' + source_path + ' to ' + templates_path) if not (os.path.isdir(dst_plugin_path)): os.makedirs(dst_plugin_path) copy2(source_path + '/zigbee2mqtt.html', templates_path) copy2(source_path + '/zigbee2mqtt.js', templates_path) copy2(source_path + '/zigbee_devices.js', dst_plugin_path) copy2(source_path + '/zigbee_groups.js', dst_plugin_path) copy2(source_path + '/libs/leaflet.js', dst_plugin_path) copy2(source_path + '/libs/leaflet.css', dst_plugin_path) copy2(source_path + '/libs/viz.js', dst_plugin_path) copy2(source_path + '/libs/viz.full.render.js', dst_plugin_path) Domoticz.Log('Installing plugin custom page completed.') except Exception as e: Domoticz.Error('Error during installing plugin custom page') Domoticz.Error(repr(e)) def uninstall(self): Domoticz.Log('Uninstalling plugin custom page...') try: templates_path = os.path.abspath( os.path.dirname(os.path.abspath(__file__)) + '/../../www/templates') dst_plugin_path = templates_path + '/zigbee2mqtt' Domoticz.Debug('Removing files from ' + templates_path) if (os.path.isdir(dst_plugin_path)): rmtree(dst_plugin_path) if os.path.exists(templates_path + "/zigbee2mqtt.html"): os.remove(templates_path + "/zigbee2mqtt.html") if os.path.exists(templates_path + "/zigbee2mqtt.js"): os.remove(templates_path + "/zigbee2mqtt.js") Domoticz.Log('Uninstalling plugin custom page completed.') except Exception as e: Domoticz.Error('Error during uninstalling plugin custom page') Domoticz.Error(repr(e))
class TwoBot: """ The almighty two bot class, instantiate it, then run() it """ def __init__(self): try: config = yaml.load(open('config.yaml')) except FileNotFoundError as e: print( "No config.yaml found - have you copied config_example.yaml to config.yaml?" ) exit(2) try: self.SLACK_TOKEN = config['slack_token'] self.KEYWORD = str( config['keyword']) # 2 in YAML will be a number, not str self.COMMAND = config['command'] self.FILENAME = config.get("data_file", "twodata.json") self.API_ENABLE = config.get("api_enable", False) self.API_ADDRESS = config.get("api_address", "0.0.0.0") self.API_PORT = config.get("api_port", 2222) except KeyError as e: print("Config.yaml missing some values! {}".format(e)) exit(3) self.slack = SlackClient(self.SLACK_TOKEN) self.cache = { } # Cache for Slack API calls; (user ID) -> (Slack API response)["user"] self.twoinfo = None if not os.path.isfile(self.FILENAME): with open(self.FILENAME, "w+") as datafile: datafile.write("{}") datafile.close() with open(self.FILENAME, "r") as datafile: self.twoinfo = json.loads(datafile.read()) for key in ["lasttime", "limitmsgtime", "twos"]: if key not in self.twoinfo: self.twoinfo[key] = {} if self.API_ENABLE: self.starttime = datetime.utcnow() print("{} {} {} starttime".format(self, type(self), self.starttime)) self.api = API(self, host=self.API_ADDRESS, port=self.API_PORT) self.api.start() def save_data(self): """ Save the data in the twoinfo structure """ with open(self.FILENAME, "w") as datafile: datafile.write(json.dumps(self.twoinfo)) def is_a_user(self, user): """ Checks if a user is know to exist or to have existed """ if user is None: return False if user.startswith("I-"): # IRC users exist only if they have been twod before # because there is no good way to properly check if they # exist return user in self.twoinfo["twos"] # For slack users, we can check if they exist. return self.slack.api_call("users.info", user=user).get("user") is not None def user_info(self, user): """ Gets the user info dict from slack for a user ID """ if user is None: return None if user.startswith("I-"): # IRC user nick = user[2:] return {"name": nick, "irc_user": True} if user in self.cache and (time.time() - self.cache[user]["fetched"]) < 900: # could expose cache time (900s) as a config value return self.cache[user] else: result = self.slack.api_call("users.info", user=user).get("user") result["fetched"] = time.time() self.cache[user] = result return result def channel_info(self, channel): """ Gets the channel info dict from slack for a user ID """ return self.slack.api_call("channels.info", channel=channel).get("channel") def send_message(self, channel, text): """ Send `text` as a message to `channel` in slack """ return self.slack.api_call("chat.postMessage", channel=channel, text=text) @staticmethod def get_dict_string(input_dict, path): """ Lookup a valud in the dict from a path joined with `.` """ parts = path.split(".") mydict = input_dict for part in parts: if part in mydict: if isinstance(mydict[part], dict): mydict = mydict[part] else: value = mydict[part] if isinstance(value, str): if value.strip() != "": return value.strip() return None else: return None @staticmethod def user_name(user): """ Get the name of the user at it should appear in slack """ if user is None: return "<Unknown User>" namepaths = [ "profile.display_name_normalized", "profile.display_name", "profile.real_name_normalized", "profile.real_name", "real_name", "name" ] for path in namepaths: name = TwoBot.get_dict_string(user, path) if name is not None: return name return "<Unknown User>" @staticmethod def lower_id(userid): """ Make a user ID lower case, if it is an IRC nick style ID """ if userid.startswith("I-"): return "I-" + userid[2:-6].lower() + " (IRC)" return userid def handle_command(self, msgtext, channelid): """ respond to a command message """ parts = [part for part in msgtext.split(" ") if part != ""] if len(parts) == 1: twos = self.twoinfo["twos"] times = self.twoinfo["lasttime"] usertimes = [(user, number, times[user]) for (user, number) in twos.items()] leaders = list(reversed(sorted(usertimes, key=itemgetter(1, 2)))) numleaders = 5 if len(leaders) > numleaders: leaders = leaders[0:numleaders] text = ", ".join([ "%s: %d" % (TwoBot.user_name(self.user_info(user)), num) for user, num, _ in leaders ]) self.send_message(channelid, "Leaderboard of shame: %s" % (text)) if len(parts) == 2: match = re.search(r"(?:^<(@[^>]*)>$|^([^@<>\n ]+)$)", parts[1]) if not match: self.send_message( channelid, "Malformed %s command, didn't recognise parameter" % (self.COMMAND)) else: userid = match.groups()[0] if userid is None: userid = match.groups()[1] if userid is None: # uh-oh return # ??? if userid.startswith("@U"): userid = userid[1:] else: userid = "I-%s (IRC)" % (userid) if not self.is_a_user(userid): self.send_message(channelid, "No such user") else: self.send_message( channelid, "%s has a total of %d" % (TwoBot.user_name(self.user_info(userid)), self.twoinfo["twos"].get(TwoBot.lower_id(userid), 0))) if len(parts) > 2: self.send_message( channelid, "Malformed %s command, specify zero or one parameters " "where the optional parameter is a \"@mention\" for slack users " "or \"nick\" for IRC users" % (self.COMMAND)) def handle_keyword(self, channelid, user, userid): """ respond to the keyword """ userid = TwoBot.lower_id(userid) if userid not in self.twoinfo["twos"]: self.twoinfo["twos"][userid] = 0 if userid not in self.twoinfo["lasttime"]: self.twoinfo["lasttime"][userid] = 0 then = self.twoinfo["lasttime"][userid] endtime = then + (60 * 10) now = time.time() if endtime > now: # Rate limit if userid not in self.twoinfo["limitmsgtime"]: self.twoinfo["limitmsgtime"][userid] = 0 limittime = self.twoinfo["limitmsgtime"][userid] last = self.twoinfo["lasttime"][userid] if limittime < last: self.twoinfo["limitmsgtime"][userid] = time.time() endtime_rounded = ( (endtime // 60) + 1) * 60 # Round up to next minute timeoutstr = datetime.fromtimestamp(endtime_rounded).strftime( "%H:%M") self.send_message( channelid, "Rate limit: %s cannot be %s'd again until %s" % (TwoBot.user_name(user), self.KEYWORD, timeoutstr)) self.save_data() else: self.twoinfo["twos"][userid] += 1 self.twoinfo["lasttime"][userid] = now self.save_data() self.send_message( channelid, "Whoops! %s got %s'd! (total: %d)" % (TwoBot.user_name(user), self.KEYWORD, self.twoinfo["twos"][userid])) def run_once(self): """ Wait until a messages is available, then deal with it and return """ data_list = self.slack.rtm_read(blocking=True) for data in data_list: # There's lots of reasons to ignore a message... data_type = data.get("type") if data_type != "message": # Must be of type message continue channelid = data.get("channel") channel = self.channel_info(channelid) if channel is None: # Must have a valid channel continue userid = data.get("user") user = self.user_info(userid) if user is None: if data.get('subtype') == 'bot_message' and data.get( 'bot_id') == 'B4ZFXE0A0': # Hardcoded exception for using IRC bridge with this bot id userid = "I-" + data.get("username") user = self.user_info(userid) else: # Must be from a valid user continue msgtext = data.get("text") print("Message in %s, from %s: %s" % (channel.get("name"), TwoBot.user_name(user), data.get("text"))) if not msgtext: # Must contain some text continue msgtext = msgtext.strip() # At this point we have a valid message if msgtext.startswith(self.COMMAND): self.handle_command(msgtext, channelid) if any([ msgtext == self.KEYWORD, msgtext == "_%s_" % (self.KEYWORD), msgtext == "*%s*" % (self.KEYWORD) ]): self.handle_keyword(channelid, user, userid) def run(self): """ Run the slack bot! Unitil interrupted """ if not self.slack.rtm_connect(): print("Unable to connect") else: print("Connected to slack") while True: self.run_once()
def __init__(self, config_file): self.config_file = config_file self.conf = configuration.Configuration(self.config_file) self.mod_rc = zmqrc.ModRemoteControl("localhost") self.dpd = dpd.DPD() self.api = API(self.mod_rc, self.dpd)
def run_api(topology, scheduler): # starts the API # The API takes the topology and scheduler as an input, in order to be able to modify them. api = API(topology, scheduler) api.run(debug=True)
from PySide2.QtWidgets import QApplication, QDialog, QMessageBox from PySide2.QtGui import QIcon from timerwindow import TimerWindow from loginwindow import LoginWindow from systemtray import SystemTrayIcon from error import ApiException from api import API from config import Config from database import Database from _version import __version__ import sys import os import qtawesome as qta os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1" api = API() db = Database() if __name__ == '__main__': app = QApplication(sys.argv) data = api.whoami(db.readDatabase()['userdata']['api_token']) print(data) try: if data == None: raise ApiException('Error could not connect to api') if data.get('error'): login = LoginWindow() trayIcon = SystemTrayIcon(QIcon(Config.icon)) #Maybe move this inside the SystemTray class, keep the main clean yo
def reprocess(args): """ Reprocesses all specified recording from specified server """ api = API(args.server, args.user, args.password) print(f"Querying server {args.server}") print(f"Limit is {args.limit}") if len(args.recording_id) == 1: recordings = [ int(rec_id) for rec_id in args.recording_id[0].split(",") if rec_id ] print(f"Reprocessing {recordings}") api.reprocess(recordings) return hour_ago = datetime.now() - timedelta(hours=1) where = { "type": "thermalRaw", "processingState": { "$ne": "FINISHED" }, "processingStartTime": { "$or": { "$gt": hour_ago.isoformat(), "$eq": None } }, } if args.algorithm_id: where["$or"] = [ { "additionalMetadata.algorithm": { "$eq": None } }, { "additionalMetadata.algorithm": { "$lt": args.algorithm_id } }, ] else: where["additionalMetadata.algorithm"] = {"$eq": None} if len(args.recording_id) == 2: if args.recording_id[0]: where.setdefault("id", {})["$gte"] = int(args.recording_id[0]) if args.recording_id[1]: where.setdefault("id", {})["$lte"] = int(args.recording_id[1]) q = api.query(where=where, limit=args.limit, raw_json=True) count = q["count"] if count: print(f"Still reprocessing {count} records") print([row["id"] for row in q["rows"]]) return where["processingState"] = "FINISHED" rows = api.query(where=where, limit=args.limit) recording_ids = [row["id"] for row in rows] print(f"Reprocessing recording ids: {recording_ids}") api.reprocess(recording_ids)
import config as cfg from music import Music from responses import Responses, QuestionHandler from automod import Automod from database import Database from util import Util from api import API music = Music() questionHandler = QuestionHandler() automod = Automod() database = Database() api = API() class Bot(discord.Client): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.bg_task = self.loop.create_task(self.backgroundLoop()) def isAdmin(self, memberObject): for roleObject in memberObject.roles: if roleObject.id in cfg.ADMIN_ROLE_IDS: return True return False def hasRole(self, memberObject, roleToCheck):
def ask(): api = API.using_request(request) rss = RSS(api.ask_hn(), 'Hacker News: Ask HN', 'https://news.ycombinator.com/ask') return rss.response()
# Query the current json return jsonobj.Path(query).query(self._json) def update(self, argument, value): if argument in self._sessionData: if self._sessionData[argument] != value: raise ValueError( ('That argument is already set in the ' + 'profile with a difference value. Arg: {0}, ' + 'Old Value: {1}, New Value: {2}').format( argument, self._sessionData[argument], value)) self._sessionData[argument] = value if __name__ == "__main__": controller = Controller('Files/header.json') controller.registerRateLimit(15, 1) controller.registerRateLimit(95, 120) api = API('Files/endpoints.txt', 'Files/arguments.txt', controller) nav = APINav(api) nav.navigate('summoners', {'summonerName': 'Darqk'}) print(nav.execute('name')) nav.navigate('matchlists') print(nav.execute('matches[0]'))
else: await ctx.send(f"Room {room_id} does not exist") @bot.command() async def reroll(ctx): if ctx.message.author.id in [ player.user.id for r in rooms for team in r.players for player in team ]: for r in rooms: for team in r.players: for player in team: if player.user.id == ctx.message.author.id and player.reroll_count > 0: champ = getNewNumber(r.champions, 0, len(api.champions)) r.champions.remove(player.champion) player.champion = champ r.champions.append(champ) player.reroll_count -= 1 await ShowRoom(ctx, r.id) if __name__ == "__main__": config = configparser.ConfigParser() config.read('config.ini') api = API(config['League of Legends API']['token']) bot.run(config['Discord']['token'])
def __init__(self, api=None): self.api = api or API()
class ProcessWorkbook: """ Provides a mechanism for extracting the content from the workbook and uploading it to Odoo """ def __init__(self): self.api = API() self.workbook = load_workbook(filename=SPREADSHEET, data_only=True)[SHEET] self.records = list() self.last_parent = None # These lists are expected to contain one or more Tuples # Each Tuple will be of the format (make, model) self.models_to_search = list() self.models_to_create = list() # The Tuples here will be of the format ((make, model), id) self.models_to_ids = list() print('Initialized ProcessWorkbook') def get_id_from_model(self, model): """ Searches `self.models_to_ids` for a matching `model`, and returns the id if there is one. Returns None otherwise """ for item in self.models_to_ids: if model == item[0][1]: return item[1] return None def serial_in_records(self, serial, records=None): """ Determines if a particular serial number is in a record list, and returns True if so; otherwise, returns False `serial` is a string to check against the record list `records` is an optional argument that will search that specific record list (such as a parent's children) instead of just the parent records """ if records is None: records = self.records if serial in [record.serial for record in records if record.serial]: return True return False def get_records(self): """ Reads the workbook and sorts each row into multiple instances of the Record class. When this method comes across a "Parent" record, it stores that Record instance, which will be used whenever this method comes across a "Child" record. When this method comes across a "Child" record, it gets appended to the `children` list of the stored parent object. Returns `self` (this instance of ProcessWorkbook) """ print('Getting rows from the spreadsheet and sorting relationships') for row in self.workbook.iter_rows(min_row=FIRST_ROW, max_col=LAST_COL, max_row=LAST_ROW): serial = str(row[0].value) relationship = row[2].value if relationship == 'Parent': if not self.serial_in_records(serial): record = Record(serial=serial, asset_tag=str(row[1].value), make=str(row[3].value), model=str(row[4].value), device_type=str(row[5].value)) self.records.append(record) self.last_parent = record # Add unique models so we can search for their sellable ids if record.model not in itertools.chain( *self.models_to_search): self.models_to_search.append( (record.make, record.model)) elif relationship == 'Child': last_parent_children = self.last_parent.children if not self.serial_in_records(serial, last_parent_children): record = Record(serial=serial, asset_tag=str(row[1].value), make=str(row[3].value), model=str(row[4].value), device_type=str(row[5].value), children=None) last_parent_children.append(record) else: pass return self def show_records(self): """ Pretty Prints a JSON string for all of the records that are stored """ pprint([record for record in self.records]) def get_odoo_model_ids(self): """ Iterates over unique models and searches Odoo for the database id of those models. Once the search is complete, `self.models_to_ids` contains a mapping between each unique model name and the database id. When a model can't be found, that model is printed so that a manual search can be done, or a new item can be created. When a model returns multiple ids, that model is printed so that a manual search can be done to select the "correct" database id. Returns `self` (this instance of ProcessWorkbook) """ print('Searching Odoo for sellable items with matching models') for model in self.models_to_search: odoo_records = self.api.do_search_and_read( 'erpwarehouse.sellable', [('model', 'ilike', model[1])]) if not odoo_records: print('Unable to find model: %s' % model[1]) self.models_to_create.append(model) else: # If the search returns more than one, we'll # assume that it was the first one since some # models are duplicated (for whatever reason) self.models_to_ids.append((model, odoo_records[0]['id'])) def create_missing_model_ids(self): """ For any models that couldn't be located when initally searched, create them and then store those newly created ids Returns `self` (this instance of ProcessWorkbook) """ print('Creating sellable items for missing models') for model in self.models_to_create: print('Creating model: %s' % model[1]) result = self.api.do_create('erpwarehouse.sellable', { 'make': model[0], 'model': model[1] }) self.models_to_ids.append((model, result)) return self def create_line_items(self): """ For all the records that we can process (there's a mapped model), create the asset catalog and data destruction line items. Returns `self` (this instance of ProcessWorkbook) """ print('Creating Line items for accepted records in Odoo') for record in self.records: # Is the model one that we have the id for if self.get_id_from_model(record.model): result = self.api.do_create( 'erpwarehouse.asset', { 'catalog': ASSET_CATALOG_ID, 'make': self.get_id_from_model(record.model), 'serial': record.serial, 'tag': record.asset_tag, }) print('Added id: %s' % (result)) if not record.children: device_type = '0' if record.device_type == 'Network': device_type = 'N' elif record.device_type == 'Tape': device_type = 'T' result = self.api.do_create( 'erpwarehouse.ddl_item', { 'ddl': DATA_DESTRUCTION_ID, 'make': self.get_id_from_model(record.model), 'serial': record.serial, 'storser': 'N/a', 'type': device_type, }) print('Added id: %s' % (result)) else: for child in record.children: device_type = '0' if child.device_type == 'Hard Drive': device_type = 'H' result = self.api.do_create( 'erpwarehouse.ddl_item', { 'ddl': DATA_DESTRUCTION_ID, 'make': self.get_id_from_model(record.model), 'serial': record.serial, 'storser': child.serial, 'type': device_type, }) print('Added id: %s' % (result)) else: print('Unable to add %s as there is no sellable id' % (record.serial)) return self def run(self): """ Runs everything in the order that is required """ self.get_records() self.get_odoo_model_ids() self.create_missing_model_ids() self.create_line_items()