Last active
December 10, 2015 15:19
-
-
Save nitinthewiz/4453913 to your computer and use it in GitHub Desktop.
A Pythonista script to pull the first 50 items from your First Kindling Group from Fever RSS and display them in a nice format. Also allows for importing to Instapaper app, if you've got it. Original code courtesy Thomas Allen (look for the FeverBuddy git repo). Please note, there is a lot of redundant code here. Please don't worry. Pythonista d…
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
FeverBuddy.py | |
Created by Thomas Allen on 2010-06-04. | |
Copyright (c) 2010 __telega.org__. All rights reserved. | |
FeverBuddy for Pythonista | |
Modified by Nitin Khanna on 2013-01-04 | |
No Copyrights. This is Github. I'll be sad if you don't steal this script. | |
""" | |
import sys | |
import os | |
import getopt | |
import json | |
import hashlib | |
import urllib | |
import time | |
import webbrowser | |
import SimpleHTTPServer | |
import SocketServer | |
class FeverBuddy(Exception): | |
def __init__(self, msg): | |
self.msg = msg | |
def setEndpoint(self,endpoint): | |
#sets the endpoint of the fever installation ie. base directory | |
self.endpoint = "http://"+endpoint+"/?api" | |
#print "end point set to "+self.endpoint | |
def getEndpoint(self): | |
return self.endpoint | |
def setEmail(self,email): | |
self.email = email | |
#print "Email set to "+self.email | |
def getEmail(self): | |
return self.email | |
def setPassword(self,password): | |
self.password = password | |
#print "password set to "+self.password | |
def getPassword(self): | |
return self.password | |
def setApiKey(self): | |
#creates the md5 hash from the email and password for POSTing to fever. Requried for Authentication. | |
self.apikey = {"api_key":hashlib.md5(str(self.getEmail())+':'+str(self.getPassword())).hexdigest()} | |
def getApiKey(self): | |
return self.apikey | |
def setParams(self,*params): | |
self.params = "" | |
for elem in params: | |
self.params = self.params + str(elem) | |
#print self.params | |
def getParams(self): | |
return self.params | |
def setDays(self,days): | |
self.days = days | |
def getDays(self): | |
return self.days | |
def setUrl(self): | |
self.url = str(self.getEndpoint())+str(self.getParams()) | |
def getUrl(self): | |
return self.url | |
def getJson(self): | |
self.feverJson = json.load(urllib.urlopen(self.getUrl(),urllib.urlencode(self.getApiKey()))) | |
#print self.feverJson | |
return self.feverJson | |
def getHotItems(self): | |
"get Hot Items from fever, returns a list" | |
storyque = [] | |
myJson = self.getJson() | |
print myJson | |
x = 0 | |
for links in myJson['links']: | |
temperature = str(round(myJson['links'][x]['temperature'],1)) | |
#print temperature | |
ct = storyque.count(temperature) | |
#print ct | |
if ct > 0: | |
storyque.insert((storyque.index(temperature)+2),myJson['links'][x]['title']) #this is the line that inserts Title for items, need to include URL here | |
elif ct == 0: | |
storyque.append(temperature) | |
storyque.append('-----') | |
storyque.append(myJson['links'][x]['title']) #this is the line that inserts Title for items, need to include URL here | |
x=x+1 | |
return storyque | |
def printHotItems(self,storyque=[]): | |
"should be called in conjunction with getHotItems()" | |
for item in storyque: | |
print item.encode('utf8') | |
def getOneGroup(self): | |
"get Group One Items from fever" | |
myJson = self.getJson() | |
#print myJson | |
f=open('workfile.txt', 'w+') | |
json.dump(myJson, f) | |
indexfile = open('index.html', 'w+') | |
print >>indexfile, """<html><head><style>h1 {font-size:1em;} img {float:right;}</style><link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet"><script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Group1</title></head><body><table border="1">""" | |
for item in myJson['items']: | |
print >>indexfile, """<tr><td><h1><a href=\"http://www.instapaper.com/text?u=%s\">%s</a><a href=\"i%s\"><img src='http://cdn.alternativeto.net/i/51bdf128-5bd4-e111-9bfd-0025902c7e73_19042.png'></a></h1></td></tr>""" % (item['url'].encode('utf8'), item['title'].encode('utf8'), item['url'].encode('utf8')) | |
print >>indexfile, """</table></body></html>""" | |
#Handler = SimpleHTTPServer.SimpleHTTPRequestHandler | |
#httpd = SocketServer.TCPServer(("",8000), Handler) | |
#httpd.serve_forever() | |
#print os.path.abspath('index.html') | |
webbrowser.open('file:///'+os.path.abspath('index.html')) | |
def getGroups(self): | |
"get Hot Items from fever, returns a list" | |
storyque = [] | |
myJson = self.getJson() | |
print myJson | |
for group in myJson['groups']: | |
storyque.insert(group['id'],group['title']) | |
# Insert a row of data | |
return storyque | |
def printGroups(self,storyque=[]): | |
"should be called in conjunction with getHotItems()" | |
for item in storyque: | |
print str(storyque.index(item))+": "+item.encode('utf8') | |
def getAllItems(self): | |
self.setParams("&items&max_id=0") | |
self.setUrl() | |
myJson = self.getJson() | |
lastMaxId = 0 | |
print myJson | |
for item in myJson['items']: | |
lastMaxId = item['id'] | |
while lastMaxId != 0: | |
self.setParams("&items&max_id="+str(lastMaxId)) | |
self.setUrl() | |
myJson = self.getJson() | |
for item in myJson['items']: | |
lastMaxId = item['id'] | |
def getLastRefresh(self): | |
myJson = self.getJson() | |
#time of last refresh | |
rtime = time.localtime(int(myJson["last_refreshed_on_time"])) | |
#current time | |
ntime = time.localtime() | |
#figure out roughly how many minutes ago | |
self.lastRefresh = ((ntime[0]-rtime[0])*525948) + ((ntime[3]-rtime[3])*60) +(ntime[4]-rtime[4]) | |
return self.lastRefresh | |
def printLastRefresh(self): | |
print "Last Refresh: %d minutes ago" %self.getLastRefresh() | |
def main(): | |
#default values for some options | |
verbose = False | |
hotitems = False | |
lastrefresh = False | |
groups = False | |
group1 = False | |
#create a fb item | |
fb = FeverBuddy("FeverBuddy") | |
try: | |
#Edit the Endpoint, email and password | |
group1 = True | |
fb.setEndpoint('http://YOURFEVERINSTALL') | |
fb.setEmail('[email protected]') | |
fb.setPassword('PASSWORDSrUS') | |
#take action | |
fb.setApiKey() | |
if hotitems == True: | |
fb.setParams("&links&offset=0&range=",fb.getDays(),"&page=1") | |
fb.setUrl() | |
fb.printHotItems(fb.getHotItems()) | |
elif lastrefresh == True: | |
fb.setParams("") | |
fb.setUrl() | |
fb.printLastRefresh() | |
elif groups == True: | |
fb.setParams("&groups") | |
fb.setUrl() | |
fb.printGroups(fb.getGroups(c)) | |
elif group1 == True: | |
#set the group ID to some other number for other Kindling Groups | |
fb.setParams("&items&group_ids=1") | |
fb.setUrl() | |
fb.getOneGroup() | |
#fb.getAllItems() | |
# We can also close the connection if we are done with it. | |
# Just be sure any changes have been committed or they will be lost. | |
#conn.close() | |
except FeverBuddy, err: | |
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) | |
print >> sys.stderr, "\t for help use --help" | |
return 2 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment