Last Updated: 18-July-2017

I don't tend to share that much on Twitter, partly due to not being bothered and partly because I couldn't be bothered with optimising the tweets; pulling in headings, adding #hastags, etc.

We'll today, on my lunch, I knocked a little JS bookmarklet together to do both title and hash-tagging for me. FYI, this follows setting a little challange for myself to gain more followers, starting with 1,000.

So, what does my little button do:

If you've just finished a blog post, you can click the button and the URL is sent to my server, from here the page is requested and the page title is parsed. Once the title is parsed, it's checked for common, marketing related, hash-tags.

This site is built on Flask, so while this example is in Flask, it's also a nice easy way to visualise what happens.

First, here's a little gif of it in action:

The bookmarket looks like this (HIGHLIGHT THIS AND DRAG IT INTO YOUR BOOKMARKS BAR):

javascript: ( function() {  window.open("http://www.digipy.co.uk/twitter-parse?url="+location.href);}() )

If you're a marketer, feel free to use this :) 

My URL route in Flask, which contains the main logic, looks like this:

@app.route('/twitter-parse')

def twitter_parser():

    try:

        hashtags = ['seo', 'ppc'...<snipped>...]

        url = request.args.get('url', None)

        title = None

        if url:

            r=requests.get(url.strip())

            data = r.text

            soup = BeautifulSoup(data, 'html5lib')

            title = soup.title.text.encode('utf-8', 'ignore')

            for hashtag in hashtags:

                if hashtag in title.split(' '):

                    title = title.replace('#{}'.format(hashtag), hashtag.strip()).replace(hashtag.strip(), '#{}'.format(hashtag))

                else:

                    pass

        else:

            return 'error'

    except Exception as e:

        return '{}'.format(e)

    params = {'text':title, 'url': url}

    return redirect('https://twitter.com/intent/tweet?{}'.format(urllib.urlencode(params)))

 

And that's it. The code could probably do with a little refining but it was just a quick hack.

If you want to use this, feel free, just add the bookmarklet code as a bookmark.

About the author

Image

Craig Addyman @craigaddyman
Head of Digital Marketing. Python Coder.