V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zzerd
V2EX  ›  推广

自已家的赣南脐橙开卖,给 v 友抽几箱

  •  
  •   zzerd · 87 天前 · 9420 次点击
    这是一个创建于 87 天前的主题,其中的信息可能已经有所发展或是发生改变。

    抽奖规则和去年一样取上证指数和最后指定日期最高楼。上一年的帖子/996196

    今年取 2024-11-22 上证指数收盘价作为随机数种子,有效抽奖用户为 2024-11-22 帖子回复最高楼层前的所有所有人

    去年的抽奖代码没有去重所有回复多个的用户中的概率高一点点,今年我用 ai 糊了一下。代码如下

    const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36';
    
    async function handleRequest(tid, num, seedParam, maxFloor) {
        if (!tid || isNaN(num)) {
            console.error('请提供有效的帖子 ID 和抽奖人数');
            return;
        }
        console.log(`Fetching OP for TID: ${tid}`);
        const op = await getOp(tid);
        console.log(`OP: ${op}`);
        console.log(`Fetching all users for TID: ${tid}`);
        const uniqueUserList = await getAllUser(tid, maxFloor);
        console.log(`Unique Users: ${uniqueUserList.join(', ')}`);
        const filteredUserList = uniqueUserList.filter(user => user !== op);
        console.log(`Filtered Users: ${filteredUserList.join(', ')}`);
        const userCount = filteredUserList.length;
        console.log(`User Count: ${userCount}`);
        const seed = seedParam !== null ? seedParam : userCount.toString();
        console.log(`Seed: ${seed}`);
        console.log(`Max Floor: ${maxFloor}`);
        const combinedSeed = `${seed}-${maxFloor}`;
        console.log(`Combined Seed: ${combinedSeed}`);
        const shuffledList = shuffle(filteredUserList, combinedSeed);
        console.log(`Shuffled Users: ${shuffledList.join(', ')}`);
        const result = `符合条件的用户共有 ${filteredUserList.length} 人
    抽奖结果:
    ${shuffledList.slice(0, num).join('\n')}`;
        console.log(result);
    }
    
    async function getOp(tid) {
        const url = `https://www.v2ex.com/amp/t/${tid}`;
        console.log(`Fetching OP URL: ${url}`);
        const response = await fetch(url, { headers: { 'User-Agent': UA } });
        if (!response.ok) {
            console.log(`Failed to fetch OP: ${response.status} ${response.statusText}`);
            return null;
        }
        const text = await response.text();
        const match = /<div class="topic_author">[\s\S]*?<amp-img[^>]+alt="([^"]+)"[\s\S]*?<\/div>/.exec(text);
        console.log(`OP Match: ${match ? match[1] : null}`);
        return match ? match[1].trim() : null;
    }
    
    async function getUserList(url, startIndex, endIndex) {
        console.log(`Fetching User List URL: ${url}`);
        const response = await fetch(url, { headers: { 'User-Agent': UA } });
        if (!response.ok) {
            console.log(`Failed to fetch User List: ${response.status} ${response.statusText}`);
            return [];
        }
        const text = await response.text();
        const replyItemRegex = /<div class="reply_item">([\s\S]*?)<\/div>/g;
        let replyItemMatch;
        const users = [];
        let currentIndex = startIndex || 0;
        while ((replyItemMatch = replyItemRegex.exec(text)) !== null) {
            if (endIndex !== undefined && currentIndex >= endIndex) {
                break;
            }
            const replyItem = replyItemMatch[1];
            const altRegex = /<amp-img[^>]+alt="([^"]+)"[^>]*>/g;
            const altMatch = altRegex.exec(replyItem);
            if (altMatch) {
                users.push(altMatch[1]);
            }
            currentIndex++;
        }
        console.log(`Matches: ${users.join(', ')}`);
        return users;
    }
    
    async function getAllPages(tid) {
        const url = `https://www.v2ex.com/amp/t/${tid}`;
        console.log(`Fetching All Pages URL: ${url}`);
        const response = await fetch(url, { headers: { 'User-Agent': UA } });
        if (!response.ok) {
            console.log(`Failed to fetch All Pages: ${response.status} ${response.statusText}`);
            return [];
        }
        const text = await response.text();
        const pageCountMatch = /\u5171 (\d+) \u9875/.exec(text);
        const pageCount = pageCountMatch ? parseInt(pageCountMatch[1], 10) : 1;
        console.log(`Page Count: ${pageCount}`);
        const pages = [];
        for (let i = 1; i <= pageCount; i++) {
            pages.push(`${url}/${i}`);
        }
        return pages;
    }
    
    async function getAllUser(tid, maxFloor) {
        const pages = await getAllPages(tid);
        console.log(`Pages: ${pages.join(', ')}`);
        let allUsers = [];
        let currentFloor = 0;
        for (let page of pages) {
            const startIndex = currentFloor;
            const endIndex = maxFloor !== undefined ? maxFloor : undefined;
            const users = await getUserList(page, startIndex, endIndex);
            allUsers = allUsers.concat(users);
            currentFloor += users.length;
            if (endIndex !== undefined && currentFloor >= endIndex) {
                break;
            }
        }
        const uniqueUsers = [];
        const seen = new Set();
        for (const user of allUsers) {
            if (!seen.has(user)) {
                uniqueUsers.push(user);
                seen.add(user);
            }
        }
        console.log(`Unique Users: ${uniqueUsers.join(', ')}`);
        return uniqueUsers;
    }
    
    function shuffle(array, seed) {
        const rng = lcg(seed);
        console.log(`Shuffling with Seed: ${seed}`);
        for (let i = array.length - 1; i > 0; i--) {
            const j = Math.floor(rng() * (i + 1));
            console.log(`Shuffling: i=${i}, j=${j}`);
            if (array[i] !== undefined && array[j] !== undefined) {
                [array[i], array[j]] = [array[j], array[i]];
            } else {
                console.log(`Error: array[i] or array[j] is undefined. i=${i}, j=${j}, array[i]=${array[i]}, array[j]=${array[j]}`);
            }
        }
        return array;
    }
    
    function lcg(seed) {
        let state = hashCode(seed);
        console.log(`LCG State: ${state}`);
        const a = 1664525;
        const c = 1013904223;
        const m = Math.pow(2, 32);
        return function() {
            state = (a * state + c) % m;
            console.log(`LCG Next: ${state / m}`);
            return state / m;
        }
    }
    
    function hashCode(str) {
        l
    ![]( https://i.v2ex.co/VlpwI4y5.jpeg)
    et hash = 0;
        for (let i = 0; i < str.length; i++) {
            const char = str.charCodeAt(i);
            hash = ((hash << 5) - hash) + char;
            hash |= 0;
        }
        return Math.abs(hash);
    }
    
    // Example usage:
    handleRequest('123456', 3, seed123, 10);
    这里的 '123456' 是帖子 ID ,3 是抽奖人数,'seed123' 是种子(可选),10 是最大楼层(可选)。
    在 V2EX 网站里用浏览器 consele 运行就可以
    

    为了方便 v 友进行类似抽奖我糊了一个 cloudflare work 来使用直接请求 https://v2ex.240801.xyz/?tid=12345&num=10&seed=12345&maxFloor=11(大佬别打,刚装上访问不了可能 cf 路由没设置好)这里的参数说明为 tid 为帖子 id num 为中奖人数 seed 为随机数种子 maxFloor 为最高楼层

    下面是橙子链接请大家按需购买

    ps 甜度不用担心,今年我们县的都甜。现摘现发没有清洗打腊,只要老家不下雨一般下单第二天就发货。室温 20 度以内还是很耐放的两三个星期没啥问题。有个卖橙子的微信群想加的加我微信 console.log(atob('enF3MjAwOA=='))拉群

    第 1 条附言  ·  86 天前

    今年的橙子我也还没吃上,这图都是前段时间家时机人拍的现在橙子应该黄一些了。

    为啥这次的代码块没有高亮。。。代码看起来多,是因为ai 写的取回复用户数没用v站的api,下面是去除相关爬虫代码后的代码:https://gist.github.com/zzerding/d80d25149a74c0dd96516d949e9e52b7

    cloudflare work我会改为api形式代码后续在上面的gist里更新

    晚上12点以后可根据规则自行开奖

    第 2 条附言  ·  86 天前

    统一回复一下:

    橙子大约9分甜一分酸,每个人的口感不一样可以进群问问去年的v友

    可以发顺丰。晚上熬夜开的团图片先错了,里面有顺丰的商品链接大约江西周边9省53r/5kg

    好了不摸鱼了领导催进度了

    第 3 条附言  ·  85 天前

    感谢大家的支持,因为是现摘发货一天也就发300斤,受天气影响22号下单的会在24或者25号发完,先下单的先发,介意的可以申请退款。

    抽奖

    使用node自行开奖

    看到515楼我知道这个正则表达式取用户非常不可靠(但是当前代码好像没有注入成功),所以改用v2ex api。 使用方式

    恭喜以下用户

    @AchieveHF @xwh @ruiy @kuzhan 可以回复联系方式或者带主页截图找我领奖

    544 条回复    2024-12-04 10:27:27 +08:00
    1  2  3  4  5  6  
    sds7ss
        201
    sds7ss  
       86 天前
    分母+1
    flyation
        202
    flyation  
       86 天前
    碰运气!
    cyhulk
        203
    cyhulk  
       86 天前
    必须分子
    zzerd
        204
    zzerd  
    OP
       86 天前
    @WalterHs 有顺丰的链接,就是本来我想做两个团的一个邮政一个顺丰但是晚上精力不够早上加上了顺丰的商品链接
    pridealloverme
        205
    pridealloverme  
       86 天前
    争取分子
    Aeolusire
        206
    Aeolusire  
       86 天前
    碰碰运气
    yangmabi
        207
    yangmabi  
       86 天前
    分母
    xiaoxixi
        208
    xiaoxixi  
       86 天前
    我分母一下
    vipfts
        209
    vipfts  
       86 天前
    已下单, 别抽我 , 抽到我也要
    zhengxiexie2
        210
    zhengxiexie2  
       86 天前
    分母+1
    weirking
        211
    weirking  
       86 天前
    重在参与!
    kangwei
        212
    kangwei  
       86 天前
    重在参与
    AkiseAru
        213
    AkiseAru  
       86 天前
    非常好橙子
    huan90s
        214
    huan90s  
       86 天前
    分母,中不了就买一箱
    jyootai
        215
    jyootai  
       86 天前
    分子来了
    delonglimin
        216
    delonglimin  
       86 天前
    我一定会吃上
    codersun123
        217
    codersun123  
       86 天前
    我爱 VC
    sjqmmd
        218
    sjqmmd  
       86 天前
    重在参与!
    lessMonologue
        219
    lessMonologue  
       86 天前
    支持一下!
    codingbody
        220
    codingbody  
       86 天前
    参与一下
    tracyliu
        221
    tracyliu  
       86 天前
    做个分母 重在参与
    czkm1320
        222
    czkm1320  
       86 天前
    分母哈哈
    yansideyu
        223
    yansideyu  
       86 天前
    万一中了呢
    Regened
        224
    Regened  
       86 天前
    成为分母
    annet
        225
    annet  
       86 天前
    重在参与!
    COW
        226
    COW  
       86 天前 via Android
    参与下
    dreampet
        227
    dreampet  
       86 天前
    分母+1
    zhangpulin
        228
    zhangpulin  
       86 天前
    分母还是分子
    lixiaobai913
        229
    lixiaobai913  
       86 天前
    尝试中奖 体会 v 友的开心
    geeppeng233
        230
    geeppeng233  
       86 天前
    已经买了一箱了,这个再支持一下
    wangzh
        231
    wangzh  
       86 天前
    已下单
    zhaolin191544
        232
    zhaolin191544  
       86 天前
    参与一下
    mscsky
        233
    mscsky  
       86 天前
    还能参加吗
    HMYQ
        234
    HMYQ  
       86 天前 via Android
    成为分母
    9527000
        235
    9527000  
       86 天前
    参与
    AchieveHF
        236
    AchieveHF  
       86 天前
    有果冻橙吗
    bey0nd
        237
    bey0nd  
       86 天前
    后排
    huxKKK
        238
    huxKKK  
       86 天前
    踊跃参与!
    zard999
        239
    zard999  
       86 天前
    重在参与
    kingwang
        240
    kingwang  
       86 天前
    重在参与
    rangerxo
        241
    rangerxo  
       86 天前
    先来一箱试试口感
    xdlailai
        242
    xdlailai  
       86 天前
    参与下
    jasmineJo
        243
    jasmineJo  
       86 天前
    重在参与!
    CoCoCorina
        244
    CoCoCorina  
       86 天前
    try
    wyshp
        245
    wyshp  
       86 天前
    重在参与,祝橙子大卖,财源滚滚来~
    qhx1018
        246
    qhx1018  
       86 天前
    做个分母
    suilin
        247
    suilin  
       86 天前
    参与
    Zz09
        248
    Zz09  
       86 天前
    分母+1
    2Inception
        249
    2Inception  
       86 天前
    参与一下,需要补点维 C
    hulalalla
        250
    hulalalla  
       86 天前
    做个分母
    AmItheRobot
        251
    AmItheRobot  
       86 天前
    来个分母
    axo
        252
    axo  
       86 天前
    分母分母
    h1t
        253
    h1t  
       86 天前
    分母+1
    lada05
        254
    lada05  
       86 天前
    严谨啊
    renyijiu
        255
    renyijiu  
       86 天前
    支持一下
    5ibug
        256
    5ibug  
       86 天前
    分母
    shl1n
        257
    shl1n  
       86 天前
    股市失意,v 站得意。
    coolloves
        258
    coolloves  
       86 天前
    分母+1
    codebs
        259
    codebs  
       86 天前
    我来组成分母
    mingself
        260
    mingself  
       86 天前
    我是大学生
    ooTwToo
        261
    ooTwToo  
       86 天前
    好吃真好吃
    wujb07
        262
    wujb07  
       86 天前
    参与一下
    L4Linux
        263
    L4Linux  
       86 天前
    祝自己好运
    Ljxtt
        264
    Ljxtt  
       86 天前 via Android
    分子加一
    yang12345
        265
    yang12345  
       86 天前
    买了一份 zc 一下
    kin7
        266
    kin7  
       86 天前
    做个分子,感谢 OP
    airwalkers
        267
    airwalkers  
       86 天前
    楼主好人
    Kevin2
        268
    Kevin2  
       86 天前 via Android
    做个分母
    skykk1op
        269
    skykk1op  
       86 天前
    做个分母
    niko12138
        270
    niko12138  
       86 天前
    重在参与!
    Alwaysonline
        271
    Alwaysonline  
       86 天前
    试试运气+1
    yxlian
        272
    yxlian  
       86 天前
    冲冲冲!
    ravour
        273
    ravour  
       86 天前 via iPhone
    重在参与
    jackple
        274
    jackple  
       86 天前
    来来来
    Godzilla123
        275
    Godzilla123  
       86 天前
    重在参与!
    shoalyu
        276
    shoalyu  
       86 天前
    分母+1
    anychuan
        277
    anychuan  
       86 天前
    +1
    t1o1
        278
    t1o1  
       86 天前
    参与一下
    GG668v26Fd55CP5W
        279
    GG668v26Fd55CP5W  
       86 天前 via iPhone
    我要来十箱
    SKYNE
        280
    SKYNE  
       86 天前
    重在参与
    nodejx
        281
    nodejx  
       86 天前
    参与下
    pengjl
        282
    pengjl  
       86 天前
    分子+1
    parallx
        283
    parallx  
       86 天前
    分母+1
    echodone
        284
    echodone  
       86 天前
    重在参与~
    songjiaxin2008
        285
    songjiaxin2008  
       86 天前
    参与一下~
    wikilab
        286
    wikilab  
       86 天前 via Android
    参与一下
    lastrush
        287
    lastrush  
       86 天前
    1
    timedivision
        288
    timedivision  
       86 天前 via iPhone
    让我试试 我明天一个橙子
    zzdgfv
        289
    zzdgfv  
       86 天前
    参与一下
    cobbage
        290
    cobbage  
       86 天前 via Android
    参加下
    Muyiafan
        291
    Muyiafan  
       86 天前
    分母+1
    keepme
        292
    keepme  
       86 天前
    提前谢谢
    ywl19891989
        293
    ywl19891989  
       86 天前
    分母+1
    Feedmo
        294
    Feedmo  
       86 天前
    代码加点注释,我一个后端怎么看得懂🤣
    leeside
        295
    leeside  
       86 天前 via iPhone
    分母 重在参与
    charisna
        296
    charisna  
       86 天前
    参与一下
    6c9fd
        297
    6c9fd  
       86 天前
    试试
    typetest
        298
    typetest  
       86 天前
    分子++
    iThink
        299
    iThink  
       86 天前
    重在参与
    pwli
        300
    pwli  
       86 天前
    分子
    1  2  3  4  5  6  
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   872 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:47 · PVG 04:47 · LAX 12:47 · JFK 15:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.