from flask import request, render_template
from random import randint
from urllib2 import urlopen
import json
from db import conn
import re
cur = conn.cursor()

def train():
    if request.method=="POST":
        form = request.form
        constructive = form["constructive"]
        revid = form["diffid"]
        rev = urlopen("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=json&rvdiffto=prev&revids=%s" % revid).read()
        rev = json.loads(rev)
        pages = rev["query"]["pages"]
        diff = pages.values()[0]["revisions"][0]["diff"]["*"]
        addedlines = " ".join(re.findall(r'diff-addedline"><div>[.\n\r]*diffchange-inline">([.\n\r]*?)</span>', diff))
        deledlines = " ".join(re.findall(r'diff-deletedline"><div>[.\n\r]*diffchange-inline">([.\n\r]*?)</span>', diff))
        print addedlines
        print deledlines
        cur.execute("INSERT INTO training_diffs (added, deled, is_good) VALUES (%s, %s, %s)", (addedlines, deledlines, constructive))
        conn.commit()

    revid = randint(57000000, 58500000)
    c = urlopen("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=json&rvdiffto=prev&revids=%s" % revid).read()
    c = json.loads(c)
    if "pages" not in c["query"]:
        return "omg pls exist<br/><a href=\"/train/\">pls refresh</a>"
    pages = c["query"]["pages"]
    diff = pages.values()[0]["revisions"][0]["diff"]["*"]
    comment = pages.values()[0]["revisions"][0]["comment"]
    print comment
    return render_template("train.html", diff=diff, diffid=revid, editsummary=comment)

@@ -133,16 +133,33 @@
 port re%0A
+import lxml.html%0A
 cur = co
@@ -598,238 +598,847 @@
     
-addedlines = %22 %22.join(re.findall(r'diff-addedline%22%3E%3Cdiv%3E%5B.%5Cn%5Cr%5D*diffchange-inline%22%3E(%5B.%5Cn%5Cr%5D*?)%3C/span%3E', diff))%0A        deledlines = %22 %22.join(re.findall(r'diff-deletedline%22%3E%3Cdiv%3E%5B.%5Cn%5Cr%5D*diffchange-inline%22%3E(%5B.%5Cn%5Cr%5D*?)%3C/span%3E', diff)
+diff = lxml.html.document_fromstring(diff)%0A        addedlines = diff.xpath(%22//td%5B@class='diff-addedline'%5D%22)%0A        deledlines = diff.xpath(%22//td%5B@class='diff-deletedline'%5D%22)%0A        addedstring = %22%22%0A        deledstring = %22%22%0A        for i in addedlines:%0A            diffchanges = i.xpath(%22./span%5B@class='diffchange diffchange-inline'%5D/text()%22)%0A            if not diffchanges:%0A                addedstring = addedstring + %22 %22 + i.text_content()%0A            else:%0A                addedstring = addedstring + %22 %22 + %22 %22.join(diffchanges)%0A        for i in deledlines:%0A            diffchanges = i.xpath(%22./span%5B@class='diffchange diffchange-inline'%5D/text()%22)%0A            if not diffchanges:%0A                deledstring = deledstring + %22 %22 + i.text_content()%0A            else:%0A                deledstring = deledstring + %22 %22 + %22 %22.join(diffchanges
 )%0A  
@@ -1454,21 +1454,22 @@
 nt added
-lines
+string
 %0A       
@@ -1480,21 +1480,22 @@
 nt deled
-lines
+string
 %0A       
@@ -1591,25 +1591,27 @@
 dded
-lines
+string
 , deled
-lines
+string
 , co

Fix fucking web interface fuck this shit