Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bottle import route, request, debug, run, template, redirect
- import urllib2
- import twurl
- import json
- import sqlite3
- import pickle
- session={'logins':0, 'user': ""}
- shared_archives = {}
- def checkLogin(name,password):
- if name==password:
- return True
- else:
- return False
- @route('/login')
- def login_form():
- global session
- if session['user'] != "":
- return template('loggedIn2.tpl',message='Successfully logged in!',success=True,logins=session['logins'], name=session['user'])
- else:
- return template ('login.tpl')
- @route('/logout')
- def logout():
- session['user'] = ""
- return template ('login.tpl')
- @route('/checkLogin', method='post')
- def login_submit():
- global session
- name = request.forms.get('name')
- password = request.forms.get('password')
- if checkLogin(name, password):
- session['logins']=session['logins']+1
- session['user'] = name
- #return template('loggedIn2.tpl',message='Successfully logged in!',success=True,logins=session['logins'], name=name)
- return redirect('/')
- else:
- return template('loggedIn2.tpl',message='Unlucky! Try again.',success=False)
- def makeMenu():
- menu = "<a class='menuItem' href='/'>Home</a><br>"
- menu += "<a class='menuItem' href='/login'>Login</a><br>"
- connect = sqlite3.connect('twitterDB.db')
- cursor = connect.cursor()
- cursor.execute("select id, name from archives order by name asc")
- result = cursor.fetchall()
- cursor.close()
- connect.close()
- for archive in result:
- menu += "<a class='menuItem' href='/showArchive/" + str(archive[0]) + "'>" + archive[1] + "</a><br>"
- menu += '''<form method='post' action='/addArchive'>
- <input type='text' name='newArchive' size='15'><br>
- <input type='submit' name='submit' value='New Archive'>
- </form>'''
- return menu
- def getArchiveName():
- connect = sqlite3.connect('twitterDB.db')
- cursor = connect.cursor()
- cursor.execute("select name from archives where id=?", (session['archiveID'],))
- result = cursor.fetchone()
- cursor.close()
- connect.close()
- html = "Archive: " + str(result[0])
- html += "<br /><br /><font size='4px'>Shared with:"
- archiveName = str(result[0])
- if shared_archives.has_key(archiveName):
- for user in shared_archives[archiveName]:
- html += user + "<br />"
- else:
- html += "<br />No one"
- html += "<form action='/shareArchive/" + archiveName + "'>"
- html += """<input type="text" name="user" placeholder="User to share with..." />
- <input type="submit" value="Share" />
- </form>
- """
- return html
- @route('/shareArchive/<name>')
- def shareArchive(name):
- user = request.GET.get('user')
- if shared_archives.has_key(name):
- shared_archives[name].append(user)
- else:
- shared_archives[name] = list([user])
- return redirect('/')
- def callAPI(twitter_url, parameters):
- url=twurl.augment(twitter_url, parameters)
- connection = urllib2.urlopen(url)
- return connection.read()
- def makeArchiveDropdown():
- connect = sqlite3.connect('twitterDB.db')
- cursor = connect.cursor()
- cursor.execute("select id, name from archives order by name asc")
- result = cursor.fetchall()
- cursor.close()
- connect.close()
- html = "<select name='archiveID' onchange='form.submit()'>"
- html += "<option>Save to...</option>"
- for archive in result:
- html += "<option value='" + str(archive[0]) + "'>" + archive[1] + "</option>"
- html += "</select>"
- return html
- def replaceEntities(item, tweetHTML):
- hashtags = set([hashtag['text'] for hashtag in item['entities']['hashtags']])
- mentions = set([mention['screen_name'] for mention in item['entities']['user_mentions']])
- for hashtag in hashtags:
- find = '#{0}'.format(hashtag)
- replace = '<a href="http://www.twitter.com/hashtag/{0}">#{0}</a>'.format(hashtag)
- tweetHTML = tweetHTML.replace(find, replace)
- for mention in mentions:
- find = '@{0}'.format(mention)
- replace = '<a href="http://www.twitter.com/{0}">@{0}</a>'.format(mention)
- tweetHTML = tweetHTML.replace(find, replace)
- return tweetHTML
- def makeTweet(item, mode, archiveList):
- html, tweetHTML, links = '', item['text'], ''
- if mode=='myTimeline':
- links = "<form name='archive' method='post' action='/archive'>"
- links += "<input type='hidden' name='tweetID' value='" + str(item['id']) + "'>"
- links += archiveList
- links += "</form>"
- elif mode=='archive':
- links="<a href='/moveUp/" + str(item['id']) + "'>Up</a><br>" + \
- "<a href='/moveDown/" + str(item['id']) + "'>Down</a><br>" +\
- "<a href='/deleteTweet/" + str(item['id']) + "'>Delete</a>"
- html += "<table><tr valign='top'><td width='70'>"
- html += "<img src='" + item['user']['profile_image_url'] + "'></td>"
- html += "<td><a href='/user/" + item['user']['screen_name'] + "'>@" + item['user']['screen_name'] + "</a>"
- html += " (" + item['user']['name'] + ")"
- html += "</td></tr>"
- html += "<tr><td>" + links + "</td><td>" + tweetHTML + "<br>"
- html += "</td></tr></table><hr>"
- return html
- def showMyTimeline():
- twitter_url='https://api.twitter.com/1.1/statuses/home_timeline.json'
- parameters = {'count':5}
- data = callAPI(twitter_url, parameters)
- js = json.loads(data)
- html, archives = '', makeArchiveDropdown()
- for item in js:
- html = html + makeTweet(item, 'myTimeline', archives)
- return html
- def getTweet(id):
- twitter_url='https://api.twitter.com/1.1/statuses/show.json'
- parameters = {'id':id}
- data = callAPI(twitter_url, parameters)
- return json.loads(data)
- def showStoredTweets(archiveID):
- connect = sqlite3.connect('twitterDB.db')
- cursor = connect.cursor()
- cursor.execute("select tweet from tweets where archiveID=? order by position asc", (archiveID,))
- result = cursor.fetchall()
- cursor.close()
- connect.close()
- html = ''
- for tweet in result:
- html = html + makeTweet(pickle.loads(tweet[0]), 'archive', '')
- return html
- @route('/archive', method='post')
- def archiveTweet():
- global session
- archiveID = request.POST.get('archiveID', '').strip()
- tweetID = request.POST.get('tweetID', '').strip()
- pickledTweet = pickle.dumps(getTweet(tweetID))
- connect = sqlite3.connect('twitterDB.db')
- cursor = connect.cursor()
- cursor.execute("SELECT position from tweets where archiveID=? ORDER BY position DESC LIMIT 1", (session['archiveID'],))
- dbRow = cursor.fetchone()
- if dbRow is not None:
- nextPosition = int(dbRow[0])+1
- else:
- nextPosition = 1
- cursor.execute("INSERT INTO tweets (tweetID, tweet, archiveID, position) VALUES (?,?,?,?)", \
- (tweetID, sqlite3.Binary(pickledTweet), archiveID, nextPosition))
- connect.commit()
- cursor.close()
- connect.close()
- session['archiveID'] = archiveID
- html = showStoredTweets(archiveID)
- return template('showTweets.tpl', heading=getArchiveName(), menu=makeMenu(), html=html)
- @route('/deleteTweet/<id>')
- def deleteTweet(id):
- global session
- connect = sqlite3.connect('twitterDB.db')
- cursor = connect.cursor()
- cursor.execute("DELETE from tweets WHERE tweetID=? and archiveID=?", (id,session['archiveID']))
- connect.commit()
- cursor.close()
- connect.close()
- html = showStoredTweets(session['archiveID'])
- return template('showTweets.tpl', heading=getArchiveName(), menu=makeMenu(), html=html)
- @route('/moveUp/<id>')
- def moveUp(id):
- global session
- connect = sqlite3.connect('twitterDB.db')
- cursor = connect.cursor()
- print "Archive is " + str(session['archiveID'])
- cursor.execute("SELECT position FROM tweets WHERE tweetID=? and archiveID=?", (id,session['archiveID']))
- position = cursor.fetchone()[0]
- cursor.execute("SELECT tweetID, position FROM tweets WHERE position<? and archiveID=? order by position desc limit 1", (position,session['archiveID']))
- dbRow = cursor.fetchone()
- if dbRow is not None:
- otherID, otherPosition = dbRow[0], dbRow[1]
- cursor.execute("UPDATE tweets set position=? WHERE tweetID=? and archiveID=? ", (otherPosition,id,session['archiveID']))
- cursor.execute("UPDATE tweets set position=? WHERE tweetID=? and archiveID=?", (position,otherID,session['archiveID']))
- connect.commit()
- cursor.close()
- connect.close()
- html = showStoredTweets(session['archiveID'])
- return template('showTweets.tpl', heading=getArchiveName(), menu=makeMenu(), html=html)
- @route('/moveDown/<id>')
- def moveDown(id):
- html = showStoredTweets(session['archiveID'])
- return template('showTweets.tpl', heading=getArchiveName(), menu=makeMenu(), html=html)
- @route('/showArchive/<id>')
- def showArchive(id):
- global session
- html = showStoredTweets(id)
- session['archiveID'] = id
- return template('showTweets.tpl', heading=getArchiveName(), menu=makeMenu(), html=html)
- @route('/addArchive', method='post')
- def addArchive():
- global session
- newArchive = request.POST.get('newArchive', '').strip()
- connect = sqlite3.connect('twitterDB.db')
- cursor = connect.cursor()
- cursor.execute("INSERT into archives (name) VALUES (?)", (newArchive,))
- connect.commit()
- cursor.close()
- connect.close()
- html = showMyTimeline()
- return template('showTweets.tpl', heading="My timeline", menu=makeMenu(), html=html)
- @route('/')
- def index():
- html = showMyTimeline()
- return template('showTweets.tpl', heading="My timeline", menu=makeMenu(), html=html)
- def replaceEntities(item, tweetHTML):
- hashtags = set([hashtag['text'] for hashtag in item['entities']['hashtags']])
- mentions = set([mention['screen_name'] for mention in item['entities']['user_mentions']])
- for hashtag in hashtags:
- find = '#{0}'.format(hashtag)
- replace = '<a href="http://www.twitter.com/hashtag/{0}">#{0}</a>'.format(hashtag)
- tweetHTML = tweetHTML.replace(find, replace)
- for mention in mentions:
- find = '@{0}'.format(mention)
- replace = '<a href="http://www.twitter.com/{0}">@{0}</a>'.format(mention)
- tweetHTML = tweetHTML.replace(find, replace)
- return tweetHTMLo
- debug(True)
- run(reloader=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement