Monday, April 6, 2015

Python Twitter Bot with Tweepy

In an earlier post I laid out a bit how I was working on a twitter bot to automate the promotion of this blog.  I'm a slacker, that has a bad rap for many folks, for me it's the highest calling I can aspire. Get the most accomplished, with the least effort, and disturbance. In the words of Bruce Lee, "Be Like Water, My Friend".  In the dao of piglet, and the te of poo, it's the mystery of being the uncarved block. In the teachings of the Kabbalah or Quabbalah, which was made into such nice easy pictograms in traditional tarot, it's learning to be representative of any, while maintaining your own sense of self.  I have one Boss, and I'm really hoping that Boss never shows up, but I always am sure to recognize There is always a bigger fish. When I crashed the party of social media a few years back, it was never intended to a social outlet. I had already experienced that in years of mmorpg's, irc, bbs, going back to wargames dialers, and phone phreaking. What I was always best at was social hacking, a rather disturbing art when taken to the level of social engineering, or mass media marketing.  After all the drugs, meditation, and study I did over the course of  a lifetime, I finally started to see a pattern, it's not even an unknown pattern, matter of fact it's all over the place, or at least portions of it are.  When I finally awoke to what I had been attempting to understand, about people, and their place in creation, I found that I had missed the relative short window of opportunity to be counted among those who's say carried any weight. In social media I saw an opportunity to leverage the flood of information as a means to get ideas running through my mind. into the hands of those who could make use of them.  I accidently crashed the party of the twitter follow gangs, that handle the bulk of the twitter advertising. It's a fairly ingenious method of leveraging the split platforms of twitter, and sms, in concert to trade influence in the twitter verse. I just saw it as an expedient means of sharing ideas to everyone as quickly as possible, regardless of how they were accepted. I even refused ever offer of easy income, not out a disdain for the finer things the world has to offer, but from an understanding of the myth of an infinite growth paradigm in a zero sum game. I'm speaking of the effects of compounding interest on privatized currency creation. The same effect is seen again and again in almost every market. It's the money itself I have issue with, not currency as an idea. The accounting for who has what, and why, is important. In our current system so many are being left behind, not out of ability, but just by becoming a party to the madness which has a seeming strangle hold on our global civilization. The worship of money, and idol, a representation of something only in potential.  As I said I'm an old school hacker, I once streamlined a system for reporting the percentage of the utilities used in manufacturing in New York state were taxable.  Was a great job while it lasted, at 18 having a work from home job for 10 or 12 an hour is amazing. Taking the time each reports takes to generate from 6 hours to 2 hours, and being told your services are no longer required, not so amazing. Being allowed to help a crop of Police Officers pass a class they had no reason taking, Amazing!. Having your professor tell you there is no place in the world for programming games, again not so amazing. I stopped using computers, and started playing games with them, and then started fixing them.  In the course of my several years of tech service for a local community, what I found was they didn't actually want for me to really solve the issues, they wanted me to manage the problems that cropped up with using outdated, or mixed era systems. Sometimes they almost wanted me to take over the day to day operations of their endeavors. All well and good if it is a mentor situation, when it's a lack of desire to stay current enough to manage your own affairs, well if your not the adult, there aren't many roles left.  After exhausting some of the more entertainment oriented uses for the raspberry pi b+ I turned to what else could this little marvel do. I started seeing vpn's, and pirate drop boxes, wifi extenders to create public clouds all over. All cool concepts, but not much use to me personally, given my current circumstance. Realizing I needed to have a bit of programing brush up, I think the last time I wrote code before this was 88 or so. I hit up code academy, and picked python as it is a seemingly popular high lvl language with many popular api's. I also did some html work, I get it, and it's powerful, it's just puts me to sleep doing it. Even in python as entertaining as it was to create the bot as far as I've taken it, and I am well aware how limited it is in it's current state.  It's basic, but it will tweet for you, pick from search terms to promo others, so your account generates more attention from the community, and will auto follow new folks, and unfollow those that are just attempting to boost numbers. Once I got there essentially I had a automatic twitter promotional bot. Did you know people get paid for doing promotional work on twitter? Even though it makes sense from the marketing game standpoint, that is a slippery area, we are talking about folks that have no moral issue with using psych jobs on children to profit.
So today I'm going post the code for my bot friend. working title was twitternanny, but for easy of use I have taken to naming them after the account they manage. I currently have 2 instances running on my raspberry pi. They may come down at the end of the month, as I have already given notice, my leap of faith I guess, with no place on horizon as of yet. I guess I could always battery power it, and stick in an open wifi cloud in a tree or something, but what would be the point. I ended up using twitter as an object lesson of the insanity of exclusivity.
Enough of my ramble, I get sick of myself sometimes.
here is the code, use it abuse it.



def hourlytweet():
    filename=open(argfile,'r')
    f=filename.readlines()
    filename.close()
    api.update_status(status=f)
    print("hourly tweet posted")


def follow_unfollow():
    followers = api.followers_ids()
    friends = api.friends_ids()

    for z in followers:
        if z not in friends:
            api.create_friendship(z)

    for z in friends:
        if z not in followers:
            api.destroy_friendship(z)
    print("follow, unfollow done")


def search_retweet():
        search_terms= ['python', 'technology', 'raspberry pi', 'ingress', 'news', 'breaking']
        g = random.choice(search_terms)
        g = '#' + g
        results = api.search(q=g, count=1)
        for u in results:
            if u not in alreadytweeted:
                api.retweet(u.id)
                print("retweeted top result for", g)
                alreadytweeted.append(u)



x=1
y=2

while x<y:
    localtime = time.asctime(time.localtime(time.time()))
    hour = int(localtime[11] + localtime[12])
    print('current hour is:',hour)
    if hour>5 and hour<24:
        try:
            hourlytweet()
            time.sleep(1700+random.randint(200,400))
        except:
            print("could not post hourly tweet")
            time.sleep(1700+random.randint(200,400))
        try:
            search_retweet()
            follow_unfollow()
        except:
            print("could not post retweet")
    elif hour == 24 or hour < 6:
        try:
            search_retweet()
            follow_unfollow()
        except:
            pass
    time.sleep(1700+random.randint(200,400))

some of the basic framework came form http://www.dototot.com/how-to-write-a-twitter-bot-with-python-and-tweepy/
So big ups and respect, thanks for the head start, and to those I didn't mention, sorry I use many reference sources when I start a little project.

Thanks
Jack
aka
PanseyBard Digital PiedPiper

No comments:

Post a Comment