Created
January 12, 2010 23:21
-
-
Save atifaziz/275740 to your computer and use it in GitHub Desktop.
IronPython client to save IE sessions to RIL
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
# Read It Later IE | |
# Utility to post IE sessions to http://readitlaterlist.com | |
# | |
# The MIT License | |
# | |
# Copyright (c) 2010, Atif Aziz | |
# | |
# Author(s): | |
# | |
# Atif Aziz, http://www.raboof.com/ | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
import sys, clr | |
from System import Type, Activator, StringComparison | |
from System.Collections.Specialized import NameValueCollection | |
from System.Net import WebClient | |
from System.Text import Encoding | |
from System.Text.RegularExpressions import Regex | |
clr.AddReference('System.Web.Extensions') | |
from System.Web.Script.Serialization import JavaScriptSerializer as JsonSerializer | |
def ie_docs(): | |
shell_type = Type.GetTypeFromProgID('Shell.Application') | |
shell = Activator.CreateInstance(shell_type) | |
result = [] | |
wins = shell.Windows() | |
for i in range(wins.Count()): | |
title = None | |
try: | |
document = wins.Item(i).Document() | |
title = document.title | |
except: | |
pass | |
if title is None: | |
continue | |
if not type(title) is str: | |
continue | |
href = document.location.href | |
# TODO: exclude local addresses | |
if not Regex.IsMatch(href, r'^(http|https)\:\/\/\S+'): | |
continue | |
result.append((href, title)) | |
return result | |
def main(args): | |
if len(args) < 3: | |
from os import getenv | |
uid, pwd, apikey = getenv('RIL_UID'), getenv('RIL_PWD'), getenv('RIL_KEY') | |
else: | |
uid, pwd, apikey = args[:3] | |
if not uid or not pwd or not apikey: | |
print 'Usage: %s USERNAME PASSWORD APIKEY' % sys.argv[0] | |
sys.exit(2) | |
docs = ie_docs() | |
for url, title in docs: | |
print title | |
print url | |
bookmarks = [{'url': url, 'title': title} for url, title in docs] | |
form = NameValueCollection() | |
form.Add('username', uid) | |
form.Add('password', pwd) | |
form.Add('apikey', apikey) | |
new_json = JsonSerializer().Serialize(dict([(str(i), bkmk) for i, bkmk in zip(range(len(bookmarks)), bookmarks)])) | |
form.Add('new', new_json) | |
wc = WebClient() | |
reply = wc.UploadValues('https://readitlaterlist.com/v2/send', 'POST', form) | |
print Encoding.UTF8.GetString(reply) | |
print '\n'.join(['%8s = %s' % (int(wc.ResponseHeaders[k]).ToString('N0'), k[8:]) for k in wc.ResponseHeaders if k.StartsWith('X-Limit-', StringComparison.OrdinalIgnoreCase)]) | |
if __name__ == '__main__': | |
try: | |
main(sys.argv[1:]) | |
except Exception, e: | |
print >> sys.stderr, e | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment