Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Last active October 28, 2021 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohhoy/0cd157076085a79fd5ed to your computer and use it in GitHub Desktop.
Save yohhoy/0cd157076085a79fd5ed to your computer and use it in GitHub Desktop.
Nickname generator for TAKUMI
//
// Nickname generator for TAKUMI
//
package main
import (
"bufio"
"fmt"
"math/rand"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
const (
url = "https://www.asahi.co.jp/beforeafter/old/takumi/index01.html"
)
func rsplit(target string, sep string) (string, string) {
elem := strings.Split(target, sep)
left := strings.Join(elem[0:len(elem)-1], sep)
return left, elem[len(elem)-1]
}
func choice(list []string) string {
return list[rand.Intn(len(list))]
}
func main() {
res, err := http.Get(url)
if err != nil {
panic(err)
}
defer res.Body.Close()
utfBody := transform.NewReader(
bufio.NewReader(res.Body), japanese.ShiftJIS.NewDecoder())
doc, err := goquery.NewDocumentFromReader(utfBody)
if err != nil {
panic(err)
}
nickPrefix := []string{}
nickSuffix := []string{}
doc.Find(".IndexNickname").Each(func(i int, sel *goquery.Selection) {
nick := strings.Split(sel.Text(), " ")[1]
p, s := rsplit(nick, "の")
nickPrefix = append(nickPrefix, p)
nickSuffix = append(nickSuffix, s)
})
rand.Seed(time.Now().UnixNano())
yourNick := choice(nickPrefix) + "の" + choice(nickSuffix)
fmt.Println(yourNick)
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Nickname generator for TAKUMI
#
import urllib.request
import lxml.html
import random
URL = 'https://www.asahi.co.jp/beforeafter/old/takumi/index01.html'
page = urllib.request.urlopen(URL).read()
parser = lxml.html.fromstring(page)
elems = parser.xpath('//div[@class="IndexNickname"]')
nick_prefix = []
nick_suffix = []
for e in elems:
nick = e.text.split()[1]
(p, s) = nick.rsplit('の', 1)
nick_prefix.append(p)
nick_suffix.append(s)
your_nick = random.choice(nick_prefix) + 'の' + random.choice(nick_suffix)
print(your_nick)
@yohhoy
Copy link
Author

yohhoy commented Oct 10, 2015

@yohhoy
Copy link
Author

yohhoy commented Jan 12, 2016

@yohhoy
Copy link
Author

yohhoy commented Feb 4, 2017

$ ./takumi-nick.py | cowthink 
 ____________________
( モダニズムの自由主義者 )
 --------------------
        o   ^__^
         o  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

@yohhoy
Copy link
Author

yohhoy commented Oct 28, 2021

$ go run takumi-nick.go | cowsay
 ____________________
< 雅空間の熱血講談師 >
 --------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment