I am trying to:
Grab multiple tweets, find urls inside the tweets, grab the urls I found and save each "url data" to different txt file.
So far I managed to write these parts and I am stuck, can someone please help me?
Grab tweets:
import tweepy
from tweepy import OAuthHandler
import sys
import re
def process_or_store(tweet):
print(json.dumps(tweet))
consumer_key = '***************************'
consumer_secret = '******************************'
access_token = '*******************************'
access_secret = '****************************'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
f = open("C:\\Twitter\Twitts.txt",'w')
print >>f, 'twits'
for status in tweepy.Cursor(api.home_timeline).items(20):
# Process a single status
print(status.text)
f.write(status.text+ '\n')
def extract_urls(fname):
with open(fname) as f:
return re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', f.read())
Data from URL:
import urllib2
url = 'https://***************'
response = urllib2.urlopen(url)
with open('C:\\Twitter\Data_from_urls\ + "url"', 'w') as f:
f.write(response.read())
Related
I am trying to print all screen_names found on a search result with Twython.
Here is my current code
#Import the required modules
from twython import Twython
#Setting the OAuth
Consumer_Key = ''
Consumer_Secret = ''
Access_Token = '-'
Access_Token_Secret = ''
#Connection established with Twitter API v1.1
twitter = Twython(Consumer_Key, Consumer_Secret,
Access_Token, Access_Token_Secret)
#Twitter is queried
response = twitter.search(q='Roblox', since='2017-02-25', until='2017-02-26')
for tweet in response["statuses"]:
st = tweet["entities"]["user_mentions"]
screen_name = st[0]["screen_name"]
f = open("output.txt", "a")
f.write(str(screen_name, ",\n"))
print(screen_name)
f.close()
but it is not editing or printing in the log file. Right as I start it, the program stops with no apparent error.
(the codes have been filled in, just removed for the post)
I want an image to be sent as a retweet on Twitter via Tweepy, when the bot is tagged, but I've hit a wall and can not figure out why. It is detecting the image in the external file, but not using it. I know it's a bit vague, but the documentation on Tweepy is a pain for me to understand, as I just picked up Tweepy yesterday. Any help is appreciated!!!
import tweepy
import time
import os
#keys
CONSUMER_KEY = 'xxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxx'
ACCESS_KEY = 'xxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxx'
#auth
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
media = api.media_upload('cat_image.jpg')
#aquiring mentions
mentions = api.mentions_timeline()
#file import
FILE_NAME = 'last_seen_id.txt'
#operations
def retrieve_last_seen_id(file_name):
f_read = open(file_name, 'r')
last_seen_id = int(f_read.read().strip())
f_read.close()
return last_seen_id
def store_last_seen_id(last_seen_id, file_name):
f_write = open(file_name, 'w')
f_write.write(str(last_seen_id))
f_write.close()
return
def reply_to_tweets():
print('replying to tweets...')
#id fetching
last_seen_id = retrieve_last_seen_id(FILE_NAME)
mentions = api.mentions_timeline(
last_seen_id,
tweet_mode='extended')
for mention in reversed(mentions):
print(str(mention.id) + ' - ' + mention.full_text)
last_seen_id = mention.id
store_last_seen_id(last_seen_id, FILE_NAME)
if 'cat' in mention.full_text.lower():
print('found user #!')
print('responding....')
un = '#' + mention.user.screen_name
#response tweet
tweet = 'un'
api.update_status(un, media, mention.id)
while True:
reply_to_tweets()
time.sleep(9)
It's not clear to me what the exact error you're seeing is, but I think you're saying that the image file is read, and the Tweet is sent, but the media is not attached?
api.media_upload() returns a Media object, which will have its own values. You need to pass the media.media_id value as the parameter to update_status()
api.update_status(un, media.media_id, mention.id)
If the media uploaded successfully in the earlier step, that should work.
I'm trying to extract tweets using the historical Twitter API from Twitter and then copying them onto a text file. However, the text file is not being written to at all.
I've tried writing to a CSV though that has not worked either. It is being run on Python 3.6 and all the libraries are installed. I am not getting any error messages suggesting a problem with the text file.
import tweepy
import sys
import os
import codecs
CONSUMER_KEY = "" # These are removed for obvious reasons!
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
f = codecs.open('C:\\Users\\ctrh1\\Desktop\\tweets30apr.txt', "w", encoding="utf-8")
for tweet in tweepy.Cursor(api.search,
q="brexit",
count=100,
since="2019-04-28",
until="2019-04-29",
lang="en").items():
print(tweet.text)
f.write(tweet.text)
I would expect to have the text from some tweets written to file f, but it is blank after I have stopped the code from running.
Maybe you can first try if you are authorized to write with this minimal example:
file_name = 'C:\\Users\\ctrh1\\Desktop\\tweets30apr.txt'
with open(file_name, 'w') as f:
f.write("Hello World!")
I wasn't able to serialize Twitter API results until I used jsonpickle (that's code A below), but then I wasn't able to decode the json file (code B below). Code A created one large json object. Please help.
Code A
#!/usr/bin/env python
import tweepy
import simplejson as json
import jsonpickle
consumer_key = "consumer key here"
consumer_secret = "consumer secret here"
access_token = "access token here"
access_token_secret = "access token secret here"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.ModelParser())
jsonFile = open('/location_here/results.json', 'wb')
for status in tweepy.Cursor(api.search, q="keyword").items():
print status.created_at, status.text
frozen = jsonpickle.encode(status)
s = json.loads(frozen)
json.dump(s, jsonFile)
Code B
import jsonpickle
import simplejson as json
in_file = open("/location_here/results.json")
out_file = open("/location_here/data.json", 'wb')
jsonstr = in_file.read()
thawed = jsonpickle.decode(jsonstr)
data = json.loads(thawed)
This gives an error, ValueError: Trailing data.
Thanks.
I'm trying to post a tweet with the tweepy library. I use this code:
import tweepy
CONSUMER_KEY ="XXXX"
CONSUMER_SECRET = "XXXX"
ACCESS_KEY = "XXXX"
ACCESS_SECRET = "XXXX"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status('Updating using OAuth authentication via Tweepy!')
But when I run the application, I receive this error:
raise TweepError(error_msg, resp)
TweepError: Read-only application cannot POST.
How can I fix this?
In the application's settings, set your Application Type to "Read and Write". Then renegotiate your access token.
the code works for me with only
api.update_status (**status** = 'Updating using OAuth authentication via Tweepy!')
You have to set your app to read and write
After that, you'll be able to run your code.
the following python script will tweet a line from a text file. if you want to tweet multiple tweets just put them on a new line separated by a blank line.
import tweepy
from time import sleep
# import keys from cregorg.py
from credorg import *
client = tweepy.Client(bearer_token, consumer_key, consumer_secret, access_token, access_token_secret)
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)
print("Tweet-TXT-Bot v1.0 by deusopus (deusopus#gmail.com)")
# Open text file tweets.txt (or your chosen file) for reading
my_file = open('tweets.txt', 'r')
# Read lines one by one from my_file and assign to file_lines variable
file_lines = my_file.readlines()
# Close file
my_file.close()
# Tweet a line every 5 minutes
def tweet():
for line in file_lines:
try:
print(line)
if line != '\n':
api.update_status(line)
sleep(300)
else:
pass
except tweepy.errors.TweepyException as e:
print(e)
while True:
tweet()