def getAuth(): auth = tweepy.OAuthHandler(config.getApiKey(), config.getApiSecret()) auth.set_access_token(config.getAccessToken(), config.getAccessSecret()) return auth
from flask import Flask, request, abort import config from game import Game from linebot import (LineBotApi, WebhookHandler) from linebot.exceptions import (InvalidSignatureError) from linebot.models import (MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, TemplateSendMessage, ButtonsTemplate, PostbackAction, PostbackEvent) app = Flask(__name__) line_bot_api = LineBotApi(config.getAccessToken()) handler = WebhookHandler(config.getChannelSecret()) @app.route("/callback", methods=['POST']) def callback(): # get X-Line-Signature header value signature = request.headers['X-Line-Signature'] # get request body as text body = request.get_data(as_text=True) app.logger.info("Request body: " + body) # handle webhook body try: handler.handle(body, signature) except InvalidSignatureError: print( "Invalid signature. Please check your channel access token/channel secret."
1 2 3 # coding: utf-8 import datetime import requests import tweepy import os import config # apiを取得 auth = tweepy.OAuthHandler(config.getConsumerKey(), config.getConsumerSecret()) auth.set_access_token(config.getAccessToken(), config.getAccessSecret()) api = tweepy.API(auth) def tweet_search(location, restaurant_name): tweet_list = [] searchWord = [location, restaurant_name] # 検索ワード複数 for status in api.search( q=searchWord, lang='ja', result_type='recent', count=10): # result_type は recent or popular or mixed userID = "ユーザーID:" + status.user.name # userIDを表示 userName = "******" + status.user.screen_name # ユーザー名を表示 time = status.created_at + datetime.timedelta(hours=9) tweet_Time = "投稿日時:" + str( status.created_at + datetime.timedelta(hours=30)) # 投稿日時を表示 tweet = status.text # ツイートを表示 tweet_dic = { "userID": userID, "userName": userName,
import tweepy import textblob import config consumer_key = config.getConsumerKey() consumer_secret = config.getConsumerSecret() access_token = config.getAccessToken() access_token_secret = config.getAccessTokenSecret() auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) def getSentiment(keyWord): tweets = api.search(keyWord) sentimentValues = [] for tweet in tweets: if tweet.lang == "en": analysis = textblob.TextBlob(tweet.text) sentimentValues.append(analysis.sentiment.polarity) print(sentimentValues) Average_Sentiment = float(sum(sentimentValues)) / max( len(sentimentValues), 1) return Average_Sentiment def collectSentiment(): Avg_Sentiment = getSentiment('God') print("Average Sentiment: " + str(Avg_Sentiment)) return Avg_Sentiment