七牛云壁纸API:使用Python遍历,php读取图片文件列表写入txt文件
- 共 3,307 次检阅

php程序:

<?php

if($_GET['folder']){
$folder=$_GET['folder'];
}else{
$folder='nothumb';
}

//存有图片链接的文件名img.txt
$filename = "img.txt";
if(!file_exists($filename)){
    die('文件不存在');
}
 
//从文本获取链接
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)){
    $line=trim(fgets($fs));
    if($line!=''){
        array_push($pics, $line);
    }
}
 
//从数组随机获取链接
$pic = $pics[array_rand($pics)];
 
//返回指定格式
$type=$_GET['type'];
switch($type){
 
//JSON返回
case 'json':
    header('Content-type:text/json');
    die(json_encode(['pic'=>$pic]));
 
default:
    die(header("Location: $pic"));
}
?>

Python程序:

# -*- coding: utf-8 -*-
# flake8: noqa
import json
from qiniu import Auth
from qiniu import BucketManager

access_key = 'access_key '
secret_key = 'secret_key '

q = Auth(access_key, secret_key)
bucket = BucketManager(q)

bucket_name = 'wallpapero'

prefix = None
limit = 1000
delimiter = None
marker = None

ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter)
imglist = json.dumps(ret['items'])
imglist = json.loads(imglist)
with open("/www/wwwroot/imgapi/img.txt", "w") as f:
    for imgs in imglist:
        imglink = imgs['key']
        imglink = '七牛云绑定玉米带http' + imglink
        imglink += "\n"
        f.write(imglink)

 

分享到:

这篇文章还没有评论

发表评论