Skip to content

Commit

Permalink
refactor(*): make modules loose coupled
Browse files Browse the repository at this point in the history
  • Loading branch information
qqiangwu committed Oct 8, 2016
1 parent 0b0d1af commit 9c5c42d
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 282 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
就是想写代码了!

# 使用方法
由于在从QQ空间内下载所有说说, 因此需要登录. 但考虑到登录复杂度较高, 且不是这个项目的主要目的. 因以, 我使用了一种简洁的方法, 即, 直接在浏览器登录, 然后自己手动复制cookie, 并将其存储到一个文件中(比如cookie.txt).
由于在从QQ空间内下载所有说说, 因此需要登录. 但考虑到登录复杂度较高, 且不是这个项目的主要目的. 因以, 我使用了一种简洁的方法, 即, 直接在浏览器登录, 然后自己手动复制cookie, 并作为命令行参数传入.

## 获取可执行文件
可以直接在github的release中下载jar包, 也可以自己手动编译. 编译方法很简单, 下载项目, 运行`mvn package -Dmaven.test.skip=true`.

## 下载说说
准备好了cookie文件及jar包后, 将其放在同一个目录下, 并在此目录下, 运行`java -jar wx-qzone.jar cookie.txt`.
准备好了cookie及jar包后, 运行`java -jar wx-qzone.jar "p_uin=o0373490201; p_skey=GYatlPmdAIAz9uEvWPA8basA0JrkyCy7hn6jpbwc23U_"`.

一段时间后, 会生成一个以你的QQ命名的json文件.

## Cookie文件需要哪些项?
## Cookie需要哪些项?
QZone登录流程变动很快, 所以我实在懒得去写一个自动登录的程序, 维护成本太高. 直接复制Cookie简单易行. 目前看来, 有两个项是必须复制的, 即`p_uin``p_skey`.

示例如下:
Expand Down
76 changes: 23 additions & 53 deletions src/main/java/me/wuqq/WxQzoneApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,46 @@

import lombok.SneakyThrows;
import lombok.val;
import me.wuqq.core.Fetcher;
import me.wuqq.util.BadCredentialException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import me.wuqq.core.BadCredentialException;
import me.wuqq.core.Driver;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

import static java.nio.charset.StandardCharsets.UTF_8;

@SpringBootApplication
public class WxQzoneApplication {

public static void main(String[] args) {
SpringApplication.run(WxQzoneApplication.class, args);
}

@Component
public static final class Startup implements CommandLineRunner {
@Autowired Fetcher mFetcher;
try {
val context = SpringApplication.run(WxQzoneApplication.class, args);
val driver = context.getBean(Driver.class);

@Override
public void run(final String... args) throws Exception {
try {
val credential = this.getCookie(args);
driver.fetch();

mFetcher.fetch(credential);
System.out.println("\n\nFetching done!");
} catch (BadCredentialException e) {
System.out.println(e.getMessage());
} catch (BeanCreationException e) {
System.out.println(e.getMostSpecificCause().getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());

System.out.println("\n\nFetching done!");
} catch (IllegalArgumentException | BadCredentialException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e.getMessage());

this.dumpError(e);
}
dumpError(e);
}
}

private String getCookie(final String... args) {
if (args.length != 1) {
throw new IllegalArgumentException("Cookie file is not specific in args");
}

val cookieFile = args[0];

try {
val cookie = new String(Files.readAllBytes(Paths.get(cookieFile)), UTF_8);
@SneakyThrows
private static void dumpError(final Exception e) {
val debugFile = "wx-qzone-debug.log";

return cookie;
} catch (IOException e) {
throw new IllegalArgumentException("Cannot load cookie file: " + cookieFile);
}
try (val writer = new FileWriter(debugFile);
val printer = new PrintWriter(writer)) {
e.printStackTrace(printer);
}

@SneakyThrows
private void dumpError(final Exception e) {
val debugFile = "wx-qzone-debug.log";

try (val writer = new FileWriter(debugFile);
val printer = new PrintWriter(writer)) {
e.printStackTrace(printer);
}

System.out.println("\nError details are dumped into " + debugFile);
}
System.out.println("\nSee error details in " + debugFile);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.wuqq.util;
package me.wuqq.core;

/**
* Created by wuqq on 2016/10/2.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/me/wuqq/core/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.wuqq.core;

/**
* Created by wuqq on 16-10-8.
*/
public interface Driver {
void fetch() throws BadCredentialException;
}
8 changes: 6 additions & 2 deletions src/main/java/me/wuqq/core/Fetcher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.wuqq.core;

import me.wuqq.util.BadCredentialException;
import com.fasterxml.jackson.databind.JsonNode;

/**
* Created by wuqq on 16-9-30.
Expand All @@ -9,5 +9,9 @@
*
*/
public interface Fetcher {
void fetch(String credential) throws BadCredentialException;
JsonNode fetchMessagesFromOffset(int i) throws BadCredentialException;

String getTargetQQ();

int getPageSize();
}
63 changes: 63 additions & 0 deletions src/main/java/me/wuqq/impl/DriverImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package me.wuqq.impl;

import lombok.val;
import me.wuqq.core.*;
import me.wuqq.domain.QZoneMeta;
import me.wuqq.domain.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* Created by wuqq on 16-10-8.
*/
@Component
public final class DriverImpl implements Driver {
@Autowired Archiver mArchiver;
@Autowired Progressor mProgressor;
@Autowired Extractor mExtractor;
@Autowired Fetcher mFetcher;

QZoneMeta mMetaInfo;

@Override
public void fetch() throws BadCredentialException {
this.initComponents();
this.doFetch();
}

private void initComponents() throws BadCredentialException {
mMetaInfo = this.fetchMeta();

mArchiver.init(mMetaInfo);
mProgressor.init(mMetaInfo.getRecordCount());
}

private void doFetch() throws BadCredentialException {
val recordCount = mMetaInfo.getRecordCount();
val recordPageSize = mFetcher.getPageSize();

for (int offset = 0; offset < recordCount; offset += recordPageSize) {
val records = this.fetchRecordsFromOffset(offset);

mProgressor.advance(records.size());
mArchiver.save(records);
}
}

private QZoneMeta fetchMeta() throws BadCredentialException {
val data = mFetcher.fetchMessagesFromOffset(0);

val recordCount = mExtractor.extractRecordCount(data);
val username = mExtractor.extractUsername(data);

return new QZoneMeta(recordCount, username, mFetcher.getTargetQQ());
}

private List<Record> fetchRecordsFromOffset(final int offset) throws BadCredentialException {
val rawData = mFetcher.fetchMessagesFromOffset(offset);

return mExtractor.extractRecords(rawData);
}
}
Loading

0 comments on commit 9c5c42d

Please sign in to comment.