def fav2(return_type="list", switch=None): wait_sec = 30 start = time.time() e = lambda a, b : round(a-b) print(return_type + " is selected!") Mastodon = login(switch) my_cred = Mastodon.account_verify_credentials() id = my_cred['id'] limit = 40 #40 print("Your id is {0}".format(id)) page = 0 favourites = Mastodon.favourites(None,None,limit) max_id = favourites[-1]['_pagination_next']['max_id'] print("count: {0} ({1})".format(str(len(favourites)), str(len(favourites)))) while True: latest_favourites = Mastodon.favourites(max_id,None,limit) if isinstance(latest_favourites,dict): pprint("Error code 429:{0} wait {1} sec...".format(latest_favourites['error'],wait_sec)) time.sleep(wait_sec) latest_favourites = previous_favourites continue elif len(latest_favourites) < limit: favourites.extend(latest_favourites) page += 1 elapsed_time = time.time() - start print("End fetch your favourites") print("count: {0} time:{1}sec".format(str(len(favourites)), elapsed_time)) break else: max_id = favourites[-1]['_pagination_next']['max_id'] favourites.extend(latest_favourites) page += 1 previous_favourites = latest_favourites print("count: {0} ({1}) time:{2}sec".format(str(len(favourites)), str(len(latest_favourites)), e(time.time(),start))) time.sleep(3) if return_type == "json": filename = str(id) + "_fav" jsoner(favourtes,filename) else: return favourites
url = sys.argv[1] cid_file = 'client_id_pawoo.txt' token_file = 'access_token_pawoo.txt' mastodon = Mastodon(client_id=cid_file, access_token=token_file, api_base_url=url) # 自分のアカウントのお気に入り数を数える next_id = '' fav_count = 0 # 自分のアカウントのお気に入り数を数える while True: # ローカルタイムラインの取得 fetched_favs = mastodon.favourites(limit=40, max_id=next_id) # 新しいトゥートが取得できなかったらループ終了 if len(fetched_favs) > 0: fav_count += len(fetched_favs) else: break # 80件以上のページネーションするための値取得 favs_last = fetched_favs[len(fetched_favs) - 1] if '_pagination_next' in favs_last.keys( ) and 'max_id' in favs_last['_pagination_next'].keys(): next_id = favs_last['_pagination_next']['max_id'] else: break
def backup(args): """ Backup toots, followers, etc from your Mastodon account """ (username, domain) = args.user.split("@") url = 'https://' + domain client_secret = domain + '.client.secret' user_secret = domain + '.user.' + username + '.secret' status_file = domain + '.user.' + username + '.json' data = None if os.path.isfile(status_file): print("Loading existing backup") with open(status_file, mode = 'r', encoding = 'utf-8') as fp: data = json.load(fp) if not os.path.isfile(client_secret): print("Registering app") Mastodon.create_app( 'mastodon-backup', api_base_url = url, to_file = client_secret) if not os.path.isfile(user_secret): print("Log in") mastodon = Mastodon( client_id = client_secret, api_base_url = url) url = mastodon.auth_request_url( client_id = client_secret, scopes=['read']) print("Visit the following URL and authorize the app:") print(url) print("Then paste the access token here:") token = sys.stdin.readline().rstrip() mastodon.log_in( username = username, code = token, to_file = user_secret, scopes=['read']) else: mastodon = Mastodon( client_id = client_secret, access_token = user_secret, api_base_url = url) print("Get user info") user = mastodon.account_verify_credentials() def find_id(list, id): """Return the list item whose id attribute matches.""" return next((item for item in list if item["id"] == id), None) def fetch_up_to(page, id): statuses = [] # use a generator expression to find our last status found = find_id(page, id) # get the remaining pages while len(page) > 0 and found is None: statuses.extend(page) sys.stdout.flush() page = mastodon.fetch_next(page) if page is None: break found = find_id(page, id) page = page[0:page.index(found)] statuses.extend(page) print("Fetched a total of %d new toots" % len(statuses)) return statuses if data is None or not "statuses" in data: print("Get statuses (this may take a while)") statuses = mastodon.account_statuses(user["id"]) statuses = mastodon.fetch_remaining( first_page = statuses) else: id = data["statuses"][0]["id"] print("Get new statuses") statuses = fetch_up_to(mastodon.account_statuses(user["id"]), id) statuses.extend(data["statuses"]) if data is None or not "favourites" in data: print("Get favourites (this may take a while)") favourites = mastodon.favourites() favourites = mastodon.fetch_remaining( first_page = favourites) else: id = data["favourites"][0]["id"] print("Get new favourites") favourites = fetch_up_to(mastodon.favourites(), id) favourites.extend(data["favourites"]) data = { 'account': user, 'statuses': statuses, 'favourites': favourites } print("Saving %d statuses and %d favourites" % ( len(statuses), len(favourites))) date_handler = lambda obj: ( obj.isoformat() if isinstance(obj, (datetime.datetime, datetime.date)) else None) with open(status_file, mode = 'w', encoding = 'utf-8') as fp: data = json.dump(data, fp, indent = 2, default = date_handler)