Last active
February 6, 2018 23:03
-
-
Save beyondliu/650faddf832ec8dfee3cfc3ab063aac5 to your computer and use it in GitHub Desktop.
add_snippet.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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.' | |
return | |
text = appex.get_text() | |
images = appex.get_attachments('public.jpeg') | |
files = {} | |
if images: | |
f = open('fromiphone.jpg', 'wb') | |
f.write(images[0]) | |
f.close() | |
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!' | |
#return | |
text = '' | |
print text | |
print '\nAdding snippet......\n' | |
s = requests.session() | |
s.get('http://3exps.org/login/') | |
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') | |
keychain.set_password('notebook',username,passwd) | |
else: | |
username = 'leon' | |
#login the website | |
s.post('http://3exps.org/loginUser/',data={'username':username,'password':passwd, 'next':'','csrfmiddlewaretoken':csrftoken}) | |
#add the snippet. For simplicity, snippet is added as private and with vote 0 | |
r = s.post('http://3exps.org/'+username+'/snippetbook/notes/addNote2/' | |
, data=dict(desc=text.encode('utf8'), private='on', vote=0, tags=['']), files=files) | |
#print 'files:',files | |
if files: | |
f.close() | |
print(r.status_code, r.reason, r.text) | |
if r.status_code == 200: | |
console.alert('Snippet created successfully!', button1='Ok', hide_cancel_button=True) | |
else: | |
console.alert('Error creating snippet!', button1='Ok', hide_cancel_button=True) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment