2020-10-28 21:39:54 -04:00
|
|
|
import tweepy
|
|
|
|
import csv
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
#### Credentials
|
|
|
|
CONSUMER_KEY = os.environ.get('CONSUMER_KEY')
|
|
|
|
CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET')
|
|
|
|
ACCESS_KEY = os.environ.get('ACCESS_KEY')
|
|
|
|
ACCESS_SECRET = os.environ.get('ACCESS_SECRET')
|
|
|
|
|
|
|
|
#### Authorization
|
|
|
|
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
|
|
|
|
auth.set_access_token(access_token, access_token_secret)
|
|
|
|
api = tweepy.API(auth,wait_on_rate_limit=True)
|
|
|
|
|
|
|
|
#### Keywords
|
2020-10-29 12:24:09 -04:00
|
|
|
keywords = ['#SelfSovereignIdentity', '#DecentralizedIdentity', '#decentralizedidentity', "#IIW"]
|
2020-10-28 21:39:54 -04:00
|
|
|
|
|
|
|
#### Get Date
|
|
|
|
from datetime import date, timedelta, datetime
|
|
|
|
|
|
|
|
#### Find 7 Days Ago
|
|
|
|
current_date = date.today()
|
|
|
|
days_before = (date.today()-timedelta(days=7))
|
|
|
|
now = datetime.now()
|
|
|
|
date_time = current_date.strftime("%m%d%y")
|
|
|
|
|
|
|
|
#### Open CSV + Write Column Names
|
|
|
|
fname = 'ssitw' + date_time + '.csv'
|
2020-10-29 12:24:09 -04:00
|
|
|
csvFile = open(fname, 'w+')
|
2020-10-28 21:39:54 -04:00
|
|
|
csvWriter = csv.writer(csvFile)
|
2020-10-29 12:24:09 -04:00
|
|
|
csvWriter.writerow(["Time", "Link", "Text", "Hashtags", "User", "Favorites", "Following", "Followers", "Retweets", "Urls", "UrlTitle", "UrlDesc", "UrlImg", "ImageUrls", "ReplyURL","QuoteID","QuoteText","QuoteImg","QuoteUrl"])
|
2020-10-29 17:32:18 -04:00
|
|
|
lines_seen = []
|
|
|
|
|
2020-10-28 21:39:54 -04:00
|
|
|
|
|
|
|
for keyword in keywords:
|
2020-10-29 17:32:18 -04:00
|
|
|
# Search hashtags\keywords
|
2020-10-28 21:39:54 -04:00
|
|
|
for tweet in tweepy.Cursor(api.search,q=keyword + '-filter:retweets',count=100, tweet_mode="extended", lang="en", since=days_before).items():
|
2020-10-29 17:32:18 -04:00
|
|
|
### Set \ Reset Variables
|
2020-10-28 21:39:54 -04:00
|
|
|
medias = []
|
|
|
|
lnks = []
|
|
|
|
replink = ""
|
|
|
|
rtsr = []
|
|
|
|
title = []
|
|
|
|
description = []
|
|
|
|
image = []
|
2020-10-29 12:24:09 -04:00
|
|
|
qtid = ''
|
|
|
|
qttext = ''
|
|
|
|
qtmedia = ['']
|
|
|
|
qturls = ['']
|
|
|
|
|
2020-10-28 21:39:54 -04:00
|
|
|
retweetcount = tweet.retweet_count
|
|
|
|
favorites = tweet.favorite_count
|
|
|
|
following = tweet.user.friends_count
|
|
|
|
followers = tweet.user.followers_count
|
|
|
|
username = tweet.user.screen_name
|
|
|
|
id = "https://twitter.com/" + username + "/status/" + tweet.id_str
|
|
|
|
text = tweet.full_text
|
|
|
|
hashtags = [hashtag['text'] for hashtag in tweet.entities["hashtags"]]
|
|
|
|
created = tweet.created_at
|
|
|
|
try:
|
|
|
|
reply = tweet.in_reply_to_status_id_str
|
|
|
|
user = tweet.in_reply_to_user_id_str
|
|
|
|
replink = "https://twitter.com/" + user + "/" + reply
|
|
|
|
except:
|
|
|
|
print("no reply url")
|
|
|
|
if 'media' in tweet.entities:
|
|
|
|
for media in tweet.extended_entities['media']:
|
|
|
|
medias.append(media['media_url_https'])
|
2020-10-29 17:32:18 -04:00
|
|
|
### Add URLs to array
|
2020-10-28 21:39:54 -04:00
|
|
|
if 'urls' in tweet.entities:
|
2020-10-29 17:32:18 -04:00
|
|
|
quoted = 'FALSE'
|
2020-10-28 21:39:54 -04:00
|
|
|
for url in tweet.entities['urls']:
|
2020-10-29 17:32:18 -04:00
|
|
|
lkn = url['expanded_url']
|
2020-10-28 21:39:54 -04:00
|
|
|
lnks.append(url['expanded_url'])
|
|
|
|
from webpreview import web_preview
|
2020-10-29 17:32:18 -04:00
|
|
|
if username == "Docbasia" or 'twitter.com' in lkn: continue
|
2020-10-28 21:39:54 -04:00
|
|
|
else:
|
|
|
|
try:
|
2020-10-29 17:32:18 -04:00
|
|
|
### get title img description
|
2020-10-28 21:39:54 -04:00
|
|
|
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
|
2020-10-29 17:32:18 -04:00
|
|
|
tit, desc, ima = web_preview(lkn,timeout=10,headers=headers,parser='lxml')
|
2020-10-28 21:39:54 -04:00
|
|
|
title.append(tit)
|
|
|
|
description.append(desc)
|
|
|
|
image.append(ima)
|
|
|
|
except:
|
|
|
|
print("broken link")
|
2020-10-29 17:32:18 -04:00
|
|
|
### If it's a quote-tweet, get original stats
|
2020-10-29 12:24:09 -04:00
|
|
|
if hasattr(tweet, 'quoted_status'):
|
|
|
|
qtmedia = ['']
|
|
|
|
qturls = ['']
|
|
|
|
qttext = tweet.quoted_status.full_text
|
|
|
|
qtuser = tweet.quoted_status.user.screen_name
|
|
|
|
qtid = "https://twitter.com/" + qtuser + "/status/" + tweet.quoted_status.id_str
|
|
|
|
if 'media' in tweet.quoted_status.entities:
|
|
|
|
for media in tweet.quoted_status.extended_entities['media']:
|
|
|
|
qtmedia.append(media['media_url_https'])
|
|
|
|
if 'urls' in tweet.quoted_status.entities:
|
|
|
|
for url in tweet.quoted_status.entities['urls']:
|
|
|
|
qturls.append(url['expanded_url'])
|
|
|
|
seen = 'FALSE'
|
2020-10-29 17:32:18 -04:00
|
|
|
count = 0
|
|
|
|
for y in lines_seen:
|
|
|
|
if id == y:
|
2020-10-29 12:24:09 -04:00
|
|
|
seen = 'TRUE'
|
|
|
|
|
2020-10-29 17:32:18 -04:00
|
|
|
#### Column attributes
|
|
|
|
line = [created, id, text, hashtags, username, favorites, following, followers, retweetcount, lnks, title, description, image, medias, replink, qtid, qttext, qtmedia, qturls]
|
|
|
|
|
|
|
|
#### Only add line to csv if it's not already been added
|
|
|
|
lines_seen.append(id)
|
2020-10-29 12:24:09 -04:00
|
|
|
if seen == 'TRUE' or username == "Docbasia" :
|
2020-10-29 17:32:18 -04:00
|
|
|
print("Seen. Don't Save.")
|
2020-10-29 12:24:09 -04:00
|
|
|
else:
|
|
|
|
csvWriter.writerow(line)
|
|
|
|
print(line)
|
2020-10-28 21:39:54 -04:00
|
|
|
csvFile.close()
|