Forum Archive

Posting to wordpress from safari with clipboard

TomKellytak54

Using poster. Safari won't pass the selection so i go through the clipboard. Pythonista combines the clipboard and the URL info into a nice post.

TomKellytak54

The javascript is standard, sending the URL and title to pythonista, and asking pythonista to forward them to Poster.

javascript:window.location='pythonista://clip2poster?action=run&argv='+encodeURIComponent(document.title)+'&argv='+encodeURIComponent(location.href);
TomKellytak54

Here is the pythonista code

# -*- coding: utf-8 -*-
# url title and clipboard to poster
# clip2poster
# includes code from f viticci and others

print('begin')
import webbrowser
import urllib
import clipboard
import urllib2
import sys
import re
import bs4
import console
import markdown
import string
#reduce load time by eliminating some imports
print('begin2')

numArgs = len(sys.argv)

console.clear()
#       for testing
if numArgs < 2:
    webpage = 'http://google.com'
    webpage = webpage.encode('utf-8') 
    newurl = webpage
    name = webpage
    title = webpage
    myurl = webpage
#i don't use this feature above.

#console.show_activity()

#    soup = bs4.BeautifulSoup(urllib.urlopen(newurl))
#    title = soup.title.string
#    name = title.encode('utf-8')

#console.hide_activity()

else:
    webpage = sys.argv[1]
    myurl = sys.argv[2]

name = webpage

#general steps to sanitize the file name by removing colons, etc.

name = webpage.encode('utf-8') 
safechars = '_-() ' + string.digits + string.ascii_letters
allchars = string.maketrans('', '')
deletions = ''.join(set(allchars) - set(safechars))
#filename = '#abc.$%.txt'
name = string.translate(name, allchars, deletions)
print(name)
# this didn't work for me:'
#s = "John Smith's Cool Page";
#filename = s.replace(/[^a-z0-9]/gi, '_').toLowerCase();

print '\nGenerating url and title..'
print('\n\n title= ' + webpage)
print('\n\n myurl= ' + myurl)


mdlink = '[' + webpage +'](' +myurl +')'
#htmlurl = markdown.markdown(mdlink) + '\n'

# to use if and when poster accepts html
#mdlink = '(' + webpage +')[' +myurl +']'
#clip = mdlink + clip
# for use if and when poster accepts markdown.
clip = clipboard.get()

clip = mdlink + '\n\n' + clip
clip = 'Clipped from link: ' + myurl + '\n\n' + clip + '...'
text = clip.encode('utf-8')
text = urllib.quote(text, safe='')

safari = myurl
poster = "posterapp://create?"
actions = '&callback_url=' + safari
mytitle =  name # or webpage
mytitle = urllib.quote(mytitle, safe='')
print('\n\n poster= ' + poster)
print('\n\n text= ' + text)
print('\n\n actions= ' + actions)
print('\n\n mytitle= ' + mytitle)
webbrowser.open(poster + '&title=' + mytitle + '&text=' + text + actions)
frankvanexter152

Works great. Thanks for sharing it.