Skip to content

Instantly share code, notes, and snippets.

@vwillcox
Last active April 19, 2022 18:42
Show Gist options
  • Save vwillcox/ea1164cf592ef1d674571263fed86b1a to your computer and use it in GitHub Desktop.
Save vwillcox/ea1164cf592ef1d674571263fed86b1a to your computer and use it in GitHub Desktop.
Quick and Dirty Word cloud in Python!
import tweepy
import time
import matplotlib.pyplot as py
from wordcloud import WordCloud,STOPWORDS
from PIL import Image
import numpy as np
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--twittername", help="Twitter handle to use", required=True)
parser.add_argument("-m", "--mask", help="Masking Image to use", required=True)
parser.add_argument("-o", "--output", help="Output File Name", required=True)
args = parser.parse_args()
config = vars(args)
consumer_key = "<KEY>" #Enter your key as string
consumer_secret = "<KEY>"
access_token = "<KEY>"
access_token_secret = "<KEY>"
screenname = args.twittername
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
tweets = api.user_timeline(screen_name=screenname,count=5000)
tempwordfile = "file.txt"
maskingfilename = args.mask
wordcloudfile = args.output
f= open (tempwordfile,"w+")
for info in tweets:
f.write(str(info.text))
f.write("\n")
f.close()
f= open (tempwordfile,"r")
words=f.read()
f.close()
stopwords = set(STOPWORDS)
stopwords.add('https')
stopwords.add('t')
stopwords.add('co')
stopwords.add('https://t.co')
twitter_mask= np.array(Image.open(maskingfilename)) #sitr.jpg image name
wCloud= WordCloud(
background_color='black',
mask=twitter_mask,
contour_width=1,
contour_color='steelblue',
stopwords=stopwords
).generate(words)
wCloud.to_file(wordcloudfile)
@vwillcox
Copy link
Author

To use run as follows
$ python3 wordcloud.py -t TWITTERHANDLE -m MASKINGIMAGE -o FILENAME_FOR_Output

python3 wordcloud.py -t elonmusk -m muskface.png -o muskwordcloud.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment