Skip to content

Commit

Permalink
upload the code
Browse files Browse the repository at this point in the history
  • Loading branch information
harryhare committed Mar 17, 2021
1 parent 176fe4d commit 2f29662
Show file tree
Hide file tree
Showing 27 changed files with 837 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# IDE
.idea/
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.phony: push build bash run

build:
docker build -t 1point3acres -f docker/dockerfile .

bash:
docker run --rm -it --entrypoint=bash 1point3acres

run:
docker run --rm -it --entrypoint=python3 1point3acres _1point3acres.py

push:
docker tag 1point3acres:latest 655445328103.dkr.ecr.us-east-1.amazonaws.com/1point3acres:latest
docker push 655445328103.dkr.ecr.us-east-1.amazonaws.com/1point3acres:latest
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# 1point3acres
一亩三分地自动签到 答题 https://www.1point3acres.com/bbs/

## 作用
自动执行一亩三分地论坛的签到和答题

### github action 模式


### AWS-lambda 模式


### crontab 模式
4 changes: 4 additions & 0 deletions configure/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[{
"username": "replace_with_your_username",
"password": "repalce_with_your_password"
}]
16 changes: 16 additions & 0 deletions docker/debian.aliyun.sources.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
#deb http://mirrors.aliyun.com/debian-security buster/updates main
#deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
#deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

#deb-src http://mirrors.aliyun.com/debian-security buster/updates main
#deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
#deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
#deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

# deb http://snapshot.debian.org/archive/debian/20210111T000000Z buster main
deb http://mirrors.aliyun.com//debian buster main
# deb http://snapshot.debian.org/archive/debian-security/20210111T000000Z buster/updates main
deb http://mirrors.aliyun.com//debian-security buster/updates main
# deb http://snapshot.debian.org/archive/debian/20210111T000000Z buster-updates main
deb http://mirrors.aliyun.com//debian buster-updates main
6 changes: 6 additions & 0 deletions docker/debian.image.source.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# deb http://snapshot.debian.org/archive/debian/20210111T000000Z buster main
deb http://deb.debian.org/debian buster main
# deb http://snapshot.debian.org/archive/debian-security/20210111T000000Z buster/updates main
deb http://security.debian.org/debian-security buster/updates main
# deb http://snapshot.debian.org/archive/debian/20210111T000000Z buster-updates main
deb http://deb.debian.org/debian buster-updates main
16 changes: 16 additions & 0 deletions docker/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# for running docker locally
# debian
# size: 729MB
from debian:latest
copy docker/debian.aliyun.sources.list /etc/apt/sources.list
copy docker/pip.conf /root/.pip/pip.conf
run apt-get update
#run apt-get -y install python3 python3-pip tesseract-ocr
run apt-get -y install autoconf python3 python3-pip tesseract-ocr
run pip3 install requests lxml pytesseract cssselect
workdir /app
copy example /app/example/
copy *.py *.json /app/
env PYTHONIOENCODING=utf8
CMD ["python3","_1point3acres.py"]

17 changes: 17 additions & 0 deletions docker/dockerfile.aws.lambda
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# for aws lambda deploy
# debian + awslambdaric
# size: 729MB
from debian:latest
copy docker/debian.aliyun.sources.list /etc/apt/sources.list
copy docker/pip.conf /root/.pip/pip.conf
#run echo "deb https://notesalexp.org/tesseract-ocr/buster/ buster main" >> /etc/apt/sources.list
run apt-get update #-oAcquire::AllowInsecureRepositories=true
run apt-get -y install autoconf python3 python3-pip cmake libtool tesseract-ocr
run pip3 install requests lxml pytesseract cssselect awslambdaric
workdir /app
copy example /app/example/
copy *.py *.json /app/
env PYTHONIOENCODING=utf8
ENTRYPOINT ["python3","-m","awslambdaric"]
CMD ["app.handler"]

3 changes: 3 additions & 0 deletions docker/pip.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[global]
index-url = https://mirrors.aliyun.com/pypi/simple

49 changes: 49 additions & 0 deletions docker/ubuntu.image.sources.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ bionic main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ bionic main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ bionic universe
# deb-src http://archive.ubuntu.com/ubuntu/ bionic universe
deb http://archive.ubuntu.com/ubuntu/ bionic-updates universe
# deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu/ bionic multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://archive.ubuntu.com/ubuntu/ bionic-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu bionic partner
# deb-src http://archive.canonical.com/ubuntu bionic partner

deb http://security.ubuntu.com/ubuntu/ bionic-security main restricted
# deb-src http://security.ubuntu.com/ubuntu/ bionic-security main restricted
deb http://security.ubuntu.com/ubuntu/ bionic-security universe
# deb-src http://security.ubuntu.com/ubuntu/ bionic-security universe
deb http://security.ubuntu.com/ubuntu/ bionic-security multiverse
# deb-src http://security.ubuntu.com/ubuntu/ bionic-security multiverse
10 changes: 10 additions & 0 deletions docker/ubuntu18.tencent.sources.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
deb http://mirrors.cloud.tencent.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.cloud.tencent.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.cloud.tencent.com/ubuntu/ bionic-updates main restricted universe multiverse
#deb http://mirrors.cloud.tencent.com/ubuntu/ bionic-proposed main restricted universe multiverse
#deb http://mirrors.cloud.tencent.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic-updates main restricted universe multiverse
#deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic-proposed main restricted universe multiverse
#deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic-backports main restricted universe multiverse
Binary file added example/example1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/example2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/example3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/misc1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/misc2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/misc3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
requests
lxml
pytesseract
Pillow
cssselect


13 changes: 13 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# aws lambda entry
import service
import json
import os


def handler(event, context):
print(os.system("id"))
service.main()
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
6 changes: 6 additions & 0 deletions src/md5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import hashlib

if __name__=="__main__":
m=hashlib.md5()
m.update(b"xxxxxx")
print(m.hexdigest())
7 changes: 7 additions & 0 deletions src/ocr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from PIL import Image
import pytesseract

if __name__ == "__main__":
img_path = 'shard(30, 18).png'
text = pytesseract.image_to_string(Image.open(img_path), lang="eng", config="--psm 10")
print(text)
56 changes: 56 additions & 0 deletions src/process_gif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from PIL import Image
from process_image import get_code_from_image


def split_png(f):
n = f.n_frames
for i in range(n):
f.seek(i)
f.save("misc3_%d.png" % (i))


def print_infos(f):
n = f.n_frames
for i in range(n):
f.seek(i)
print(f"frame:{i}\t duration:{f.info}")


def get_longest_frame_number(f):
frame = 0
max_duration = 0
n = f.n_frames
for i in range(n):
f.seek(i)
d = f.info["duration"]
if d > max_duration:
frame = i
max_duration = d
return frame


def get_longest_frame(f):
n = get_longest_frame_number(f)
f.seek(n)
return f


# this is the function
def get_code_from_gif(f):
f = get_longest_frame(f)
# f.show()
code = get_code_from_image(f)
return code


if __name__ == "__main__":
file_path = "../example/misc1.gif"
# file_path = "H:/git/1point3acres/example/misc1.gif"
f = Image.open(file_path)
# split_png(f)
# print_infos(f)
frame = get_longest_frame_number(f)
print("the frame:", frame)
code = get_code_from_gif(f)
print(code)
f.close()
127 changes: 127 additions & 0 deletions src/process_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from PIL import Image
import pytesseract


def print_color_statistic(f):
width, height = f.size
c_map = {}
for x in range(width):
for y in range(height):
c = f.getpixel((x, y))
if c in c_map.keys():
c_map[c] = c_map[c] + 1
else:
c_map[c] = 1
print(c_map)
for k in c_map:
if c_map[k] > 200:
print(k, c_map[k])


visit = []


def dfs(f, x, y, c, range_x, range_y):
global visit
if x < 0 or x > f.size[0] - 1 or y < 0 or y > f.size[1] - 1:
return 0
if f.getpixel((x, y)) != c:
return 0
if visit[y][x] == 1:
return 0
count = 1
visit[y][x] = 1
range_x[0] = min(range_x[0], x)
range_x[1] = max(range_x[1], x)
range_y[0] = min(range_y[0], y)
range_y[1] = max(range_y[1], y)
count += dfs(f, x - 1, y, c, range_x, range_y)
count += dfs(f, x + 1, y, c, range_x, range_y)
count += dfs(f, x, y - 1, c, range_x, range_y)
count += dfs(f, x, y + 1, c, range_x, range_y)
return count


# def dfs_fill(fin,fout,c):


def get_shards(f) -> list:
width, height = f.size
global visit
visit = [[0 for i in range(width)] for j in range(height)]
shard_list = []
for y in range(height):
for x in range(width):
if visit[y][x] == 0:
range_x = [x, x]
range_y = [y, y]
count = dfs(f, x, y, f.getpixel((x, y)), range_x, range_y)
if count > 80:
shard_list.append({"key": (x, y), "count": count, "range_x": range_x, "range_y": range_y})
shard_list.sort(reverse=True, key=lambda x: x["count"])
return shard_list


def get_charaters(shard_list: list, f) -> map:
characters = {}
for shard in shard_list:
c = f.getpixel(shard["key"])
range_x = shard["range_x"]
range_y = shard["range_y"]
print(shard, f"{range_x[1] - range_x[0]}x{range_y[1] - range_y[0]}")

padding = 5
offset_x = range_x[0]
offset_y = range_y[0]
w = range_x[1] - range_x[0] + 1 + padding * 2
h = range_y[1] - range_y[0] + 1 + padding * 2
if range_x[1] - range_x[0] > 25 or range_y[1] - range_y[0] > 25:
continue
img = Image.new('RGB', (w, h), color=(255, 255, 255))
# todo: use dfs_fill instead
for x in range(range_x[1] - range_x[0] + 1):
for y in range(range_y[1] - range_y[0] + 1):
if f.getpixel((x + range_x[0], y + range_y[0])) == c:
img.putpixel((x + padding, y + padding), (0, 0, 0))
text = pytesseract.image_to_string(img, lang="eng", config="--psm 10")
if len(text) == 0:
continue
if not text[0].isalnum():
continue
characters[range_x[0]] = text
# img.save(f'shard{k}.png')
# img.close()
if len(characters) >= 4:
break
return characters


def get_code(characters):
result = ""
keys = sorted(characters.keys())
for k in keys:
result += characters[k][0]
return result


# this is the function
def get_code_from_image(f):
shard_list = get_shards(f)
print(shard_list)
characters = get_charaters(shard_list, f)
print(characters)
code = get_code(characters)
print(code)
return code


if __name__ == "__main__":
file_path = "../example/example1.png"
# file_path = "H:/git/1point3acres/example/example1.png"
f = Image.open(file_path)
# f.show()
print(get_code_from_image(f))

# todo:
# 相似颜色合并
# dfs_fill
Loading

0 comments on commit 2f29662

Please sign in to comment.