Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Discord Twitter Bot Modifiy
#1
Hi!

I'm looking for a kind soul that could help me remove unnecessary "spam", or replies from this Discord Twitter Bot.
I've tried to modify the code myself without any success. :(

The script are using tweepy in background.

What I want it to follow: http://twitter.com/BlizzardCSEU_EN
What it does actually follow: http://twitter.com/BlizzardCSEU_EN/with_replies

Which make my channel looks like this: [Image: Kmnxw7U.png]

When I only want major posts like this: [Image: WDG2Mtx.png]

Code:
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler, Stream
from discordWebhooks import Webhook, Attachment, Field
import calendar, time, random, json
from time import gmtime, strftime

class StdOutListener(StreamListener):
    def on_status(self, status):
        """Called when a new status arrives"""

        colors = ['#7f0000', '#535900', '#40d9ff', '#8c7399', '#d97b6c', '#f2ff40', '#8fb6bf', '#502d59', '#66504d',
                  '#89b359', '#00aaff', '#d600e6', '#401100', '#44ff00', '#1a2b33', '#ff00aa', '#ff8c40', '#17330d',
                  '#0066bf', '#33001b', '#b39886', '#bfffd0', '#163a59', '#8c235b', '#8c5e00', '#00733d', '#000c59',
                  '#ffbfd9', '#4c3300', '#36d98d', '#3d3df2', '#590018', '#f2c200', '#264d40', '#c8bfff', '#f23d6d',
                  '#d9c36c', '#2db3aa', '#b380ff', '#ff0022', '#333226', '#005c73', '#7c29a6']

        try:
            data = status._json


            with open('data.json') as data_file:
                dataDiscords = json.load(data_file)

            for dataDiscord in dataDiscords['Discord']:

                if data['user']['id_str'] in dataDiscord['twitter_ids']:  # filter out tweets from people replying to dota 2 personalities

                    for wh_url in dataDiscord['webhook_urls']:

                        wh = Webhook(url=wh_url)

                        text = ''
                        if 'extended_tweet' in data:
                            text = data['extended_tweet']['full_text']
                        else:
                            text = data['text']

                        for url in data['entities']['urls']:
                            if url['expanded_url'] == None:
                                continue
                            text = text.replace(url['url'], "[%s](%s)" %(url['display_url'],url['expanded_url']))

                        for userMention in data['entities']['user_mentions']:
                            text = text.replace('@%s' %userMention['screen_name'], '[@%s](http://twitter.com/%s)' %(userMention['screen_name'],userMention['screen_name']))

                        media_url = ''
                        if 'extended_tweet' in data:
                            if 'media' in data['extended_tweet']['entities']:
                                for media in data['extended_tweet']['entities']['media']:
                                    if media['type'] == 'photo':
                                        media_url = media['media_url']

                        if 'media' in data['entities']:
                            for media in data['entities']['media']:
                                if media['type'] == 'photo':
                                    media_url = media['media_url']


                        at = Attachment(author_name=data['user']['screen_name'],
                                        author_icon=data['user']['profile_image_url'],
                                        color=random.choice(colors), pretext=text,
                                        image_url=media_url,
                                        title_link="https://twitter.com/" + data['user']['screen_name'] + "/status/" + str(data['id_str']),
                                        footer="Tweet created on",
                                        footer_icon="https://cdn1.iconfinder.com/data/icons/iconza-circle-social/64/697029-twitter-512.png",
                                        ts=calendar.timegm(time.strptime(data['created_at'], '%a %b %d %H:%M:%S +0000 %Y')))


                        print(strftime("[%Y-%m-%d %H:%M:%S]", gmtime()), data['user']['screen_name'], 'twittered.')

                        wh.addAttachment(at)


                        if ('quoted_status' in data):


                            text = data['quoted_status']['text']
                            for url in data['quoted_status']['entities']['urls']:
                                if url['expanded_url'] == None:
                                    continue
                                text = text.replace(url['url'], "[%s](%s)" % (url['display_url'], url['expanded_url']))

                            for userMention in data['quoted_status']['entities']['user_mentions']:
                                text = text.replace('@%s' %userMention['screen_name'], '[@%s](http://twitter.com/%s)' %(userMention['screen_name'],userMention['screen_name']))



                           field = Field(data['quoted_status']['user']['screen_name'], text)
                           at.addField(field)

                        wh.post()

        except:
            print('@@@@@@@@@@@@@@@@@@@@@@')
            print(data)
            print(type(data))

        return True

    def on_limit(self, track):
        """Called when a limitation notice arrives"""
        print('on_limit')
        print(track)
        return

    def on_error(self, status_code):
        """Called when a non-200 status code is returned"""
        print('on_error')
        print(status_code)
        return False

    def on_disconnect(self, notice):
        """Called when twitter sends a disconnect notice
        Disconnect codes are listed here:
        https://dev.twitter.com/docs/streaming-apis/messages#Disconnect_messages_disconnect
        """
        print('on_disconnect')
        print(notice)
        return

    def on_warning(self, notice):
        """Called when a disconnection warning message arrives"""
        print('on_warning')
        print(notice)
        return


if __name__ == '__main__':
    print('Starting bot....')

    with open('data.json') as data_file:
        data = json.load(data_file)

    l = StdOutListener()
    auth = OAuthHandler(data['Twitter']['consumer_key'], data['Twitter']['consumer_secret'])
    auth.set_access_token(data['Twitter']['access_token'], data['Twitter']['access_token_secret'])
    stream = Stream(auth, l)

    while True:
        try:
            stream.filter(follow=data['twitter_ids'])
        except:
            time.sleep(5)
            print('restarting')
Thank you in advance!

/lolboll
Reply
#2
Adding a reward on $25 to the one that solves this for me.
Sending thru Paypal.

/lolboll
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I scrape profile information from Twitter People search results? asdad 0 704 Nov-29-2022, 10:25 AM
Last Post: asdad
  Help discord bot Edward 0 2,059 Sep-15-2022, 09:55 PM
Last Post: Edward
  Is it possible to write a python script to block twitter feeds? cubangt 0 837 Apr-07-2022, 04:14 PM
Last Post: cubangt
  Discord Bot Dark_Sovereign_193 0 32,323 Jun-03-2021, 03:24 PM
Last Post: Dark_Sovereign_193
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 45,698 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
  Discord Bot Help Oshadha 0 1,383 Jan-26-2021, 04:23 PM
Last Post: Oshadha
  Twitter follower network gugatcgwgf 2 2,019 May-06-2020, 10:29 AM
Last Post: gugatcgwgf
  Creating Discord Bot: Unused import statement 'import discord EyeballCandy 0 3,515 Feb-02-2020, 10:53 PM
Last Post: EyeballCandy
  Callback URL for twitter app Truman 5 5,346 Dec-10-2018, 10:45 PM
Last Post: nilamo
  Twitter listen script, dynamic search value? quitte74 0 1,860 Nov-01-2018, 01:09 PM
Last Post: quitte74

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020