def add_tweet_group(request): body = request.json sched_start_date = body['sched_start_date'] interval = body['interval'] conn = db.connect_db(dbname) cursor = db.get_cursor(conn) result = db.add_group(cursor, sched_start_date, interval) db.commit(conn) conn.close() res_body = {'gid': result} return res_body
def delete_tweet_group(request): body = request.json gid = body['gid'] conn = db.connect_db(dbname) cursor = db.get_cursor(conn) result = db.del_group(cursor, gid) db.commit(conn) conn.close() print("Deleted the tweet group with gid {}".format(result)) res_body = {'gid': result} return res_body
def delete_tweet(request): body = request.json id = body['id'] conn = db.connect_db(dbname) cursor = db.get_cursor(conn) db.del_tweet(cursor, id) db.commit(conn) conn.close() print("Deleted the tweet with id {}".format(id)) res_body = {'id': id} return res_body
def generate_sessionid(): # session IDの生成 (hash関数を使う) sessionid_src = str(datetime.datetime.now()) + config.RANDOM_SEED print("Session ID src: " + sessionid_src) sessionid = hashlib.sha256(sessionid_src.encode()).hexdigest() print("New Session ID: " + sessionid) # session IDのDBへの登録 # DBアクセス conn = db.connect_db(dbname) cursor = db.get_cursor(conn) db.register_sessionid(cursor, sessionid) db.commit(conn) conn.close() return sessionid
def authorize_sessionid(request): # RequestのSession IDの取得 ## sessionid = request.get_cookie("session") print("Request Session ID: " + str(sessionid)) # DBアクセスし、Session IDをチェック if sessionid != '' and sessionid != None: # DBアクセス conn = db.connect_db(dbname) cursor = db.get_cursor(conn) result = db.check_sessionid(cursor, sessionid) # 有効期間の更新 if result == True: db.extend_validate_period(cursor, sessionid) db.commit(conn) conn.close() return result else: return False
def update_tweet(request): # Tweet(RT, 画像付TweetもTweetとして取り扱う) 新規登録・更新用API。 # ポストされるデータのidの定義の有無で新規または更新を判定。 # 新規登録: # id未定義の場合。rt_flagが必須(0: tweet, 1: RT) # gid(推奨 default=0), subid(推奨 default=0), text(推奨 default='')、org_tweet_id(推奨 default=0) 。 # idを採番して返す。 # 更新: # id定義の場合。rt_flagが必須(0: tweet, 1: RT) # tweetの場合、text、retweetの場合、org_tweet_idが推奨(ブランクの場合は空欄または0で更新される) # gid, subidは無視。 ## Session ID認証成功の場合のメイン処理 req_error = False body = request.json # 入力のチェック try: id = body['id'] new_tweet = False except KeyError: new_tweet = True # rt_flag (必須) try: rt_flag = int(body['rt_flag']) except KeyError: rt_flag = 0 if rt_flag == 1: try: org_tweet_id = body['org_tweet_id'] except KeyError: org_tweet_id = 0 elif rt_flag == 0: try: text = body['text'] except KeyError: text = '' conn = db.connect_db(dbname) cursor = db.get_cursor(conn) if new_tweet == True: # 新規登録 try: gid = body['gid'] except KeyError: gid = 0 try: subid = body['subid'] except KeyError: subid = 0 if rt_flag == 0: id = db.add_tweet(cursor, gid, subid, text) # return gid print( "Added new tweet {} with gid {}, subid {}, and text {}".format( id, gid, subid, text)) elif rt_flag == 1: # Retweet対象のtweetのテキストを取得 org_tweet_text = get_formatted_org_tweet_text(org_tweet_id) id = db.add_retweet(cursor, gid, subid, org_tweet_id, org_tweet_text) print( "Added new retweet {} with gid {}, subid {}, and org_tweet_id {} ({})" .format(id, gid, subid, org_tweet_id, org_tweet_text)) else: # 更新 if rt_flag == 0: id = db.update_tweet(cursor, id, text) print("Updated the tweet with id {}, and text {}".format(id, text)) elif rt_flag == 1: # Retweet対象のtweetのテキストを取得 org_tweet_text = get_formatted_org_tweet_text(org_tweet_id) id = db.update_retweet(cursor, id, org_tweet_id, org_tweet_text) print("Updated the retweet with id {}, and org_tweet_id {}({})". format(id, org_tweet_id, org_tweet_text)) db.commit(conn) conn.close() res_body = {'id': id} if rt_flag == 1: res_body['org_tweet_text'] = org_tweet_text return res_body
for gid in gids: for tuple in tweets[gid]: # Tweet処理 if tuple['rt_flag'] == 0: res = tc.post_tweet(twitter, tuple['text']) elif tuple['rt_flag'] == 1: res = tc.retweet(twitter, tuple['org_tweet_id']) status_code = res.status_code # 結果の記録 # res のSuccess/Failed を個々のsched_tweetsに記録する。 if status_code == 200: status = "SUCCESS" tweet_id = json.loads(res.text)['id'] else: status = "FAILED" tweet_id = 0 conn = dbc.connect_db(dbname) cursor = dbc.get_cursor(conn) # 記録のDBへの保存 dbc.update_tweet_status(cursor, tuple['id'], tweet_id, status) dbc.commit(conn) dbc.close_db(conn) time.sleep(intervals[gid]) # time.sleep(10) # conn = dbc.connect_db(dbname) cursor = dbc.get_cursor(conn) dbc.update_tweet_group_status(conn, cursor, gid, "DONE") dbc.close_db(conn)