Skip to content

Instantly share code, notes, and snippets.

Last active February 6, 2018 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyondliu/650faddf832ec8dfee3cfc3ab063aac5 to your computer and use it in GitHub Desktop.
Save beyondliu/650faddf832ec8dfee3cfc3ab063aac5 to your computer and use it in GitHub Desktop.
# coding: utf-8
import os
import urllib
import requests
import appex
import console
import keychain
import photos
def main():
if not appex.is_running_extension():
print 'This script is intended to be run from the sharing extension.'
text = appex.get_text()
images = appex.get_attachments('public.jpeg')
files = {}
if images:
f = open('fromiphone.jpg', 'wb')
statinfo = os.stat('fromiphone.jpg')
print 'file size:', statinfo.st_size
print 'There are images'
f = open('fromiphone.jpg', 'rb')
files = {'attachment': f}
if not text:
print 'No text found. Please at least enter some text!'
text = ''
print text
print '\nAdding snippet......\n'
s = requests.session()
csrftoken = s.cookies['csrftoken']
#since so far there is no way to tell if keychain had an account for a service without specifying a username, I used my account username here. You just need to change it to your username for your account on the notebook site.
passwd = keychain.get_password('notebook', 'leon')
if not passwd:
username, passwd = console.login_alert('Login your account on the notebook', 'please enter the username and password of your account', '', '', 'login')
username = 'leon'
#login the website'',data={'username':username,'password':passwd, 'next':'','csrfmiddlewaretoken':csrftoken})
#add the snippet. For simplicity, snippet is added as private and with vote 0
r =''+username+'/snippetbook/notes/addNote2/'
, data=dict(desc=text.encode('utf8'), private='on', vote=0, tags=['']), files=files)
#print 'files:',files
if files:
print(r.status_code, r.reason, r.text)
if r.status_code == 200:
console.alert('Snippet created successfully!', button1='Ok', hide_cancel_button=True)
console.alert('Error creating snippet!', button1='Ok', hide_cancel_button=True)
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment