counterfacto

small software tool to analyze twitter and highlight counterfactual statements
git clone git://parazyd.org/counterfacto.git
Log | Files | Refs | README | LICENSE

counterfacto-web (1855B)


      1 #!/usr/bin/env python2
      2 # Copyright (c) 2017 Ivan J. <parazyd@dyne.org>
      3 
      4 import json
      5 import sys
      6 from flask import Flask, render_template, request, json
      7 from twitter import *
      8 
      9 import factolib
     10 
     11 
     12 global tweetsFile
     13 global taggedFile
     14 
     15 taggedFile = "tagged.txt"
     16 
     17 try:
     18     with open('credentials') as fd:
     19         exec(fd.read())
     20 except:
     21     print("no credentials file found. please create it")
     22     sys.exit(1)
     23 
     24 app = Flask(__name__)
     25 
     26 def writetweets(tweets, twfile):
     27     twfile = open(twfile, "w")
     28     for s in tweets:
     29         sintweet = s["text"]
     30         sintweet = sintweet.replace("\n", " ")
     31         sintweet = sintweet.encode("ascii", "ignore")
     32         twfile.write(sintweet + "\n")
     33     twfile.close()
     34 
     35 @app.route("/")
     36 def main():
     37     return render_template('index.html')
     38 
     39 @app.route("/search", methods=['POST'])
     40 def search():
     41     _name = request.form['inputName']
     42     _method = request.form['searchMethod']
     43 
     44     if not _name or not _method:
     45         return "Wrong data. Please try again."
     46 
     47     api = Twitter(auth=OAuth(oatoken,oasecret,conskey,conssecret))
     48 
     49     if _method == "account":
     50         statuses = api.statuses.user_timeline(screen_name=_name, count=100)
     51         tweetsFile = "fetchedtweets-" + _name + ".txt"
     52         writetweets(statuses, tweetsFile)
     53         factolib.classify(tweetsFile, taggedFile)
     54 
     55     elif _method == "searchterm":
     56         statuses = api.search.tweets(q=_name, count=1)
     57         tweetsFile = "fetchedsearch.txt"
     58         writetweets(statuses, tweetsFile)
     59         factolib.classify(tweetsFile, taggedFile)
     60 
     61     cfs = "counterfactuals.txt"
     62     with open(cfs) as f:
     63         return f.read()
     64 
     65 if __name__ == "__main__":
     66     try:
     67         if sys.argv[1] == '-p':
     68             _port = sys.argv2
     69     except:
     70         _port = 5000
     71 
     72     app.run(host="127.0.0.1", port=int(_port))
     73     #subprocess.call(["xdg-open", "http://127.0.0.1:" + _port])