-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,36 @@ | ||
""" | ||
bot for importing files from nccommons to wikipedia | ||
from newapi.wiki_page import MainPage | ||
""" | ||
import sys | ||
# --- | ||
# from newapi.wiki_page import MainPage, NEW_API | ||
from newapi.ncc_page import MainPage as ncc_MainPage, NEW_API as ncc_NEW_API | ||
from newapi import printe | ||
''' | ||
page = MainPage(title, 'ar', family='wikipedia') | ||
exists = page.exists() | ||
text = page.get_text() | ||
save_page = page.save(newtext='', summary='', nocreate=1, minor='') | ||
''' | ||
# --- | ||
from nc_import.bots import upload_file | ||
# upload = upload_file.upload_by_url(file_name, text, url, comment='', code="en", family="wikipedia") | ||
|
||
def get_file_text(title): | ||
title = f"File:{title}" if not title.startswith("File:") else title | ||
printe.output(f"<<yellow>>get_file_text: {title} from nccommons:") | ||
|
||
page = ncc_MainPage(title, "www", family="nccommons") | ||
text = page.get_text() | ||
|
||
return text | ||
|
||
|
||
def import_file(title, code): | ||
printe.output(f"<<yellow>>import_file: File:{title} to {code}wiki:") | ||
|
||
# --- | ||
file_text = get_file_text(title) | ||
# --- | ||
api_new = ncc_NEW_API('www', family='nccommons') | ||
# api_new.Login_to_wiki() | ||
img_url = api_new.Get_image_url(title) | ||
# --- | ||
upload = upload_file.upload_by_url(title, file_text, img_url, comment='Bot: import from nccommons.org', code=code, family="wikipedia") | ||
# --- | ||
return upload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
[[File:NCCommonsLogo.svg|thumb|120px|[https://nccommons.org{{localurl:File:{{{1|NCCommonsLogo.svg}}}}} Importing] from NC Commons pending]] | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#!/usr/bin/python3 | ||
""" | ||
Usage: | ||
# --- | ||
from nc_import.bots import upload_file | ||
# upload = upload_file.upload_by_url(file_name, text, url, comment='', code="en", family="wikipedia") | ||
# --- | ||
""" | ||
# | ||
# (C) Ibrahem Qasim, 2023 | ||
# | ||
# --- | ||
import requests | ||
import urllib.request | ||
import tempfile | ||
import os | ||
# --- | ||
|
||
from newapi import printe | ||
from newapi.ncc_page import NEW_API | ||
# api_new = NEW_API('www', family='nccommons') | ||
# api_new.Login_to_wiki() | ||
# json1 = api_new.post_params(params, addtoken=False) | ||
|
||
|
||
def download_file(url): | ||
try: | ||
# Download the file to a temporary location | ||
temp_file_path, _ = urllib.request.urlretrieve(url) | ||
print(f"File downloaded to: {temp_file_path}") | ||
return temp_file_path | ||
except Exception as e: | ||
print(f"An error occurred while downloading the file: {e}") | ||
return None | ||
|
||
|
||
def upload_by_file(file_name, text, url, comment="", code="en", family="wikipedia"): | ||
# --- | ||
if file_name.startswith("File:"): | ||
file_name = file_name.replace("File:", "") | ||
# --- | ||
# get the file from url | ||
file_path = download_file(url) | ||
# --- | ||
params = {"action": "upload", "format": "json", "filename": file_name, "comment": comment, "text": text, "utf8": 1} | ||
# --- | ||
api_new = NEW_API(code, family=family) | ||
# api_new.Login_to_wiki() | ||
# --- | ||
result = api_new.post_params(params, addtoken=True, files={"file": open(file_path, "rb")}) | ||
# --- | ||
upload_result = result.get("upload", {}) | ||
# --- | ||
success = upload_result.get("result") == "Success" | ||
error = result.get("error", {}) | ||
error_code = result.get("error", {}).get("code", "") | ||
error_info = result.get("error", {}).get("info", '') | ||
# --- | ||
# {'upload': {'result': 'Warning', 'warnings': {'duplicate': ['Buckle_fracture_of_distal_radius_(Radiopaedia_46707).jpg']}, 'filekey': '1amgwircbots.rdrfjg.13.', 'sessionkey': '1amgwircbots.rdrfjg.13.'}} | ||
# --- | ||
duplicate = upload_result.get("warnings", {}).get("duplicate", [""])[0].replace("_", " ") | ||
# --- | ||
if success: | ||
printe.output(f"<<lightgreen>> ** upload true .. [[File:{file_name}]] ") | ||
return True | ||
|
||
if duplicate: | ||
printe.output(f"<<lightred>> ** duplicate file: {duplicate}.") | ||
|
||
if error: | ||
printe.output(f"<<lightred>> error when upload_by_url, error_code:{error_code}") | ||
printe.output(error) | ||
|
||
# ---- | ||
return False | ||
|
||
def upload_by_url(file_name, text, url, comment="", code="en", family="wikipedia"): | ||
# --- | ||
if file_name.startswith("File:"): | ||
file_name = file_name.replace("File:", "") | ||
# --- | ||
params = {"action": "upload", "format": "json", "filename": file_name, "url": url, "comment": comment, "text": text, "utf8": 1} | ||
# --- | ||
api_new = NEW_API(code, family=family) | ||
api_new.Login_to_wiki() | ||
# --- | ||
result = api_new.post_params(params, addtoken=True) | ||
# --- | ||
upload_result = result.get("upload", {}) | ||
# --- | ||
success = upload_result.get("result") == "Success" | ||
error = result.get("error", {}) | ||
error_code = result.get("error", {}).get("code", "") | ||
error_info = result.get("error", {}).get("info", '') | ||
# --- | ||
# {'upload': {'result': 'Warning', 'warnings': {'duplicate': ['Buckle_fracture_of_distal_radius_(Radiopaedia_46707).jpg']}, 'filekey': '1amgwircbots.rdrfjg.13.', 'sessionkey': '1amgwircbots.rdrfjg.13.'}} | ||
# --- | ||
duplicate = upload_result.get("warnings", {}).get("duplicate", [""])[0].replace("_", " ") | ||
# --- | ||
if success: | ||
printe.output(f"<<lightgreen>> ** true .. [[File:{file_name}]] ") | ||
return True | ||
|
||
if duplicate: | ||
printe.output(f"<<lightred>> ** duplicate file: {duplicate}.") | ||
|
||
if error: | ||
printe.output(f"<<lightred>> error when upload_by_url, error_code:{error_code}") | ||
# printe.output(error) | ||
|
||
if error_info == "Uploads by URL are not allowed from this domain.": | ||
return upload_by_file(file_name, text, url, comment=comment, code=code, family=family) | ||
# ---- | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters