I'm trying to create a bit of code to listen certain keywords on Twitter and am struggling to get the results I want.
I'm using Python 3.4.3.
Here's what I have that's working so far...
import tweepy
from time import sleep
CONSUMER_KEY = 'abcabcabc'
CONSUMER_SECRET = 'abcabcabc'
ACCESS_KEY = 'abcabcabc'
ACCESS_SECRET = 'abcabcabc'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
auth.secure = True
api = tweepy.API(auth)
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
try:
print(status.text)
except:
print("false")
myStreamListener = MyStreamListener
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener())
myStream.filter(track=['Cycling', 'Running'])
I'm trying to an if and elif statements to print different results depending on if the Tweet is Cycling or Running. I've used this code, but can't get it to work...
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if 'Cycling' in myStreamListener:
print('Cyclist' + status.text)
elif 'Running' in myStreamListener:
print('Runner' + status.text)
else:
print('false' + status.text)
myStreamListener = MyStreamListener
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener())
myStream.filter(track=['Cycling', 'Running'])
I can get the if & elif to work offline when not adding the complexity of Tweepy into the equation, but am confused about exactly how to use the in statement.
I'm new to Python so will more than likely be making some simple mistakes!
Any help would be greatly appreciated,
Many thanks,
Matt
You are using in with the wrong variable (which points to the listener function). Use the status variable instead:
if 'cycling' in status.text.lower():
print('Cyclist' + status.text)
Note that I added .lower() to the end of status.text. That way it will match the word 'cycling' regardless of if it is capitalised or not.
Related
I want to download the last publicly available file from https://sam.gov/data-services/Exclusions/Public%20V2?privacy=Public
while trying to download manually, the real download links look like:
https://falextracts.s3.amazonaws.com/Exclusions/Public%20V2/SAM_Exclusions_Public_Extract_V2_22150.ZIP?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220530T143743Z&X-Amz-SignedHeaders=host&X-Amz-Expires=2699&X-Amz-Credential=AKIAY3LPYEEXWOQWHCIY%2F20220530%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=3eca59f75a4e1f6aa59fc810da8f391f1ebfd8ca5a804d56b79c3eb9c4d82e32
My function gets only initial link, which refers to the real link:
import json
import requests
from operator import itemgetter
files_url = 'https://sam.gov/api/prod/fileextractservices/v1/api/listfiles?random=1653676394983&domain=Exclusions/Public%20V2&privacy=Public'
def get_file():
response = requests.get(files_url, stream=True)
links_resp = json.loads(response.text)
links_dicts = [d for d in links_resp['_embedded']['customS3ObjectSummaryList'] if d['displayKey'].count('SAM_Exclus')]
sorted_links = sorted(links_dicts, key=itemgetter('dateModified'), reverse=True)
return sorted_links[0]['_links']['self']['href']
get_file()
Result:
'https://s3.amazonaws.com/falextracts/Exclusions/Public V2/SAM_Exclusions_Public_Extract_V2_22150.ZIP'
But by following the above link, I get Access denied
So I will appreciate any hints on how to get real download links
I've edited your code as much as possible so you can understand. The requests library can convert it to json itself.
imports that are not at the beginning of the code do not look very good for reading...
import requests as req
from operator import itemgetter
files_url = "https://sam.gov/api/prod/fileextractservices/v1/api/listfiles?random=1653676394983&domain=Exclusions/Public%20V2&privacy=Public"
down_url = "https://sam.gov/api/prod/fileextractservices/v1/api/download/Exclusions/Public%20V2/{}?privacy=Public"
def get_file():
response = req.get(files_url, stream=True).json()
links_dicts = [d for d in response["_embedded"]["customS3ObjectSummaryList"]]
sorted_links = sorted(links_dicts, key=itemgetter('dateModified'), reverse=True)
key = sorted_links[0]['displayKey']
down = req.get(down_url.format(key))
if not down.status_code == 200:
return False
print(key)
open(key, 'wb').write(down.content)
return True
get_file()
So i've been working on this script for few days now and the script seems to run correctly without getting any errors but the issue is that my point of the script is getting specific user's status changes and instead of that the script prints me everytime ANYONE from my contants changes they're status inculding myself.
Please, does anyone think he can assist me ? I have been stuck on it for too long now and I am really desprate make that script works..
By the way, I was adding print(client.user_id) just to see if it works and I received the User ID of anyone in my contacts who made some kind of action.
from telethon.tl.types import UserStatusOffline
from telethon.sync import TelegramClient
from telethon import events
from datetime import datetime
import time
### Client Side ###
target = ""
phone = "+"
api_id =
api_hash = ""
client = TelegramClient(phone, api_id, api_hash)
client.start()
if client.is_user_authorized():
print(f"Session started at : {datetime.now()}")
time.sleep(2)
print("Logging in to Telegram complete.")
time.sleep(2)
else:
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
print(f"Started listening to {target}'s status...")
time.sleep(2)
target_id = client.get_peer_id(target)
print(f"{target}'s User ID is : {target_id}")
time.sleep(2)
############################################
# First status check to see rather #
# the user is correctly online or not #
# once it prints hes correct statement #
# global value will be changed so it wont #
# be printed again and again. #
############################################
first_msg = False
async def first_con():
if first_msg == False:
account = await client.get_entity(target)
if isinstance(account.status, UserStatusOffline):
print(f"{target} is correctly Offline.")
first_msg = True
else:
print(f"{target} is correctly Online.")
first_msg = True
else:
print("Something went wrong checking correct status.")
##################EVENTS####################
# Only events thats occurred after script #
# first run will pop up with prints ! #
# Every event that doesn't come from the #
# target gets as "event from username" #
############################################
#client.on(events.UserUpdate())
async def handler(event):
await first_con()
time.sleep(2)
if event.user_id == target_id:
if event.online:
print(f"{target} went Online at : {datetime.now()}")
elif event.recently:
print(f"{target} was recently online at : {datetime.now()}")
elif event.typing:
print(f"{target} typed a message at : {datetime.now()}")
else:
print("Sorry there was an error.")
else:
#print("Event from non-intersting user.") debugging propuses only
client.run_until_disconnected()
Let's zoom in on the code:
#client.on(events.UserUpdate())
async def handler(event):
x = await client.get_entity(target)
target_id = x.id
event.input_user = target_id
if event.input_user == target_id:
Let's tear it apart:
event.input_user = target_id
if event.input_user == target_id:
Let's give it different names. event.input_user will be foo and target_id will be bar:
foo = bar
if foo == bar:
You just assigned a value to the first variable, and are then comparing the first variable to that same value. Of course, this is always True, for any foo and bar values.
Modifying the event is generally a bad idea and I'm not sure what your intention was, but it seems to me that you should be caching the integer ID and then comparing that instead of making the call to get_entity every time (because input_user is a InputPeerUser and won't compare with User, which is why I guess you tried the weird assign):
target_id = client.get_peer_id(target)
#client.on(events.UserUpdate)
async def handler(event):
if event.user_id == target_id:
...
This way only one call is made at most, and you compare integers which is fast. Make sure to check the documentation on UserUpdate for more information.
Im testing new things and I landed on python... As first code i found the classic hangman game.
I followed a tutorial that shows how to do it step by step but when it comes to wordlist I've got lost...
So far I've been retrieving the words from a variable "_WORDS" which it has some strings as properties. I have now a "words.txt" file from which I want to get the words instead of the variable _WORDS = ("word1", "word2", "etc"). I found some examples but I cant hack it... some inspiration? This is my code...
_WORDS = open("words.txt", 'r')
_IFYOUWIN = ("you won!")
def __init__(self):
self._word = choice(self._WORDS)
self._so_far = "-" * len(self._word)
self._used = []
self._wrong_answers = 0
def play(self):
self._reset_game()
self._start_game()
while self._wrong_answers < len(self._HANGMAN) and self._so_far != self._word:
self._print_current_progress()
guess = self._user_guess()
self._check_answer(guess)
I tried importing "os" like this:
import os
_WORDS = os.path.expanduser("~/Desktop/pythontlearn/words.txt")
but no luck.
for the record I found this working for me
path = '/words.txt'
_WORDS = open(path,'r')
but Im still not sure why it does not work with something like
_WORDS = open("words.txt", 'r')
they look exactley the same to me
I've created a python script with tweepy that replies to suicidal tweets with a link to a support website. However, nothing happens when I run the code and tweet with any of the code words on a different account. I'm opening and running the .py file in command prompt.
Like I said, I've tried using the specific words that should trigger it but it does not reply.
import tweepy
#the following module is a file with the specific keys set in
#a dictionary to the given variable, don't want to show them due to
#privacy/security
from keys import keys
CONSUMER_KEY = keys['consumer_key']
CONSUMER_SECRET = keys['consumer_secret']
ACCESS_TOKEN = keys['access_token']
ACCESS_TOKEN_SECRET = keys['access_token_secret']
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
twts = api.search(q="suicide")
t = ['suicide',
'kill myself',
'hate myself',
'Suicidal',
'self-harm',
'self harm']
for s in twts:
for i in t:
if i == s.text:
sn = s.user.screen_name
m = "#%s You are loved! For help, visit https://suicidepreventionlifeline.org/" % (sn)
s = api.update_status(m, s.id)
It should reply with a help link, but it doesn't and I don't know what I did wrong in my code. Any help?
Replace :
if i == s.text:
with :
if i in s.text:
Or to match words case sensitive, the best should be :
if i.lower() in s.text.lower():
Because Suicidal (an other words from the t array) can't be equal to the tweet text.
I guess you want to check if the text contains this word.
This code opens a twitter listener, and the search terms are in the variable, upgrades_str. Some searches work, and some don't. I added AMZN to the upgrades list just to be sure there's a frequently used term since this is using an open Twitter stream, and not searching existing tweets.
Below, I think we only need to review numbers 2 and 4.
I'm using Python 3.5.2 :: Anaconda 4.0.0 (64-bit) on Windows 10.
Variable searches
Searching with: upgrades_str: ['AMZN', 'SWK', 'AIQUY', 'SFUN', 'DOOR'] = returns tweets such as 'i'm tired of people'
Searching with: upgrades_str: ['$AMZN', '$SWK', '$AIQUY', '$SFUN', '$DOOR'] = returns tweets as as 'Chicago to south Florida. Hiphop lives'. This search is the one I wish worked.
Explicit searches
Searching by replacing the variable 'upgrades_str' with the explicit string: ['AMZN', 'SWK', 'AIQUY', 'SFUN', 'DOOR'] = returns 'After being walked in on twice, I have finally figured out how to lock the door here in Sweden'. This one at least has the search term 'door'.
Searching by replacing the variable 'upgrades_str' with the explicit string: ['$AMZN', '$SWK', '$AIQUY', '$SFUN', '$DOOR'] = returns '$AMZN $WFM $KR $REG $KIM: Amazon’s Whole Foods buy puts shopping centers at risk as real'. So the explicit call works, but not the identical variable.
Explicitly searching for ['$AMZN'] = returns a good tweet: 'FANG setting up really good for next week! Added $googl jun23 970c avg at 4.36. $FB $AMZN'.
Explicitly searching for ['cool'] returns 'I can’t believe I got such a cool Pillow!'
import tweepy
import dataset
from textblob import TextBlob
from sqlalchemy.exc import ProgrammingError
import json
db = dataset.connect('sqlite:///tweets.db')
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
if status.retweeted:
return
description = status.user.description
loc = status.user.location
text = status.text
coords = status.coordinates
geo = status.geo
name = status.user.screen_name
user_created = status.user.created_at
followers = status.user.followers_count
id_str = status.id_str
created = status.created_at
retweets = status.retweet_count
bg_color = status.user.profile_background_color
blob = TextBlob(text)
sent = blob.sentiment
if geo is not None:
geo = json.dumps(geo)
if coords is not None:
coords = json.dumps(coords)
table = db['tweets']
try:
table.insert(dict(
user_description=description,
user_location=loc,
coordinates=coords,
text=text,
geo=geo,
user_name=name,
user_created=user_created,
user_followers=followers,
id_str=id_str,
created=created,
retweet_count=retweets,
user_bg_color=bg_color,
polarity=sent.polarity,
subjectivity=sent.subjectivity,
))
except ProgrammingError as err:
print(err)
def on_error(self, status_code):
if status_code == 420:
return False
access_token = 'token'
access_token_secret = 'tokensecret'
consumer_key = 'consumerkey'
consumer_secret = 'consumersecret'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
stream_listener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener)
stream.filter(track=upgrades_str, languages=['en'])
Here's the answer, in case someone has the problem in the future: "Note that punctuation is not considered to be part of a #hashtag or #mention, so a track term containing punctuation will not match either #hashtags or #mentions." From: https://dev.twitter.com/streaming/overview/request-parameters#track
And for multiple terms, the string, which was converted from a list, needs to be changed to ['term1,term2']. Just strip out the apostrophes and spaces:
upgrades_str = re.sub('[\' \[\]]', '', upgrades_str)
upgrades_str = '[\''+format(upgrades_str)+'\']'