Last active
October 28, 2021 13:47
-
-
Save yohhoy/0cd157076085a79fd5ed to your computer and use it in GitHub Desktop.
Nickname generator for TAKUMI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Author
yohhoy
commented
Oct 28, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment