Open main menu

Contents

update.pyEdit

# [[Emoji]] page updater code, needs Python and pywikibot https://www.mediawiki.org/wiki/Manual:Pywikibot/Installation in order to run
#
import pywikibot, json, urllib
commons = pywikibot.Site('commons', 'commons')
emojiData = json.loads(pywikibot.Page(commons, 'Data:Emoji/List.tab').text)

def emojiFileName(theme, unicode):
    unicode = unicode.replace('fe0f_20e3', '20e3') # keycaps names fix
    if theme == 'noto': return 'Emoji u' + unicode.replace('_fe0f', '').replace('_', ' ') + '.svg'
    if theme == 'noto-pie': return 'Noto Emoji Pie ' + unicode.replace('_fe0f', '').replace('_', ' ') + '.svg'
    if theme == 'noto-oreo': return 'Noto Emoji Oreo ' + unicode.replace('_fe0f', '').replace('_', ' ') + '.svg'
    if theme == 'noto-kitkat': return 'Noto Emoji KitKat ' + unicode.replace('_fe0f', '').replace('_', ' ') + '.svg'
    if theme == 'noto-lollipop': return 'Noto Emoji Lollipop ' + unicode.replace('_fe0f', '').replace('_', ' ') + '.svg'
    if theme == 'android-old': return 'Android Emoji ' + unicode.replace('_', ' ') + '.svg'
    if theme == 'twitter': return 'Twemoji2 ' + unicode.lstrip('0').replace('_', '-') + '.svg'
    if theme == 'fx': return 'Fxemoji u' + unicode.upper().replace('_', ' ') + '.svg'
    if theme == 'one-v1': return 'Emojione1 ' + unicode.upper().replace('_', '-') + '.svg'
    if theme == 'one': return 'Emojione ' + unicode.upper().replace('_', '-') + '.svg'
    if theme == 'one-bw': return 'Emojione BW ' + unicode.upper().replace('_', '-') + '.svg'
    if theme == 'phantom': return 'Phantom Open Emoji ' + unicode.replace('_', ' ') + '.svg'
    if theme == 'openmoji': return 'OpenMoji-color ' + unicode.upper().replace('_', '-') + '.svg'
    if theme == 'openmoji-black': return 'OpenMoji-black ' + unicode.upper().replace('_', '-') + '.svg'

#emojiData['data'] = emojiData['data'][:5]

def availableEmojisOfTheme(theme):
    print('Retrieving available Emojis of "%s"' % theme)
    return list(json.loads(urllib.request.urlopen("http://tools.wmflabs.org/linkstranslator/", urllib.parse.urlencode({
        "p": '|'.join(['File:' + emojiFileName(theme, emoji[1]) for emoji in emojiData['data']]),
        "from": "commons",
        "to": "commons"
    }).encode('utf-8')).read().decode('utf-8')).keys())

availableEmojis = {theme: availableEmojisOfTheme(theme) for theme in ['noto', 'noto-pie', 'noto-oreo', 'noto-kitkat', 'noto-lollipop', 'android-old', 'twitter', 'one-v1', 'one', 'one-bw', 'fx', 'phantom', 'openmoji', 'openmoji-black']}

def resolveEmoji(theme, unicode):
    name = "File:" + emojiFileName(theme, unicode)
    if name not in availableEmojis[theme]: return ''
    return ' [[' + name + '|48px]]'

text = """{{:Emoji/Table/Intro}}
{| class="wikitable" style="text-align: center; table-layout: fixed;"
|-
! # !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s !! %s""" % (
    'Unicode',
    'Name and keywords',
    'Emoji',
    '[[:Category:Noto Color Emoji Pie|Noto Color Emoji, Pie]]',
    '[[:Category:Noto Color Emoji Oreo|Noto Color Emoji, Oreo]]',
    '[[:Category:Noto Color Emoji|Noto Color Emoji, Nougat]]',
    '[[:Category:Noto Color Emoji Lollipop|Noto Color Emoji, Lollipop]]',
    '[[:Category:Noto Color Emoji KitKat|Noto Color Emoji, KitKat]]',
    '[[:Category:Android Emoji|Android Emoji]]',
    '[[:Category:Twitter Emoji|Twitter Emoji]]',
    '[[:Category:Emoji One v1|EmojiOne v1]]',
    '[[:Category:Emoji One|EmojiOne v2]]',
    '[[:Category:Emoji One BW|EmojiOne v2 BW]]',
    '[[:Category:Firefox OS Emoji|Firefox OS Emoji]]',
    '[[:Category:Phantom Open Emoji|Phantom Open Emoji]]',
    '[[:Category:OpenMoji Color|OpenMoji]]',
    '[[:Category:OpenMoji Black|OpenMoji Black]]'
)

for i, emoji in enumerate(emojiData['data']):
    text += """
|-
! %d
| %s
|style="text-align: initial"| %s
|style="font-size: 48px; line-height: 0.5;"| %s
|%s
|%s
|%s
|%s
|%s
|%s
|%s
|%s
|%s
|%s
|%s
|%s
|%s
|%s""" % (
        i + 1,
        ' <br> '.join(['<code>' + code + '</code>' for code in emoji[1].split('_')]),
        emoji[2]['en'] + ' <br><small>(' + emoji[3]['en'].replace(' |', ',') + ')</small>',
        emoji[0],
        resolveEmoji('noto-pie', emoji[1]),
        resolveEmoji('noto-oreo', emoji[1]),
        resolveEmoji('noto', emoji[1]),
        resolveEmoji('noto-lollipop', emoji[1]),
        resolveEmoji('noto-kitkat', emoji[1]),
        resolveEmoji('android-old', emoji[1]),
        resolveEmoji('twitter', emoji[1]),
        resolveEmoji('one-v1', emoji[1]),
        resolveEmoji('one', emoji[1]),
        resolveEmoji('one-bw', emoji[1]),
        resolveEmoji('fx', emoji[1]),
        resolveEmoji('phantom', emoji[1]),
        resolveEmoji('openmoji', emoji[1]),
        resolveEmoji('openmoji-black', emoji[1])
    )

page = pywikibot.Page(commons, 'Emoji/Table')
page.text = text + '\n|}'
page.save("update from [[Data:Emoji/List.tab]]")

no listed imagesEdit

import pymysql
import pandas as pd
import pywikibot as pwb
conn = pymysql.connect(host='commonswiki.labsdb', read_default_file="~/replica.my.cnf")
conn.cursor().execute('use commonswiki_p')
df = pd.read_sql_query("""
SELECT page_title
  FROM page
 WHERE page_namespace = 6
   AND page_title LIKE 'Noto_Emoji_Pie_%'
   AND page_title NOT IN (
     SELECT il_to
     FROM imagelinks
     WHERE il_from = (
       SELECT page_id
         FROM page
        WHERE page_title = 'Emoji/Table'
          AND page_namespace = 0
     )
   )
""", conn)
site = pwb.Site('commons', 'commons')
page = pwb.Page(site, 'Talk:Emoji/Not_used_Noto_Emoji_Pie')
page.text = '<gallery>\n' + '\n'.join([(b'File:' + title + b'|' + title).decode('utf8') for title in df['page_title']]) + '\n</gallery>'
page.save('Update')

androidemojis.pyEdit

import fontforge, re
# https://github.com/android/platform_frameworks_base/blob/jb-mr2.0.0-release/data/fonts/AndroidEmoji.ttf
for g in fontforge.open('AndroidEmoji.ttf').glyphs():
    if g.codepoint == None:
        print(g.glyphname + ' has not code')
        continue
    name = 'Android Emoji ' + g.codepoint.lower().replace('u+', '') + '.svg'
    g.export(name)
    file = open(name)
    orig = file.read()
    text = re.sub('viewBox="([-\d]+) ([-\d]+) ([-\d]+) ([-\d]+)"',
                  lambda m: 'viewBox="%s %s %s %s"' % (
                      int(m.group(1)) - 30,
                      int(m.group(2)) + 200,
                      int(m.group(3)) - 30,
                      int(m.group(4)) + 200
                  ),
                  orig)
    file.close()
    file = open(name, 'w')
    file.write(text)
    file.close()

disable throttleEdit

diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index 24d2266..f9d65af 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -251,7 +251,7 @@ class Throttle(object):
         else:
             pywikibot.log(message)
 
-        time.sleep(seconds)
+        #time.sleep(seconds)
 
     def __call__(self, requestsize=1, write=False):
         """Block the calling program if the throttle time has not expired.

update.pyEdit

# very very very unclean code and I should clean it up sometime, needs python 3
import pywikibot
import os
from tqdm import *
import hashlib
import urllib

commons = pywikibot.Site()
commons.login()
pywikibot.config.put_throttle = 0

def hash_file(file):
    return hashlib.sha512(file.read()).hexdigest()

for f in tqdm(os.listdir('/home/ebrahim/a')):
    #if '-' in f:
    #    continue

    fileName = '/home/ebrahim/a/' + f
    #title = 'Emojione1_' + f #.split('-')[0] + '.svg'
    title = f
    #print(title)
    source_hash = hash_file(open(fileName, 'rb'))
    page = pywikibot.FilePage(commons, title='File:' + title)

    if page.exists():
        continue
        #try:
        dest_hash = hash_file(urllib.request.urlopen(page.fileUrl()))
        #except:
        #    print("skipped")
        #    continue

        if source_hash == dest_hash:
            print('%s was already updated' % (title))
        else:
            commons.upload(page, source_filename=fileName, comment='update', ignore_warnings=True)

        #p = pywikibot.FilePage(commons, title='File:' + 'Emojione_' + f)
        #if p.exists() and (p.title() != page.title()):
        #    p.delete('Dupe to [[File:' + title + ']]')

    elif False and f.replace('.svg', '').upper() != f.replace('.svg', ''):
        p = pywikibot.Page(commons, title='File:' + 'Emojione_BW_' + f)
        if p.exists():
            p.move('File:' + 'Emojione_BW_' + f.upper().replace('.SVG', '.svg'), 'normalize files names', deleteAndMove=True)

    #p = pywikibot.Page(commons, title='File:' + 'Emojione_BW_' + f)
    #if p.exists() and p.isRedirectPage():
    #    p.delete('Dupe to [[File:' + title + ']]')
    #continue
    commons.upload(page, source_filename=fileName, comment="""=={{int:filedesc}}==
{{Information
|description = {{en|A colored [[:en:Emoji|Emoji]] from Android project}}
|date = 2012-07-11
|source = Project URL
|author = AuthorAuthor
}}

=={{int:license-header}}==
{{Apache|AuthorAuthor}}

[[Category:CATCATCAT]]""", ignore_warnings=True)

move.pyEdit

import pywikibot
from tqdm import *
commons = pywikibot.Site('commons', 'commons')
poeList = pywikibot.Page(commons, 'User:Ebrahim/sandbox2')
_, *poe = [line.split(' -> ') for line in poeList.text.split('\n')]

for row in tqdm(poe):
    page = pywikibot.FilePage(commons, 'File:' + row[0])
    if page.isRedirectPage():
        continue
    page.move('File:Phantom Open Emoji ' + row[1] + '.svg', 'normalizing Emoji files names per [[Talk:Emoji]]')
    #break

permalinkEdit

import pywikibot, json, urllib
from tqdm import *

commons = pywikibot.Site("commons", "commons")
emojiData = json.loads(pywikibot.Page(commons, 'Data:Emoji/List.tab').text)

def emojiFileName(theme, unicode):
    unicode = unicode.replace('fe0f_20e3', '20e3') # keycaps names fix
    if theme == 'noto': return 'Emoji u' + unicode.replace('_', ' ') + '.svg'
    if theme == 'android-old': return 'Android Emoji ' + unicode.replace('_', ' ') + '.svg'
    if theme == 'twitter': return 'Twemoji2 ' + unicode.lstrip('0').replace('_', '-') + '.svg'
    if theme == 'fx': return 'Fxemoji u' + unicode.upper().replace('_', ' ') + '.svg'
    if theme == 'one-v1': return 'Emojione1 ' + unicode.upper().replace('_', '-') + '.svg'
    if theme == 'one': return 'Emojione ' + unicode.upper().replace('_', '-') + '.svg'
    if theme == 'one-bw': return 'Emojione BW ' + unicode.upper().replace('_', '-') + '.svg'
    if theme == 'phantom': return 'Phantom Open Emoji ' + unicode.replace('_', ' ') + '.svg'

def availableEmojisOfTheme(theme):
    print('Retrieving available Emojis of "%s"' % theme)
    return list(json.loads(urllib.request.urlopen("http://tools.wmflabs.org/linkstranslator/", urllib.parse.urlencode({
        "p": '|'.join(['File:' + emojiFileName(theme, emoji[1]) for emoji in emojiData['data']]),
        "from": "commons",
        "to": "commons"
    }).encode("utf-8")).read()).keys())

for title in tqdm(availableEmojisOfTheme("twitter")[:2]):
    p = pywikibot.Page(commons, title)
    p.text = (
        p.text
        .replace("|source = https://github.com/twitter/twemoji/tree/gh-pages/2\n",
                 "|source = https://github.com/twitter/twemoji/blob/55443d4/2/svg/" +
                     p.title(underscore=True).replace("File:Twemoji2_", "") + "\n")
    )
    p.save("use a permanent link as source")
Return to "Emoji/code" page.