def retweet_tweepy(userid, query, cnt, api, posts): search_results = api.search(q=query, count=cnt) for tweet in search_results: post = {} try: if not "RT @" in tweet.text: #3 tweet_id = tweet.id api.retweet(tweet_id) #ファボする post["created_at"] = tweet.created_at post["user_id"] = tweet.user.screen_name post["text"] = tweet.text.replace('\n', '') post["fav"] = tweet.favorite_count post["retweet"] = tweet.retweet_count post["select"] = "リツイート" posts.append(post) db = get_db() db.execute( "INSERT INTO tweet (id,created_at,user_id,text,fav,retweet,action) values(?,?,?,?,?,?,?)", (userid, post["created_at"], post["user_id"], post["text"], post["fav"], post["retweet"], post["select"])) db.commit() close_db() except Exception as e: print(e) return posts
def tweet(): if request.method == 'POST': try: db = get_db() d = db.cursor() error_message = None # pass1=[] # user1=[] username = request.form['username'] password = request.form['pass'] user = d.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if user is None: error_message = 'ユーザー名が正しくありません' elif not check_password_hash(user['password'], password): error_message = 'パスワードが正しくありません' if error_message is not None: # エラーがあればそれを表示したうえでログイン画面に遷移 return render_template('auth/login.html', message2=error_message) session["user_id"] = user["id"] session["username"] = user["username"] db.commit() close_db() return redirect(url_for('auth.index')) except Exception as e: print(e) message2 = "usernameが間違っております" return render_template('auth/login.html', message2=message2) else: return render_template('auth/login.html')
def sessget(userid): db = get_db() d = db.cursor() d.execute('select id,ck,cs,at,ats from api') for d2 in d: if (d2["id"] == userid): ck = str(d2["ck"]) cs = str(d2["cs"]) at = str(d2["at"]) ats = str(d2["ats"]) auth = tweepy.OAuthHandler(ck, cs) auth.set_access_token(at, ats) api = tweepy.API(auth) return api
def login2(): if request.method == 'POST': try: db = get_db() username = request.form['username'] password = request.form['password'] CONSUMER_KEY = request.form['ck'] CONSUMER_SECRET = request.form['cs'] ACCESS_TOKEN = request.form['at'] ACCESS_SECRET = request.form['ats'] db.execute( "INSERT INTO user (username,password) values(?,?)", (username, generate_password_hash(str(password), method='sha256'))) db.execute( "INSERT INTO api (ck,cs,at,ats) values(?,?,?,?)", (CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET)) # #tweepy # auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) # auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) # #グローバル変数 # global api # #APIインスタンスを作成 # api = tweepy.API(auth) db.commit() close_db() # index.html をレンダリングする return render_template('auth/login.html') except Exception as e: print(e) message = "そのusernameはすでに使われております" username = request.form['username'] password = request.form['password'] CONSUMER_KEY = request.form['ck'] CONSUMER_SECRET = request.form['cs'] ACCESS_TOKEN = request.form['at'] ACCESS_SECRET = request.form['ats'] return render_template('auth/create_user2.html', message=message, username=username, password=password, ck=CONSUMER_KEY, cs=CONSUMER_SECRET, at=ACCESS_TOKEN, ats=ACCESS_SECRET) else: return render_template('auth/login.html')
def logcheck(): db = get_db() c = db.cursor() datas = [] c.execute("select * from tweet") userid = session["user_id"] # print(userid) for d3 in c: data = {} if (d3["id"] == userid): data["created_at"] = str(d3["created_at"]) data["text"] = str(d3["text"]) data["user_id"] = str(d3["user_id"]) data["fav"] = str(d3["fav"]) data["retweet"] = str(d3["retweet"]) data["action"] = str(d3["action"]) datas.append(data) close_db() return render_template('tweet/logcheck.html', posts=datas)
def getTweetBySearch(userid, s, since, until, cnt, api, posts2): result = [] ## vars sinceDate = since # この日付以降のツイートを取得する untilDate = until # この日付以前のツイートを取得する # 検索用文字列(リツイートは除外する) sratchStr = s + ' exclude:retweets' print('検索文字列 : ' + sratchStr) tweets = tweepy.Cursor(api.search, q = s, \ include_entities = True, \ tweet_mode = 'extended', \ since = sinceDate, \ until = untilDate,lang = 'ja').items() counter = 0 for tweet in tweets: post = {} try: post["twid"] = tweet.id post["user_id"] = tweet.user.screen_name post["text"] = tweet.full_text post["created_at"] = tweet.created_at post["retweet"] = tweet.retweet_count post["fav"] = tweet.favorite_count post["select"] = "検索" posts2.append(post) db = get_db() db.execute( "INSERT INTO tweet (id,created_at,user_id,text,fav,retweet,action) values(?,?,?,?,?,?,?)", (userid, post["created_at"], post["user_id"], post["text"], post["fav"], post["retweet"], post["select"])) db.commit() close_db() except Exception as e: print(e) counter += 1 if counter == cnt: break return posts2
from flask import (Blueprint, render_template, request) import tweepy, time from app.userdb import get_db, close_db from app.function import like_tweepy, get_sorted_df, get_grouped_df, get_profile, retweet_tweepy, follow_tweepy, like_tweepy from werkzeug.security import generate_password_hash, check_password_hash import pandas as pd from flask_login import LoginManager, login_user, logout_user, login_required, UserMixin from collections import defaultdict db = get_db() # モデルに関する設定 class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(100), unique=True) name = db.Column(db.String(1000)) password = db.Column(db.String(100)) # モデルからインスタンスを生成するときに使います。(利便性を高めるため) # passwordの暗号化も自動で行うことができるので、安全性も高めることができます。 @classmethod def from_args(cls, name: str, email: str, password: str): instance = cls() instance.name = name instance.email = email if password is not None: # passwordがあれば暗号化します。 instance.hash_password(password) return instance