def token_check(token): if config.authenticate(token): session['auth_token'] = config.signature flash('login successful', 'success') return redirect(url_for('index')) fail()
def index(): token = session.get('auth_token') if config.authenticate(token): return render_template('main.html') else: abort( 401, 'The request requires authentication. Please contact sysadmin for details.' )
def stop_video(): """stop the video stream""" if not config.authenticate(session.get('auth_token')): fail() print("STOP VIDEO") # kill the videoplayer scripts os.system('kill -9 `pgrep videoplayer.sh`') os.system('kill -9 `pgrep omxplayer`') return redirect(url_for('index'))
def file_view(): if not config.authenticate(session.get('auth_token')): return fail() """render the file upload form""" # session['no_csrf'] = True file_list = [] temp_list = sorted(os.listdir(app.config['UPLOAD_FOLDER'])) for idx, f in enumerate(temp_list): file_list.append((idx, f)) return render_template('files.html', file_list=file_list)
def restart_video(): """restart the video stream""" if not config.authenticate(session.get('auth_token')): fail() print("RESTART VIDEO") # kill the videoplayer scripts os.system('kill -9 `pgrep videoplayer.sh`') os.system('kill -9 `pgrep omxplayer`') time.sleep(2) # start the videoplayer os.system('~/videoplayer.sh &') return redirect(url_for('index'))
def _check_auth(): """ Check the user's credentials against the web API. """ user = {} try: user = config.authenticate(config.get_auth()) assert (user['is_authenticated']) # An EPD Free user who is trying to install a package not in # EPD free. Print out subscription level and fail. print config.subscription_message(user) _done(FAILURE) except Exception as e: print e.message # No credentials. print "" _prompt_for_auth()
def search(enpkg, pat=None): """ Print the packages that are available in the (remote) KVS. """ # Flag indicating if the user received any 'not subscribed to' # messages SUBSCRIBED = True print FMT4 % ('Name', ' Versions', 'Product', 'Note') print 80 * '=' names = {} for key, info in enpkg.query_remote(): names[info['name']] = name_egg(key) installed = {} for key, info in enpkg.query_installed(): installed[info['name']] = VB_FMT % info for name in sorted(names, key=string.lower): if pat and not pat.search(name): continue disp_name = names[name] installed_version = installed.get(name) for info in enpkg.info_list_name(name): version = VB_FMT % info disp_ver = (('* ' if installed_version == version else ' ') + version) available = info.get('available', True) product = info.get('product', '') if not (available): SUBSCRIBED = False print FMT4 % (disp_name, disp_ver, product, '' if available else 'not subscribed to') disp_name = '' # if the user's search returns any packages that are not available # to them, attempt to authenticate and print out their subscriber # level if config.get('use_webservice') and not (SUBSCRIBED): user = {} try: user = config.authenticate(config.get_auth()) except Exception as e: print e.message print config.subscription_message(user)
def _check_auth(): """ Check the user's credentials against the web API. """ user = {} try: user = config.authenticate(config.get_auth()) assert(user['is_authenticated']) # An EPD Free user who is trying to install a package not in # EPD free. Print out subscription level and fail. print config.subscription_message(user) _done(FAILURE) except Exception as e: print e.message # No credentials. print "" _prompt_for_auth()
def search(enpkg, pat=None): """ Print the packages that are available in the (remote) KVS. """ # Flag indicating if the user received any 'not subscribed to' # messages SUBSCRIBED = True print FMT4 % ('Name', ' Versions', 'Product', 'Note') print 80 * '=' names = {} for key, info in enpkg.query_remote(): names[info['name']] = name_egg(key) installed = {} for key, info in enpkg.query_installed(): installed[info['name']] = VB_FMT % info for name in sorted(names, key=string.lower): if pat and not pat.search(name): continue disp_name = names[name] installed_version = installed.get(name) for info in enpkg.info_list_name(name): version = VB_FMT % info disp_ver = (('* ' if installed_version == version else ' ') + version) available = info.get('available', True) product = info.get('product', '') if not(available): SUBSCRIBED = False print FMT4 % (disp_name, disp_ver, product, '' if available else 'not subscribed to') disp_name = '' # if the user's search returns any packages that are not available # to them, attempt to authenticate and print out their subscriber # level if config.get('use_webservice') and not(SUBSCRIBED): user = {} try: user = config.authenticate(config.get_auth()) except Exception as e: print e.message print config.subscription_message(user)
def upload_video(): """render upload page and upload file""" if not config.authenticate(session.get('auth_token')): fail() if request.method == "POST": file = request.files["file"] print("File uploaded") print(file) save_file(file) res = make_response(jsonify({"message": "File uploaded"}), 200) return res return render_template("upload_video.html")
def save_file(file): """save the file to the proper location, rename if necessary""" # check if user is logged in and session is authentic if not config.authenticate(session.get('auth_token')): fail() # sanitize the filename filename = secure_filename(file.filename) pathname = os.path.join(app.config['UPLOAD_FOLDER'], filename) # handle name collision if needed # filename will add integers at beginning of filename in dotted fashion # hello.jpg => 1.hello.jpg => 2.hello.jpg => ... # until it finds an unused name i = 1 while os.path.isfile(pathname): parts = filename.split('.') parts.insert(0, 'copy') filename = '.'.join(parts) i += 1 if i > 100: # probably under attack, so just fail raise ValueError( "too many filename collisions, administrator should check this out" ) pathname = os.path.join(app.config['UPLOAD_FOLDER'], filename) try: # ensure directory where we are storing exists, and create it directory = app.config['UPLOAD_FOLDER'] if not os.path.exists(directory): os.makedirs(directory) # finally, save the file AND create its resource object in database file.save(pathname) return True except: flash("The upload failed", "danger") return False
def file_delete(): """handle file deletion from form""" if not config.authenticate(session.get('auth_token')): fail() command = request.form.get('command') if command == 'reorder': items = request.form for item in items: if item[0] == '@': # here is an item to reorder try: idx = int(items.get(item)) orig_fname = item[1:] orig_pathname = os.path.join(app.config['UPLOAD_FOLDER'], orig_fname) # is old filename already numbered? at_loc = orig_fname.find('^') if at_loc > 0: _ord = orig_fname[:at_loc] _fname = orig_fname[at_loc + 1:] else: _fname = orig_fname newfilename = "{}^{}".format(idx, _fname) new_pathname = os.path.join(app.config['UPLOAD_FOLDER'], newfilename) os.rename(orig_pathname, new_pathname) except: flash('the order must be a number', "warning") print(item) else: items = request.form for item in items: pathname = os.path.join(app.config['UPLOAD_FOLDER'], item) if os.path.isfile(pathname): os.remove(pathname) flash('deleted {}'.format(item), "success") return redirect(url_for('file_view'))
def __init__(self): self.api = authenticate()
class TweetService: api = authenticate() def __init__(self): self.ids_cache = [] self.tweets_cache = {} self.users_cache = {} self.dont_engage_list = self.api.get_list( owner_screen_name='sole_hunt', slug='sneaker-friends') self._get_ids_for_users_followed() def _get_ids_for_users_followed(self, ids=None): if not ids or self.ids_cache: logging.info("Collecting ids for users I follow") ids = self.api.friends_ids() self.ids_cache.extend(ids) logging.info("Returning {} ids".format(len(ids))) return ids def _store_user(self, user_id=None): user = self.api.get_user(user_id) if user.id in self.users_cache.keys(): return self.users_cache[user_id] = user logging.info("Stored user {}".format(user.screen_name)) def store_users(self, ids): for user_id in ids: self._store_user(user_id) def get_followed_by_followers_count(self, count=50000): # final store for desired users to return users_with_desired_followers_count = [] # ensure ids_cache exists if not self.ids_cache: self._get_ids_for_users_followed() if not self.users_cache: self.store_users(self.ids_cache) for i in self.ids_cache: user = self.users_cache[i] logging.info("Getting tweets from {}".format(user.screen_name)) if user.followers_count >= count: logging.info("Adding {} to return list".format( user.screen_name)) users_with_desired_followers_count.append(user) return users_with_desired_followers_count def get_relevant_tweets_from_followed(self): """ Gets tweet that mentioned a tag from followed list """ users = self.get_followed_by_followers_count() rate_limit = self.get_rate_limit('users', '/users/show/:id') logging.info("followed_ids count: {}".format(len(users))) logging.info("monitoring rate limit: {}".format(rate_limit)) while rate_limit_ok(rate_limit): logging.info("rate limit ok: {}".format(rate_limit_ok(rate_limit))) for _user in users: if not self.should_engage(_user): logging.info("Not going to engage with {}".format(_user)) return user = self.users_cache[_user.id] logging.info("Collecting user {}".format(user.screen_name)) if not user.status.favorited: self.tweets_cache[user.status.id] = { "tweet": user.status.text, 'status': user.status } logging.info("Updated cache with tweet from {}".format( user.screen_name)) time.sleep(1) rate_limit = self.get_rate_limit('users', '/users/show/:id') if rate_limit_too_low(rate_limit): logging.warn("Rate limit met") break break return self.tweets_cache def get_rate_limit(self, *args): """ example call: self.get_rate_limits('users', '/users/show/:id') """ query = "self.api.rate_limit_status()['resources']" for arg in args: query += "['{}']".format(arg) rate_limit_data = eval(query) return rate_limit_data def _engage_tweet(self, tweet): if tweet['status'].favorited: logging.info("Already liked") return logging.info("engaging tweet_id {}".format(tweet['status'].id)) if has_enough_retweets(tweet['status'].retweet_count): if not tweet['status'].retweeted: try: tweet['status'].retweet() logging.info("Retweeted") except: pass if not tweet['status'].favorited: try: tweet['status'].favorite() logging.info("Liked!") except: pass def engage_tweets(self, tweets=None): """ query for all tweets in cache engage with a subset of tweets """ limit = TEN if not self.tweets_cache or tweets: # create cache if it does not exist self.get_relevant_tweets_from_followed() for tweet_id, tweet in self.tweets_cache.iteritems(): if tweet['status'].entities['urls']: url = tweet['status'].entities['urls'][0]['url'] logging.info("Engaging tweet {}".format(url)) self._engage_tweet(tweet) limit -= ONE time.sleep(ONE) if limit <= ZERO: limit = TEN time.sleep(MIN_INTERVAL * 2) continue def _reset(self): self.ids_cache = [] self.tweets_cache = {} self.users_cache = {} self._get_ids_for_users_followed() def should_engage(self, user): member_screen_names = [ member.screen_name for member in self.dont_engage_list.members() ] if user.screen_name in member_screen_names: return False return True def follow_retweeter(self, user_id): """ follow users that retweet :param tweet: :return: """ user = self.users_cache[user_id] tweet = user.status is_retweeted = tweet.retweeted if is_retweeted: retweets = tweet.retweets() if len(retweets) > 16: retweets = retweets[:15] for retweet in retweets: # follow user and add to list user = retweet.user if not user.following and user.id != self.api.me().id: user.follow() logging.info("Followed {}".format(user.screen_name)) self.api.add_list_member(slug=NON_ENGAGED_FRIENDS_LIST, user_id=user.id, owner_screen_name='sole_hunt') logging.info("Added {} to non engaged list".format( user.screen_name)) def follow_retweeters(self): # first ensure users have been stored self.store_users(self.ids_cache) followed = [] for user_id, user in self.users_cache.iteritems(): try: self.follow_retweeter(user.id) followed.append(user_id) except: continue logging.info("Followed users: {}".format(followed)) return followed