def delDeck(): result = {} json_data = request.json try: deck = Deck.query.filter_by(id=json_data['id']).first() links = Link.query.filter_by(deckid=deck.id).all() user = User.query.filter_by(username=deck.name).first() for link in links: profiles = Profile.query.filter_by(linkid=link.id).all() for profile in profiles: if (profile.run_status == 1): jobid = 'retweet_%s' % (profile.id) if (is_running(jobid)): scheduler.remove_job(jobid) db.session.delete(profile) approves = Approve.query.filter_by(linkid=link.id).all() for approve in approves: db.session.delete(approve) db.session.delete(link) removedriver(deck.name) db.session.delete(deck) db.session.delete(user) db.session.commit() result['status'] = 1 result['msg'] = 'Succefully deleted Deck.' except: result['status'] = -1 result['msg'] = 'Occured error in delete Deck.' db.session.close() return jsonify({'result': result})
def unlinkdeck(): result = {} json_data = request.json if (is_admin()): link = Link.query.filter_by(userid=json_data['userid'], deckid=json_data['deckid']).first() if (link == None): result['msg'] = 'This user is not linked with this deck.' result['status'] = 0 else: try: profiles = Profile.query.filter_by(linkid=link.id).all() for profile in profiles: if (profile.run_status == 1): jobid = 'retweet_%s' % (profile.id) if (is_running(jobid)): scheduler.remove_job(jobid) db.session.delete(profile) approves = Approve.query.filter_by(linkid=link.id).all() for approve in approves: db.session.delete(approve) db.session.delete(link) db.session.commit() result['status'] = 1 result['msg'] = 'Sucessfully unlinked.' except: result['msg'] = 'Occured error in db session.' result['status'] = 0 else: result['status'] = -1 result['msg'] = 'Please login now.' return jsonify({'result': result})
def delUser(): result = {} json_data = request.json if (is_admin()): try: user = User.query.filter_by(id=json_data['userid']).first() links = Link.query.filter_by(userid=user.id).all() for link in links: profiles = Profile.query.filter_by(linkid=link.id).all() for profile in profiles: if (profile.run_status == 1): jobid = 'retweet_%s' % (profile.id) if (is_running(jobid)): scheduler.remove_job(jobid) db.session.delete(profile) approves = Approve.query.filter_by(linkid=link.id).all() for approve in approves: db.session.delete(approve) db.session.delete(link) db.session.delete(user) db.session.commit() result['status'] = 1 result['msg'] = 'Succefully deleted user.' except: result['status'] = -1 result['msg'] = 'Occured error in delete user.' else: result['status'] = -1 result['msg'] = 'Please login now.' db.session.close() return jsonify({'result': result})
def delProfile(): result = {} json_data = request.json try: profile = Profile.query.filter_by(id=json_data['id']).first() if (profile.run_status == 1): jobid = 'retweet_%s' % (profile.id) if (is_running(jobid)): scheduler.remove_job(jobid) db.session.delete(profile) db.session.commit() result['status'] = 1 result['msg'] = 'Succefully deleted Profile.' except: result['status'] = -1 result['msg'] = 'Occured error in delete profile.' db.session.close() return jsonify({'result': result})
def updateProfile(): result = {} json_data = request.json minutes = json_data['minutes'] profile = Profile.query.filter_by(id=json_data['id']).first() link = Link.query.filter_by(id=profile.linkid).join(Deck).first() minutes = int(minutes) if (minutes < 10): result['status'] = 0 result['msg'] = 'Interval time must to be more than %s minutes' % app.config['MINIMUM_INTERVAL_TIME'] elif (profile == None): result['status'] = -1 result['msg'] = 'Profile is not existed.' elif (link.deck == None): result['status'] = -1 result['msg'] = 'Deck is not available.' else: Profile.query.filter_by(id=json_data['id']).update({"minutes": json_data['minutes']}) result['status'] = 1 result['msg'] = 'Succefully Updated Profile.' if (profile.run_status == 1): driver = getdriver(link.deck.name) if (driver != None): jobid = 'retweet_%s' % (profile.id) if (is_running(jobid)): scheduler.remove_job(jobid) params = {'profile_id': profile.id, 'driver': driver} scheduler.add_job( func=auto_rt, kwargs=params, trigger=IntervalTrigger(minutes=minutes), # trigger=IntervalTrigger(minutes=1), id='retweet_%s' % (json_data['id']), replace_existing=False, misfire_grace_time=app.config['MISFIRE_GRACE_TIME']) Profile.query.filter_by(id=profile.id).update({'run_status': 1}) else: Profile.query.filter_by(id=profile.id).update({'run_status': 0}) result['status'] = -1 result['msg'] = 'Driver is not available.' db.session.commit() db.session.close() return jsonify({'result': result})
def pauseProfile(): result = {} json_data = request.json profile = Profile.query.filter_by(id=json_data['id']).first() link = Link.query.filter_by(id=profile.linkid).join(Deck).first() if (profile == None): result['status'] = -1 result['msg'] = 'Profile is not existed.' elif (link.deck == None): result['status'] = -1 result['msg'] = 'Deck is not available.' else: result['status'] = 1 result['msg'] = 'Succefully Updated Profile.' if (profile.run_status == 1): jobid = 'retweet_%s' % (profile.id) if (is_running(jobid)): scheduler.remove_job(jobid) Profile.query.filter_by(id=profile.id).update({'run_status': 0}) db.session.commit() db.session.close() return jsonify({'result': result})
def runProfile(): result = {} json_data = request.json profile = Profile.query.filter_by(id=json_data['id']).first() link = Link.query.filter_by(id=profile.linkid).join(Deck).first() if (profile == None): result['status'] = -1 result['msg'] = 'Profile is not existed.' if(link.deck.login_code < 1): result['status'] = -1 result['msg'] = 'Please login or verify deck now.' elif (link.deck == None): result['status'] = -1 result['msg'] = 'Deck is not available.' else: result['status'] = 1 result['msg'] = 'Succefully run Profile.' if (profile.run_status < 1): driver = getdriver(link.deck.name) if (driver != None): jobid = 'retweet_%s' % (profile.id) if (is_running(jobid)): scheduler.remove_job(jobid) params = {'profile_id': profile.id, 'driver': driver} scheduler.add_job( func=auto_rt, kwargs=params, trigger=IntervalTrigger(minutes=profile.minutes), # trigger=IntervalTrigger(minutes=1), id='retweet_%s' % (json_data['id']), replace_existing=False, misfire_grace_time=app.config['MISFIRE_GRACE_TIME']) Profile.query.filter_by(id=profile.id).update({'run_status': 1}) else: result['status'] = -1 result['msg'] = 'Driver is not available.' db.session.commit() db.session.close() return jsonify({'result': result})
def acceptPendingById(): json_data = request.json result = {} approve = Approve.query.filter_by(id=json_data['id']).first() if(approve == None): result['status'] = -1 result['msg'] = 'Approve is not existed.' else: if (is_deck()): try: link = Link.query.filter_by(id=approve.linkid).join(Deck).first() profile = Profile.query.filter_by(linkid=link.id, keyword=approve.keyword).first() if (link.deck == None): result['status'] = -1 result['msg'] = 'Deck is not available.' else: if (profile != None): Profile.query.filter_by(id=profile.id).update({"minutes": approve.minutes}) result['status'] = 1 result['msg'] = 'Succefully added request to profile.' if (profile.run_status == 1): driver = getdriver(link.deck.name) if (driver != None): jobid = 'retweet_%s' % (profile.id) if (is_running(jobid)): scheduler.remove_job(jobid) params = {'profile_id': profile.id, 'driver': driver} scheduler.add_job( func=auto_rt, kwargs={'profile_id': profile.id, 'driver': driver}, trigger=IntervalTrigger(minutes=approve.minutes), # trigger=IntervalTrigger(minutes=1), id='retweet_%s' % (profile.id), replace_existing=False, misfire_grace_time=app.config['MISFIRE_GRACE_TIME']) Profile.query.filter_by(id=profile.id).update({'run_status': 1}) else: Profile.query.filter_by(id=profile.id).update({'run_status': 0}) result['status'] = -1 result['msg'] = 'Driver is not available.' db.session.delete(approve) db.session.commit() else: newprofile = Profile( linkid=link.id, keyword=approve.keyword, minutes=approve.minutes, run_status=link.deck.login_code ) db.session.add(newprofile) db.session.delete(approve) db.session.commit() result['status'] = 1 result['msg'] = 'Succefully added request to profile.' driver = getdriver(link.deck.name) if (driver != None): if (link.deck.login_code > 0): scheduler.add_job( func=auto_rt, kwargs={'profile_id': newprofile.id, 'driver': driver}, trigger=IntervalTrigger(minutes=approve.minutes), # trigger=IntervalTrigger(minutes=1), id='retweet_%s' % (newprofile.id), replace_existing=False, misfire_grace_time=app.config['MISFIRE_GRACE_TIME']) else: Profile.query.filter_by(id=newprofile.id).update({'run_status':0}) db.session.commit() result['status'] = -1 result['msg'] = 'Driver is not available.' except: result['status'] = 0 result['msg'] = 'Occured error in db session.' else: result['status'] = -1 result['msg'] = 'Please login now.' return jsonify({'result': result})
def delete_job(self, job): scheduler.remove_job(job.apscheduler_job_id) db.session.delete(job) db.session.commit()
def auto_rt(profile_id, driver): # if (type(profile_id) is int): # driver = driver # profile_id = profile_id # else: # temp = profile_id # profile_id = driver # driver = temp profile = (db.session.query( Profile.id, Deck.name, Profile.linkid, Profile.keyword, Profile.tweetid_list, Profile.last_tweetid, Profile.failed_tweetid_list, User.username).filter_by( id=profile_id).join(Link).join(Deck).join(User)).first() # last_time = profile.last_retweeted # if (last_time == None): # last_time = 0 # else: # last_time = time.mktime(last_time.timetuple()) keyword = profile.keyword if ('@' not in profile.keyword): keyword = '@%s' % profile.keyword if (isNotDeckOnline(driver)): deck = (db.session.query( Profile.linkid, Link.deckid, Deck.id, Deck.name, Deck.login_code).filter_by( id=profile_id).join(Link).join(Deck)).first() Deck.query.filter_by(id=deck.id).update({'login_code': -1}) linkid = profile.linkid profiles = Profile.query.filter_by(linkid=linkid).all() for prof in profiles: if (prof.run_status == 1): jobid = 'retweet_%s' % (prof.id) if (is_running(jobid)): scheduler.remove_job(jobid) Profile.query.filter_by(linkid=linkid).update({'run_status': 0}) db.session.commit() removedriver(deck.name) db.session.close() return bot = Retweeter(keyword, driver) if (profile.tweetid_list == ''): tweetlist = getTweetList(user=keyword, since_id=profile.last_tweetid, count=50) if (tweetlist['status'] == 1): lists = reversed(tweetlist['list']) else: Profile.query.filter_by(id=profile_id).update({'run_status': 0}) db.session.commit() jobid = 'retweet_%s' % (profile_id) if (is_running(jobid)): scheduler.remove_job(jobid) return tweetid_list = '' tweettext_list = '' last_tweetid = profile.last_tweetid for list in lists: id = list['id_str'] last_tweetid = id tweetid_list = tweetid_list + ',' + id text = list['text'] text = re.sub(r'[^\x00-\x7F]+', ' ', text) text = re.sub(r'[\n\r\t]+', ' ', text) if (text[:2] == 'RT'): index = text.index(':') + 1 text = text[index:] if ('<' in text): index = text.index('<') text = text[:index] if ('>' in text): index = text.index('>') text = text[:index] if ('"' in text): index = text.index('"') text = text[:index] if (''' in text): index = text.index(''') text = text[:index] if (len(text) > 20): text = text[:20] if (text == ''): print('blank') tweettext_list = tweettext_list + ' eof|,|eof ' + text tweetid_list = tweetid_list[1:] tweettext_list = tweettext_list[12:] Profile.query.filter_by(id=profile_id).update({ 'tweetid_list': tweetid_list, 'tweettext_list': tweettext_list, 'last_tweetid': last_tweetid }) db.session.commit() tweettext_list = Profile.query.filter_by( id=profile_id).first().tweettext_list text = tweettext_list.split(' eof|,|eof ')[0] tweetid_list = Profile.query.filter_by(id=profile_id).first().tweetid_list if (tweetid_list == ''): print 'There are no new tweets' return id = tweetid_list.split(',')[0] failed_tweetid_list = Profile.query.filter_by( id=profile_id).first().failed_tweetid_list retweeted = bot.retweets(text, id) if (retweeted == 1): log = Log(username=profile.username, deckname=profile.name, keyword=profile.keyword, retweetid=id) db.session.add(log) tweettext_list = tweettext_list.replace(text, '') if (tweettext_list != ''): tweettext_list = tweettext_list[11:] tweetid_list = tweetid_list.replace(id, '') if (tweetid_list != ''): tweetid_list = tweetid_list[1:] Profile.query.filter_by(id=profile_id).update({ 'tweetid_list': tweetid_list, 'tweettext_list': tweettext_list }) elif (retweeted == 0): tweetid_list = tweetid_list.replace(id, '') if (tweetid_list != ''): tweetid_list = tweetid_list[1:] tweettext_list = tweettext_list.replace(text, '') if (tweettext_list != ''): tweettext_list = tweettext_list[11:] failed_tweetid_list = failed_tweetid_list + ',' + id Profile.query.filter_by(id=profile_id).update({ 'tweetid_list': tweetid_list, 'tweettext_list': tweettext_list, "failed_tweetid_list": failed_tweetid_list }) Profile.query.filter_by(id=profile_id).update( {"last_retweeted": (datetime.datetime.utcnow())}) db.session.commit() db.session.close()
def delete_job(self, job): scheduler.remove_job(job.apscheduler_job_id) db.session.delete(job) db.session.commit()