分類
程序

github用法小记

github还是挺复杂的,记录下免得下次还得搜。首先按照官方文档在网页上建好项目,本地进入对应目录初始化号帐号。然后添加remote:git remote add origin_loc https://github.com/pggdt/项目名.git。然后git pull origin_loc master同步一下。最后将本地的修改提交到github:git commit可以查看本地修改过的文件。git add 文件名,可以添加等下要上传的文件。添加后再用git commit为修改做评价。最后git push origin_loc master就提交完成了。

我就是这么做的,不保证是最正确的做法,有更好意见的请提出来。

網頁註冊賬號,本地安裝git。如果想使用SSH鑒權,秩序把本機的~/.ssh/id_rsa.pub文件內容添加到https://github.com/settings/keys即可。進入要存放項目的目錄,執行git clone [email protected]:pggdt/ulogger-server.git克隆github上的項目到本地。創建cn分支來實現自己的feature git checkout -b cn。然後通過編輯器編輯代碼,完成後通過git status查看變化的文件。通過git add changedFile 來添加文件。通過 git commit -m "更新內容"來提交更新。最後推送到github上git push origin cn。

本文更新於 2018/01/25。

分類
程序

python3批量下图

又发现一个图集,用python3抓下:

import urllib.request def main(): url='http://cdn.test.com/downloads/character' path='/home/me/Pictures/paper/paper-' for i in range(1,400): ii='' if i<10: ii="00"+str(i) elif i<100: ii="0"+str(i) else: ii=str(i) url=url+ii+".jpg" print (url) h='' try: doc=urllib.request.urlopen(url) h=str(doc.info()) except urllib.error.HTTPError: print(ii+'is not exist') if('jpeg' in h): path=path+ii+".jpg" data = urllib.request.urlopen(url).read() f = open(path,"wb") f.write(data) f.close() path='/home/me/Pictures/paper/paper-' print (str(ii)+"OK") url='http://cdn.test.com/downloads/character' if __name__ == "__main__": main()[/code]

有两点改变,python2 import的是urllib,这里用到urlopen,需要import urllib.request。还有就file已经不用了,换成open就好了。getheader函数也没有了。可以对比下python2版本的另一篇python批量下图

分類
程序

python批量下图

有个网站图片很好看,网址也很有规律,但是有的是空链接。用python都下下来了。

import urllib
def main():
  url='http://static.host.com/wallpapers/picture-'
  path='/home/me/Pictures/wallpaper/picture-'
  for i in range(0,400):
    url=url+str(i)+".jpg"
    print url
    doc=urllib.urlopen(url)
    type=doc.info().getheader('Content-Type')
    if(type.find('jpeg')!=-1):
      path=path+str(i)+".jpg"
      data=urllib.urlopen(url).read()
      f=file(path,"wb")  
      f.write(data)  
      f.close()
      path='/home/me/Pictures/wallpaper/picture-'
      print str(i)+"OK"
    url='http://static.host.com/wallpapers/picture-'

if __name__ == "__main__":
  main()

本文更新於 2014/05/26。

分類
程序

mysql添加一列

ALTER TABLE `locTest` ADD `user_id` INT NOT NULL AFTER `ID`

在ID列后添加非空新列user_id。用的INT数据类型,由于设置非空,之前的行会自动设置user_id为“0”。

分類
程序

mysqli设置utf8编码

  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  mysqli_set_charset($dbc, "utf8"); 
分類
程序 网站

使用header对页面授权

此法效果类似路由器的登录页面,会弹出一个登录页。适合一个admin的简单授权管理。结束会话(关闭浏览器)后授权即失效。

首先创建authorize.php,这样只需在需要授权的页面引用即可。内容如下:

<?php
  // User name and password for authentication
  $username = 'rock';
  $password = 'roll';

  if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
    ($_SERVER['PHP_AUTH_USER'] != $username) || ($_SERVER['PHP_AUTH_PW'] != $password)) {
    // The user name/password are incorrect so send the authentication headers
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Basic realm="特征码"');//需要相同认证的页面标记
    exit('<h2>Hi</h2>Display this message when user select CANCEL.');
  }
?>

在需要授权的页面开始处添加:

<?php
  require_once('authorize.php');
?>

需要注意的是上面这段引用,即授权代码必须放在最前面,其<?php前不能有任何字符,即使是空格也不行。

分類
程序

java批量下图

这个代码没什么太大用途,权当复习一下java,里面的例外也处理。作用就是能下载http://www.style.com/fashionshows/complete/slideshow/F2014MEN-AMCQUEEN/这个相册里的30张图。

package fatch;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author 42
 */
public class Fatch {

    /**
     * 本程序获取http://www.style.com/fashionshows/complete/slideshow/F2014MEN-AMCQUEEN/相册中的30张图片
     * 通过查看图片属性发现其文件名有简单规律,虽不连续但是是有顺序的
     * first,last是网址的前半部分和后半部分
     * 先判断文件是否存在,然后将文件下载到本地
     */
    public static void main(String[] args) throws MalformedURLException, IOException {
        String imagesource="";
        String first="http://www.style.com/fashion-shows/fall-2014-menswear/london/alexander-mcqueen/collection/_ARC0";
        String last=".1366x2048.JPG";
        
        InputStream inputStream;  
        byte[] getData = null;
        for(int i=1,x=0;x<30&&i<=500;i++){
            if(i<10){
                imagesource=first+"00"+i+last;
            }else if((i>=10)&&(i<100)){
                imagesource=first+"0"+i+last;
            }else if((i>=100)&&(i<1000)){
                imagesource=first+i+last;
            }
        URL url = new URL(imagesource);  
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        try {
            inputStream = conn.getInputStream();      //通过输入流获得图片数据
            getData = readInputStream(inputStream);     //获得图片的二进制数据  
        } catch (IOException ex) {
            getData=null;
        }
        
        if(getData!=null){
        File imageFile = new File(i+".JPG");    
        FileOutputStream fos = new FileOutputStream(imageFile);
        fos.write(getData);             
        fos.close();  
          
        System.out.println(x+"picture(s) downloaded"); 
        x++;
        }
        }
        
    }  
  
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {  
        byte[] buffer = new byte[1024];  
        int len = 0;  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
        while((len = inputStream.read(buffer)) != -1) {  
            bos.write(buffer, 0, len);  
        }  
          
        bos.close();  
        return bos.toByteArray();
    }
}