Skip to content
Permalink
Browse files

first commit

  • Loading branch information
mazieres committed Jan 29, 2014
0 parents commit 302e602efce94ad2ff9d1f8c86659fd0068aba0d
1 README
@@ -0,0 +1 @@
# PORNGRAM
@@ -0,0 +1,91 @@
package main

import "fmt"
import s "strings"
import "bytes"
import "io/ioutil"
import "encoding/json"
import "net/http"
import "log"

type qword struct {
qname string
qstats map[string]float32
}

func main() {

db, stats := make_dataset("../data/xhamster_sample.json")
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Println("Inside HelloServer handler")
query := s.Split(s.ToLower(s.Replace(req.URL.Path[1:], " ", "", -1)), ",")
chResp := make(chan qword)

for _, word := range query {
a_word := qword{qname: word}
go query_word(a_word, &db, &stats, chResp)
}

res := make(map[string]map[string]float32)
for i := len(query); i > 0; i-- {
tmp := <- chResp
res[tmp.qname] = tmp.qstats
}
js, _ := json.Marshal(res)
fmt.Fprint(w, string(js))
})
err := http.ListenAndServe("localhost:8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err.Error())
}

}

func query_word(w qword, db *map[string]*bytes.Buffer, stats *map[string]int, chResp chan qword) {

count := make(map[string]int)
res := make(map[string]float32)

for year, txt := range *db {
count[year] = s.Count(txt.String(), w.qname)
}

for year, total := range *stats {
res[year] = (float32(count[year]) / float32(total)) * 100
}

w.qstats = res

chResp <- w
}

func make_dataset(path string) (map[string]*bytes.Buffer, map[string]int) {

dat, _ := ioutil.ReadFile(path)
var jdat map[string]interface{}
json.Unmarshal(dat, &jdat)

res := make(map[string]*bytes.Buffer)
stats := make(map[string]int)

for _, v := range jdat {

content := v.(map[string]interface{})
date := s.Split(content["upload_date"].(string), "-")[0]
valiDate := date != "NA" && date != "2007" && date != "2013"

if valiDate {
_, prs := res[date]

if prs{
res[date].WriteString(content["title"].(string))
stats[date] += 1

} else {
res[date] = bytes.NewBufferString(content["title"].(string))
stats[date] = 1
}
}
}
return res, stats
}

Large diffs are not rendered by default.

@@ -0,0 +1,52 @@
import json
import sys
from flask import Flask, url_for, render_template, request
import requests
from random import choice
import string

app = Flask(__name__)

examples = [
'milf,teen',
'hardcore,love',
'mother,sister,brother,father',
'secretary,boss',
'blowjob,handjob,titjob,footjob'
]

def format_query(query):
s = ''
for w in query.split(',')[:10]:
if len(w) < 30:
for ch in w:
if ch in string.letters + string.digits:
s += ch
s += ','
return s.strip(',')

def get_data(query):
r = requests.get("http://localhost:8080/%s" % query)
d = json.loads(r.text)
res = [['Year'],['2008'],['2009'],['2010'],['2011'],['2012']]
for k, v in d.iteritems():
res[0].append(str(k))
res[1].append(v['2008'])
res[2].append(v['2009'])
res[3].append(v['2010'])
res[4].append(v['2011'])
res[5].append(v['2012'])
return str(res)

@app.route('/')
def index():
try:
q = format_query(request.args['q'])
except:
q = choice(examples)

res = get_data(q)
return render_template('index', q=q, res=res)

if __name__ == "__main__":
app.run(host="0.0.0.0", port=8081, debug=False)

0 comments on commit 302e602

Please sign in to comment.
You can’t perform that action at this time.